# PY2. NumPy - Numerical Python

### Introduction

NumPy (or Numpy) is a Linear Algebra Library for Python, the reason it is so important for Data Science with Python is that 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.

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 conda install.

## 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 conda install. If you have Anaconda, install NumPy by going to your terminal or command prompt and typing:

```plaintext
conda install numpy
```

If you do not have Anaconda and can not install it, please refer to [Numpy’s official documentation on various installation instructions.](http://docs.scipy.org/doc/numpy-1.10.1/user/install.html)

### How to code using NumPy?

To use NumPy in our program, first, we have to import it. To do this, we have to write the following line:

```python
import numpy as np
```

### Convert a 1D list to a 1D array using NumPy:

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603414937/7b77ab67-35af-421b-9979-78a1180b7974.png align="left")

### Convert a 2D list to a 2D array:

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603416955/2ad7df66-e74e-4950-89c3-718cab86828f.png align="left")

### numpy.arrange()

`np.arrange()` is like `range()` that we use in loops. We can write this in three ways:

`arange(stop)`: Values are generated within the half-open interval `[0, stop)` (in other words, the interval includes the ***start*** but excludes the ***stop***).

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603418774/025694f5-45a9-4745-bd0e-2f17b5c5b1a3.png align="left")

`arange(start, stop)`: Values are generated within the half-open interval `[start, stop)`.

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603420753/5abfec40-d615-4c70-a85c-6f169b19a7c7.png align="left")

`arange(start, stop, step)` Values are generated within the half-open interval `[start, stop)`, with spacing between values given by `step`.

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603422696/ebd97fbd-99bf-4ce3-b19f-3528e16ca37f.png align="left")

### Zeros and Ones

`np.zeros()` return a new array of given shape and type, filled with zeros.

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603424005/ee99dcc7-6fd4-483f-8473-6ab873c713a2.png align="left")

`np.ones()` return a new array of given shapes and types, filled with zeros.

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603425424/31613fff-2de3-4859-818f-4baf7e36fe9f.png align="left")

### **numpy.linspace()**

`numpy.linspace()` returns evenly spaced numbers over a specific interval.

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603426688/09f6e0a6-7cfd-48f4-bf4b-9f4b556d6da2.png align="left")

### numpy.eye()

It returns a 2-D array with ones on the diagonal and zeros elsewhere.

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603428532/e19fd221-e149-40f9-98ab-835ff77650bd.png align="left")

### Create an array with random numbers

We can use the `random` module to create an array with random numbers.

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603429683/369a59c3-9a3b-4ace-8d1f-adf648cb92c8.png align="left")

### Change the shape of an array

We can reshape our array by using `reshape()`. But we have to make sure that the dimension is the same.

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603431179/b96c6f77-6026-4de3-bbbb-a412488802d6.png align="left")

### Find the maximum and minimum of an array and their index

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603433244/76f684c6-6d9a-45da-9295-d1cfb15a77cf.png align="left")

### Find the shape and data type of an array

It will return a tuple with the dimensions of the array.

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603435143/c1cd6919-6b53-4af7-b632-9e4f981fec22.png align="left")

### Array Indexing (1D Array)

NumPy array indexing is similar to list indexing.

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603437011/57afb022-dedb-4ef1-a382-2b8f5c882153.png align="left")

There is a problem in slicing the array. To understand it better, let’s see an example:

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603438376/10f069fd-2196-4e87-80a1-eb67becbb6f2.png align="left")

From the photo above, we can see that, when you edit `c`, it also affects `b`. It is happening because, in the 3rd line, we are not copying the array. Instead of copying, it only shows the live view of the array `b`. So, to fix this issue what we can do is we can use a method called `.copy()`.

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603440142/2a7a16d0-6306-4eba-b80e-8578f43b1a12.png align="left")

Now, changing `c` is not affecting `b`.

### Array Indexing (2D Array)

This code will help you to understand 2D array indexing and slicing. Slicing is almost similar to the 1D array. Just you have to think row and column-wise.

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603441586/a8924b8c-7168-4c59-8ba3-9ab6e4842d17.png align="left")

We can also filter our data using conditions like these:

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603443391/6a7e1586-aafa-4074-b759-da73d3634009.png align="left")

### NumPy Operations

We can add two arrays like a normal variable and what will happen is all the elements will be added with the corresponding other array elements. Other operators will act in the same way.

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1698603445286/9a7d70f5-f4d8-4e4c-869e-99ea290147d6.png align="left")
