# NumPy package in Python: A tutorial for beginners

One of the main reasons Python is being so vastly used in machine learning and AI is the fact that it has a great choice of libraries. These libraries provide the base to the data scientists and developers who don't have to code from the scratch their projects and in fact, can concentrate more on the complex algorithms and work with large datasets.

One such library is the "NumPy" library. It is short for Numerical Python and is the fundamental package for scientific computing with Python. NumPy guarantees great execution speed comparing it with python standard libraries. It comes with a great number of built-in functions. It provides a high-performance multidimensional array object and tools for working with these arrays. These arrays are called NumPy Arrays.

In this article, I am going to show you some ways you can work with the NumPy Arrays.

## What is NumPy Array?

A NumPy array is a multi-dimensional matrix of numerical data values (integers or floats). A NumPy array allows only for numerical data values. NumPy arrays are different from the lists in Python that allow arbitrary data types. It is even more restrictive than focusing only on numerical data values. It has to be of **homogeneous** data values as well. This means that a NumPy array contains either integer or float values, but not both at the same time.

## Installation of NumPy

To start working with NumPy Arrays, we have to first install the NumPy package as it doesn't come with basic Python by default. Use pip to install NumPy package on your command prompt.

`pip install numpy`

To get started, we have to first import the NumPy library to start using in our program.

For that, we write,

`import numpy as np`

This statement will load all the modules and functions available in the NumPy package to the memory and we start using them. Here, we are giving NumPy, a shortened name, 'np', to make our code easier to read and work with. Everytime we have use 'NumPy.function' we can write 'np.function'

## Getting Started

Let's start working with NumPy arrays.

NumPy Arrays is a table of elements (usually numbers), all of the same type, indexed by a tuple of non-negative integers. In NumPy, dimensions are called *axes*.

For example, the coordinates of a point in 3D space [1, 2, 1] have one axis. That axis has 3 elements in it, so we say it has a length of 3. In the example below, the array has 2 axes. The first axis has a length of 2, the second axis has a length of 3.

```
[[1 , 0 , 2],
[3 , 2 , 1]]
```

NumPy’s array class is called *ndarray*. It is also known by the alias array.

**Different ways of creating a ****NumPy**** array:**

Create 1 D array

```
import numpy as np
nump_arr = np.array([1,2,3])
```

This will create a one dimensional from the already existing python array:

```
import numpy as np
py_arr = [1,2,3]
nump_arr = np.array(py_arr)
```

Create a 2D array:

```
import numpy as np
nump_arr = np.array([[1,2,3,4,5],
[6,7,8,9,10]])
```

Another way to create an array in NumPy is by using zeros, ones, or empty functions:

```
arr_zeros = np.zeros(3)
print(arr_zeros)
arr_ones = np.ones((3,2))
print(arr_ones)
arr_empty = np.empty(5)
print(arr_empty)
arr_full = np.full((2,3),4)
print(arr_full)
```

The function **zeros** create an array full of zeros, the function **ones** create an array full of ones, and the function **empty** creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64. Function full

will create an array of 2 * 3, full of 4s.

```
Output:
[0. 0. 0.]
[[1. 1.]
[1. 1.]
[1. 1.]]
[0. 0.25 0.5 0.75 1. ]
[[4 4 4]
[4 4 4]]
```

Another cool feature is the ability to create different arrays like random arrays:

`np.random.rand(2,3)`

It will create a 2 * 3 array of random numbers between 0 and 1

```
array([[0.32482924, 0.51608091, 0.21548059],
[0.6142247 , 0.2120958 , 0.39264305]])
```

while

`np.random.rand(2,3)* 100`

will create a 2 * 3 array of random numbers between 0 to 100.

```
Output:
array([[94.20171774, 19.99361621, 90.99119916],
[60.80955174, 48.8203693 , 84.60342955]])
```

You can also define the size of the array in a different way: