NumPy Arrays
Published: 2022-12-31 05:56:41

**## NumPy**

NumPy (or Numpy) is a Linear Algebra Library for Python. The reason it is so important for Data Science with Python cause almost all of the libraries in the PyData Ecosystem rely on NumPy as one of their main building blocks. NumPy is also incredibly fast as it has bindings to C libraries. One should use Arrays instead of Lists because of its efficiency is more than the Python's lists. We will now only learn the basics of NumPy. To get started, we need to install it!

**Installation instructions**

It is highly recommended, you install Python using the Anaconda distribution to make sure all underlying dependencies (such as Linear Algebra Libraries) all sync up with the use of a condo install. If you have Anaconda, install NumPy by going to your terminal or command prompt and type,

`conda install numpy
`
If you cannot download Anaconda in your PC then you can also use Google Colab, also for more-http://docs.scipy.org/doc/numpy-1.10.1/user/install.html 
Using numpy

Once you have installed numpy you can import it as a library or in Google Colab you have to write the below sentence in the first cell and ran it. Afterwards, you can do whatever you want.

```
import numpy as np

```

**NumPy Arrays**

NumPy Arrays are the main way we will use NumPy. NumPy Arrays essentially come in two flavors- Vectors and Matrices. Vectors are strictly 1D Arrays and Matrices are 2D Arrays ( you should know that Matrix can it still have one row and one column. One Row Matrix is also called as Row Vector and one Column Matrix is also called as Column Vector.

**Creating NumPy Arrays**
From a Python List

We can create an array by directly converting a list or list of lists.

```
my_list = [1,2,3]
my_list
```

```
np.array(my_list)
```

```
my_matrix = [[1,2,3],[4,5,6],[7,8,9]]
my_matrix
```

```
np.array(my_matrix)
```


## Built-in methods

 
There are lots of built-in ways to generate Arrays.

**arange**- '.arange()'

Return evenly spaced values within a given interval.

```
np.arange(0,10)
```

```
np.arange(0,11,2)
```

**zeros and ones 
**- '.zeroes()', '.ones()'
Generate arrays of zeros or ones.

```
np.zeros(3)
```

```
np.zeros((5,5))
```

```
np.ones(3)
```

```
np.ones((3,3))
```

**linspace**- '.linspace()'
Return evenly spaced numbers over a specified interval.

```
np.linspace(0,10,3)
```

```
np.linspace(0,10,50)
```

**eye**

Creates an Identity Matrix.

```
np.eye(4)
```

**

## Random
**

NumPy also has lots of ways to create random number Arrays.

**rand
**
Create an array of the given shape and populate it with random samples from a uniform distribution over `[0,1)`

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

```
np.random.rand(5,5)
```

**randn**

Return a sample (or samples) from the "standard normal" distribution. Unlike ".rand()" which is uniform,

```
np.random.randn(2)
```

```
np.random.randn(5,5)
```

**randint**

Return random integers from <u>low </u>(_inclusive_) to <u>high </u>(_exclusive_)

```
np.random.randint(1,100)
```

```
np.random.randint(1,100,10)
```

## **Array Attributes and Methods**
Let's see some useful attributes and methods of an array:

```
arr = np.arange(25)
ranarr = np.random.randint(0,50,10)
```

```
arr
```

```
ranarr
```

**Reshape**

Returns an array containing the same data with a new shape.

```
arr.reshape(5,5)
```
**max, min, argmax, argmin **
In the below, these are useful methods for finding Max or Min value also to find their index locations using argmin or argmax.

```
ranarr
```

```
ranarr.max()
```

```
ranarr.argmax()
```

```
ranarr.min()
```

```
ranarr.argmin()
```





**Shape**

Shape is an attribute that Arrays have, not a method.

```
# Vector
arr.shape
```

```
# Notice the two sets of brackets
arr.reshape(1,25)
```

```
arr.reshape(1,25).shape
```

```
arr.reshape(25,1)
```

```
arr.reshape(25,1).shape
```
**dtype**

You can also grab the data type of the object in the array.

```
arr.dtype
```
;)