Master NumPy In Just A Few Minutes!
Tags: python, machinelearning, datascience, computerscience
Published: 2021-01-06 13:41:51

The objective of this article is to give brief knowledege about NumPy.

### **What is NumPy?**
- NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.
- NumPy is the fundamental package for scientific computing in Python.NumPy stands for **Numerical Python**.

### **What is the purpose of NumPy?**
- NumPy arrays facilitate advanced mathematical and other types of operations on large numbers of data. Typically, such operations are executed more efficiently and with less code than is possible using Python's built-in sequences.

- NumPy is written in **C Language**.

#### **Installing NumPy**

- You can install NumPy using `pip` and if you are using anaconda than you can also install using `conda` .

```sh
pip install numpy
```
```sh
conda install numpy
```

- After setting up numpy let's start exploring numpy.
- I am using Jupyter Notebook for NumPy and Python Programming, but if you do not have Anaconda than you can use Visual Studio Code as well.

#### **Import Numpy**

```py
import numpy as np
```

#### **Array() Function**
- This function is used to convert python array or python list into the numpy array.
- We can also convert python multi-dimensional array into numpy array using this function.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s2y7qtnwaxolu3ci1kp9.PNG)

#### **Arange Function**

- When working with data, you will often come across use cases where you need to generate data.
- NumPy has an “arrange()” method with which you can generate a range of values between two numbers. The arrange function takes the start, end, and an optional distance parameter.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/anz8rsns2zmpkycqqrs1.PNG)

#### **Zeros and Ones Function**

- You can also generate an array or matrix of zeroes or ones using NumPy.
- Also, Both these functions support n-dimensional arrays as well. You can add the shape as a tuple with rows and columns.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l9vp17zadheeia8xiv7s.PNG)

#### **Identity Matrix**

- You can also generate an identity matrix using a built-in NumPy function called “eye”.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/418xxb1luj5sucmuqnfk.PNG)

#### **Linspace Function**

- NumPy has a linspace method that generates evenly spaced points between two numbers. It takes 3 arguments as starting point, ending point and jump.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kj6pt14hwykmerul5lx3.PNG)

#### **Random Number Generation**

- When you are working on machine learning problems, you will often need to generate random numbers. NumPy has in-built functions for that as well.
- First, let's look at two major types of distributions.

**Normal Distribution**

- The normal distribution is a very important concept in statistics since it seen in many natural phenomena. It is also called a “bell curve”.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/oj1ylhg6c2x86p4t0kte.png)

**Uniform Distribution**

- If the values in the distribution have the probability as a constant, it is called a uniform distribution.
- For example, a coin toss has a uniform distribution since the probability of getting either heads or tails in a coin toss is the same.

- To generate random numbers in a uniform distribution, use the `rand()` function from `np.random`.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8n00iqv4xgce3n50i48u.PNG)

- To generate random numbers in a normal distribution, use the `randn()` function from `np.random`.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cmvfqoextgqr27ozvo03.PNG)

- To generate random integers between a low and high value, use the `randint()` function from `np.random`.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ts3jkcnls2p7jwr10zy6.PNG)

- A seed value is used if you want your random numbers to be the same during each computation.
- Whenever you use a seed number, you will always get the same array generated without any change.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pd4arrtsp5skdyz0grgz.PNG)

#### **Reshaping Arrays**

- As a data scientist, you will work with re-shaping the data sets for different types of computations.
- To get the shape of an array, use the shape property.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8qnjraa5ndjwzzt9iw2c.PNG)

- To reshape an array, use the reshape() function.
- Also, reshape only works if the existing structure makes sense. You cannot reshape a 2x2 array into a 3x1 array.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g30q8vrnnmej59gqvpnk.PNG)

#### **Slicing Data**

- Let's look at fetching data from NumPy arrays. NumPy arrays work similarly to Python lists during fetch operations.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ko7tz8citx67y175c63y.PNG)

- If you look at the above example, even though we assigned the slice of “myarr” to the variable “sliced”, changing the value of “sliced” affects the original array. This is because the “slice” was just pointing to the original array.

- To make an independent section of an array, use the `copy()` function.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wueqj0jrj21mqsa5uwwy.PNG)

- Slicing multi-dimensional arrays work similarly to one-dimensional arrays.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i23h8pdjvbhus4uorj5d.PNG)

#### **Array Computations**

- NumPy is known for its speed when performing complex computations on large multi-dimensional arrays.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3ke2alkrs159hkv50tuy.PNG)

- For zero division errors, Numpy will convert the value to NaN (not a number).

- There are also a few in-built computation methods available in NumPy to calculate values like mean, standard deviation, variance, and others.
```py
Sum — np.sum()
Square Root — np.sqrt()
Mean — np.mean()
Variance — np.var()
Standard Deviation — np.std()
```
- While working with 2d arrays, you will often need to calculate row wise or column-wise sum, mean, variance, and so on. 

- You can use the optional axis parameter to specify if you want to choose a row or a column.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s8zf332w7n6pnd85ik81.PNG)

#### **Conditional Operations**

- You can also do conditional filtering with NumPy using the square bracket notation.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/an0e1cut6zyw9lbrozup.PNG)

#### **Conclusion**

- NumPy package has lot more capabilities and especially when working with higher-dimensional data. NumPy is very Fast, Easy to Use and Robust library to work with Data, Manipulating the data.

- You can find complete [jupyter notebook](https://github.com/imdhruv99/ML-DL-Python/blob/main/01_NumPy/Numpy.ipynb) with the contents of this post.

- There are lot more functionalities I have not covered as above are mostly used. If you are interested to learn NumPy i would recommend [NumPy Official Documentation](https://numpy.org/doc/stable/user/absolute_beginners.html).

- I want to Thank You very much for reading this post.