Numpy

In this tutorial, you will learn

 

1.  How to import the Numpy library?

2.  How to create a Numpy Array?

3.  How to create create a Numpy array of Zeroes?

4.  How to create a Numpy array of Ones?

5.  How to perform Mathematical Operations on Numpy Array?

6.  How to perform Indexing and Slicing on Numpy Arrays?

7.  How to find the Shape of Numpy Array?

8.  How to change the shape of a Numpy Array?

9.  How to Stack Numpy Arrays?

10.  How to Stack Numpy Arrays along Column?

11.  How to Split a Numpy Array?

12.  How to Copy a Numpy Array?

13.  How to create Views in Numpy Array?

1. Import the Libraries

import numpy as np

2. Create a Numpy Array  

numpy_array=np.array([1,2,3,4,5])
numpy_array
how to create numpy array?

3. Create a Numpy Array of Zeroes                                    

np.zeros((3,3))
how to create a numpy array of zeroes?

4. Create a Numpy Array of Ones

np.ones((3,3))
how to create numpy array of ones?

5. Operations on Numpy Array                                            

 Mathematical operations such as Addition, Subtraction, Multiplication, Division, Dot Product can be applied on numpy arrays.

numpy_array1=np.array([1,2,3,4,5,6,7,8]) 
numpy_array2=np.array([1,1,1,1,1,1,1,1])
print(numpy_array1-numpy_array2)
print(numpy_array1+numpy_array2)
print(numpy_array1/numpy_array2)
print(numpy_array1*numpy_array2)

6.  Indexing and Slicing on Numpy Arrays                                          

Slicing and Indexing helps in manipulation of the numpy arrays easily.

numpy_array1[5]
numpy array slicing example 1
numpy_array1[:5]
numpy array slicing example 2
numpy_array1[5:]
numpy array slicing and indexing example 3
numpy_array1[2:5]
numpy array slicing and indexing example 4

7.  Shape of Numpy Array                                          

numpy_array1.shape

8.  Change the shape of Numpy Array                                          

numpy_array_example=np.array([[1,2,3,4],[5,6,7,8]])
numpy_array_example
how to change the shape of numpy array?
numpy_array_example.reshape(4,2)
how to change the shape of numpy array example 2
numpy_array_example.reshape(-1,2)
how to change the shape of numpy array example 2

9.  Stack Numpy Arrays                                         

np.vstack((numpy_array1,numpy_array2))
how to stack numpy array?

10.  Stack Numpy Arrays along Column                                      

from numpy import newaxis
np.column_stack((numpy_array1,numpy_array2))
how to stack numpy array along column?

11.  Split Numpy Array                                      

split_example=np.array([[1,2,3,4],[5,6,7,8]])
split_example
how to split numpy array example?
np.hsplit(split_example,4)
how to split numpy array?

The array has been split into four equal parts.             

12.  Copy Numpy Array                                      

numpy_array1
numpy_array3 = numpy_array1           
numpy_array3 is numpy_array1
how to create a new object in numpy array?

numpy_array3 and numpy_array1 both are same.  New object has not been created.             

numpy_array3=numpy_array1.copy()
numpy_array3
create a new object in numpy array

13.  Views in Numpy Array                                      

numpy_array3 = numpy_array1.view()
numpy_array3 is numpy_array1
how to create a view in numpy arrays?

 Summary

 

1.  Zeros: To create a Numpy Array of Zeros.

2.  Ones : To create a Numpy Array of Ones

3.  Shape :  To get the shape of Numpy Array.

4.  Stack :  To stack the numpy arrays.

5.  Column_Stack :  To stack the numpy arrays long the column.

6.  Hsplit :  To split the numpy array into equal parts.

7.  CopyTo create a new object in numpy array.

8.  View :  To create a new view in numpy array.

 You can find the Github link here.