NumPy in Python

Numpy is numerical library of python. Numpy used for numerical and scientific computing.

NumPy is a multidimensional array and a collection of routines for processing those arrays. We can work with multidimensions x,y and z axis.

We have some methods in numPy library using this we can process multidimensional array.

How to create single dimension array?

import numpy as np

n1 = np.array([10,20,30,40])

How to create multi dimensional array?

import numpy as np
n2 = np.array([10,20,30], [40,50,60])

How to initializing numPy array?

1: With Zero: We can initialize numpy array with zeros.

import numpy as np
n1 = np.zeros((1,2))

We will numPy array with zeros in which one row and two column.

2: With same values: We can initialize numpy array with same values.

import numpy as np
n1 = np.full((2,2),13)

In this we creating a array of two columns and two rows where value is 13.

3: With Range: We can initialize numpy array within a range.

import numpy as np
n1 = np.arange(10,50,5)
Output: ([10,15,20,25,30,35,40,45])

How to change shape of numPy array?

import numpy as np

n1=np.array([1,2,3][4,5,6])
n1.shape
Output: (2,3)

How to change shape of numpy array?

import numpy as np
n1.shape = (3,2)

How to join numPy array?

1: vstack: We can join numPy array vertically.

import numpy as np

n1 = np.array([10,20,30])
n2 = np.array([40,50,60])
np.vstack((n1,n2))

Output: ([ [10,20,30],
           [40,50,60] ])

2: Hstack: We can join numPy array Horizontally .

import numpy as np

n1 = np.array([10,20,30])
n2 = np.array([40,50,60])
np.vstack((n1,n2))

Output: ( [10,20,30, 40,50,60] )

3: Column stack: We can join numPy array column wise.

import numpy as np

n1 = np.array([10,20,30])
n2 = np.array([40,50,60])
np.column_stack((n1,n2))


Output: ([ [10,20],
           [30, 40],
           [50,60] ])

Leave a Comment

Your email address will not be published. Required fields are marked *