NumPy - Numerical Python
Tags: python, machinelearning, datascience, programming
Published: 2022-10-17 18:55:56

### 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:

```
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://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ioknkpux7xr3e6bninq.png)



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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nfzgiekm8roowftepevv.png)



### 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://dev-to-uploads.s3.amazonaws.com/uploads/articles/psb9am9g2bnunolga9dn.png)



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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdgkzqpchp4t5sn6gz0y.png)


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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w6qjwoqkogu6b4wxjyvd.png)



### Zeros and Ones

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0vfzfe4ifys5zyjr6d3.png)



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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6d5lwzshhp8mmcvfusi3.png)



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

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2mbbwecbd0q4shn02uge.png)



### numpy.eye()

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plxqh28ou62rsr92l2xv.png)



### Create an array with random numbers

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjd46efrpfr1u1zv7fbf.png)


### 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://dev-to-uploads.s3.amazonaws.com/uploads/articles/v1695cu6uhemkbpmzws1.png)



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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5e8bwmk6cawpymsuizos.png)



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

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wh8nu1j33xaybiqze87y.png)



### Array Indexing (1D Array)

NumPy array indexing is similar to list indexing.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70ljvju7v3x32ewpffh0.png)



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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zapeei33slgkjchmwvya.png)



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://dev-to-uploads.s3.amazonaws.com/uploads/articles/f19rwmlb023panoerjbj.png)



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://dev-to-uploads.s3.amazonaws.com/uploads/articles/hj6vat0if7fjjzz993cp.png)



We can also filter our data using conditions like these:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mx24613hgudi32eizc8p.png)



### 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 like the same way.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7x96zxeqkq1ni29tsqje.png)