NumPy Library All A Data Scientist Should Know!

Original article was published by Jitendra Singh Balla on Artificial Intelligence on Medium


  • Core features or functions of Numpy :

First of all , import Numpy library :

import numpy as np

Create an array of one dimension or two dimension as you want:

a = np.array([(1,2,3),(4,5,6)])
print(a)
#output will be an array of two dimensional.

Get dimensions of an array:

a = np.array([(1,2,3),(4,5,6)])
print(a.ndim)
#output : 2

Find data type of array:

a = np.array([(1,2,3),(4,5,6)])
print(a.dtype)
#ouput: int32

Find size of an array:

a = np.array([(1,2,3),(4,5,6)])
print(a.size)
#ouput : 6

Find number of rows and columns:

a = np.array([(1,2,3),(4,5,6)])
print(a.shape)
#output: (2,3) which means array has 2 rows and 3 columns

Use arange function as range and find byte size of each element:

#create an array using arange function
M = np.arange(1000)
print(M.size*M.itemsize)
#output will be like:
4000
which shows that memory occupied by numpy array

Reshape an array:

a = np.array([(1,2,3),(4,5,6)])
a = a.reshape(3,2)
print(a)
#In output we'll have an array of 3 rows and 2 columns because reshape function will change shape of array

Slicing for getting a specific element from an array:

a = np.array([(1,2,3)],(4,5,6)])
print(a[0,2])
#output: 3
print(a[1,1])
#output: 5
print(a[0:,2]) #getting 2nd element from all the rows
#output: [3 5]

Use linespace function:

#print three values between 1 to 5
b = np.linespace(1,3,5)
print(b)
#output: [1. 1.5 2. 2.5 3.]

Some Math function:

a = np.array([(1,2,3),(4,5,6)])
print(a.min())
#output: 1
print(1.max())
#output: 6
print(a.sum())
#output: 21

Sum for row and column , for that we use axis where axis = 0 means column and axis = 1 means row:

a = np.array([(1,2,3),(4,5,6)])
print(a.sum(axis=0))
#output: [4 6 8]
print(a.sum(axis=1))
#output: [6 12]

Finding Square root and Standing deviation:

#for Square root
a = np.array([(1,2,3)])
print(np.sqrt(a))
#output: [1. 1.434 1.732]
#for Standard deviation
print(np.std(a))
#output: 1.2909944

Basic Math function:

#Addition of two arrays
a = np.array([(1,2,3),(4,5,6)])
b = np.array([(1,2,3),(4,5,6)])
print(a+b)
#output: [[2 4 6]
[6 8 10]]
#Substraction of two arrays
print(a-b)
#output: [[0 0 0]
[0 0 0]]
#Multiplication of two arrays
print(a*b)
##output: [[1 4 9]
[9 16 25]]
#Division of two arrays
print(a/b)
##output: [[1 1 1]
[1 1 1]]

Stacking arrays:

#vertical stacking
a = np.array([(1,2,3),(4,5,6)])
b = np.array([(1,2,3),(4,5,6)])
print(np.vstack((a,b)))
#ouput: [[1 2 3]
[3 4 5]
[1 2 3]
[3 4 5]]
#horizontal Stacking
print(np.hstack((a,b)))
#output:[[1 2 3 1 2 3]
[3 4 5 3 4 5]]
#column Stacking
print(np.column_stack((a,b)))

Convert whole array to a column:

a = np.array([(1,2,3),(4,5,6)])
print(a.ravel())
#output: [1 2 3 4 5 6]

Initializing Numpy array with zeros:

import numpy as np 
n1 = np.zeros((2,2))
print(n1)
#output: [[0. 0.]
[0. 0.]]

Initializing Numpy array with same number:

import numpy as np 
n1 = np.full((2,2),10)
print(n1)
#output: [[10 10]
[10 10]]

Initializing Numpy array with random numbers:

import numpy as np 
n1 = np.random.randint((1,100,5)
print(n1)
#output: [10 23 56 86 51]