NumPy CheatSheet ๐Ÿ”ข

Back to Home

Last updated: 23 Sept 2025

All important Numpy commands for data management and manipulation with examples. ๐Ÿš€

#!python  
import numpy as np  

# Data types  
>>> np.float32(1.0)  
1.0  
>>> np.int_([1, 2, 4])  
array([1,2,4])  
>>> np.arange(3, dtype=np.uint8)  
array([0, 1, 2], dype=uint8)  


# Create arrays  
>>> np.zeros((2,3))  
array([[ 0., 0., 0.], [ 0., 0., 0.]])  
>>> np.arange(2, 10, dtype=np.float64)  
array([2., 3., 4., 5., 6., 7., 8., 9.])  
>>> np.arange(2, 3, 0.2)  
array([2. , 2.2, 2.4, 2.6, 2.8])  
>>> np.linspace(1., 4., 6)  
array([1. , 1.6, 2.2, 2.8, 3.4, 4. ])  
>>> np.random.random((2,3))  
array([[0.27105365, 0.89937328, 0.52478529],  
       [0.57497815, 0.84540218, 0.64585082]])  
>>> np.full((3,3), 5)  
array([[5, 5, 5],  
       [5, 5, 5],  
       [5, 5, 5]])  
>>> np.eye(3,3)  
array([[1., 0., 0.],  
       [0., 1., 0.],  
       [0., 0., 1.]])  


# reshape  
>>> np.arange(9).reshape((3,3))  
array([[0, 1, 2],  
       [3, 4, 5],  
       [6, 7, 8]])  


# Indexing  
>>> x = np.arange(10)  
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])  
>>> x[2:5]  
array([2, 3, 4])  
>>> x[1:7:2]  
array([1, 3, 5])  
>>> x.shape = (2,5)  
array([[0, 1, 2, 3, 4],  
       [5, 6, 7, 8, 9]])  
>>> x[0]  
array([0, 1, 2, 3, 4]  
>>> y = np.arange(35).reshape(5,7)  
array([[ 0,  1,  2,  3,  4,  5,  6],  
       [ 7,  8,  9, 10, 11, 12, 13],  
       [14, 15, 16, 17, 18, 19, 20],  
       [21, 22, 23, 24, 25, 26, 27],  
       [28, 29, 30, 31, 32, 33, 34]]))  
>>> y[1:5:2, ::3]  
array([[ 7, 10, 13],  
       [21, 24, 27]])  


# slicing  
>>> x = np.array([[10,20,30,40], [50,60, 70,80], [90,100, 110, 120 ] ] )  
>>> x  
array([[ 10,  20,  30,  40],  
       [ 50,  60,  70,  80],  
       [ 90, 100, 110, 120]])  
>>> x[1,:]  
array([50, 60, 70, 80])  
>>> x[:2, :]  
array([[10, 20, 30, 40],  
       [50, 60, 70, 80]])  
>>> x[:, 2]  
array([ 30,  70, 110])  
>>> x[np.arange(3), np.array([0,3,1])]  
array([ 10,  80, 100])  
>>> x[::-1,]    # reverse rows  
array([[ 90, 100, 110, 120],  
       [ 50,  60,  70,  80],  
       [ 10,  20,  30,  40]])  
>>> x[:, ::-1]    # reverse cols  
array([[ 40,  30,  20,  10],  
       [ 80,  70,  60,  50],  
       [120, 110, 100,  90]])  
>>> x>30  
array([[False, False, False,  True],  
       [ True,  True,  True,  True],  
       [ True,  True,  True,  True]])  
>>> x[x>30]  
array([ 40,  50,  60,  70,  80,  90, 100, 110, 120])  


# Arr operations  
>>> a = np.arange(5)  
>>> b = np.arange(5)  
>>> a+b  
array([0, 2, 4, 6, 8])  
>>> a-b  
array([0, 0, 0, 0, 0])  
>>> a**2  
array([ 0,  1,  4,  9, 16])  
>>> a>3  
array([False, False, False, False,  True])  
>>> 10*np.sin(a)  
array([ 0.        ,  8.41470985,  9.09297427,  1.41120008, -7.56802495])  
>>> a*b  
array([ 0,  1,  4,  9, 16])  
>>> x = np.random.random((2,3))  
array([[0.95802248, 0.23079335, 0.51334747],  
       [0.5247865 , 0.43557556, 0.39688238]])  
>>> x.sum()  
np.float64(3.059407747578695)  
>>> x.min()  
np.float64(0.2307933513386181)  
>>> x.max(axis=0)  
array([0.95802248, 0.43557556, 0.51334747])  
>>> x.min(axis=1)  
array([0.23079335, 0.39688238])  
>>> y = np.floor(10*x)  
array([[9., 2., 5.],  
       [5., 4., 3.]])  
>>> y.ravel()  
array([9., 2., 5., 5., 4., 3.])  
>>> y.transpose()  
array([[9., 5.],  
       [2., 4.],  
       [5., 3.]])  


# attributes  
>>> x = np.array([1,2,3,4,5])  
>>> x.size  
5  
>>> x.itemsize  
8  


# Broadcasting Operations  
>>> x = np.arange(1, 13).reshape((3,4))  
array([[ 1,  2,  3,  4],  
       [ 5,  6,  7,  8],  
       [ 9, 10, 11, 12]])  
>>> x*3  
array([[ 3,  6,  9, 12],  
       [15, 18, 21, 24],  
       [27, 30, 33, 36]])  
>>> x/3  
array([[0.33333333, 0.66666667, 1.        , 1.33333333],  
       [1.66666667, 2.        , 2.33333333, 2.66666667],  
       [3.        , 3.33333333, 3.66666667, 4.        ]])  
>>> np.empty_like(x) # creates np.arr with same shape & dtype as x with uninitialized contents with arbitrary vals  
array([[4599676419421066581, 4604180019048437077, 4607182418800017408,  
        4608683618675807573],  
       [4610184818551597739, 4611686018427387904, 4612436618365282987,  
        4613187218303178069],  
       [4613937818241073152, 4614688418178968235, 4615439018116863317,  
        4616189618054758400]])   


# More random generation  
>>> np.random.rand(5)  
array([0.29667295, 0.65686954, 0.8066308 , 0.23597214, 0.86011729])  
>>> np.random.rand(3,5)  
array([[0.83296229, 0.22301045, 0.11728102, 0.40824894, 0.26658339],  
       [0.16682019, 0.60629984, 0.01388485, 0.78301687, 0.56539043],  
       [0.03591469, 0.65108814, 0.38361917, 0.18702309, 0.7940847 ]])  
>>> np.random.randint(0, 25, 3)  
array([ 7, 15, 14], dtype=int32)  
>>> np.random.normal(1.0, 3.0, 5) # generate in normal dist format  
array([ 1.23978788,  6.35655894,  5.44832977,  3.64996349, -2.602175  ])  
>>> np.random.uniform(5, 10, 4) # generate 4 nos distributed uniformly b/w 5 & 10 inclusive  
array([8.93810383, 5.61586006, 9.10111054, 5.77172238])  


# NumPy Math Operations  
>>> a = np.array([[10,20], [30,40]], dtype=np.int64)  
array([[10, 20],  
       [30, 40]])  
>>> np.sum(a)  
np.int64(100)  
>>> np.sum(a, axis=0)  
array([40, 60])  
>>> b = np.array([[50,60], [70,80]], dtype=np.int64)  
array([[50, 60],  
       [70, 80]])  
>>> np.add(a,b)   # can also do a+b  
array([[ 60,  80],  
       [100, 120]])  
>>> np.subtract(a,b)   # can also do a-b  
array([[-40, -40],  
       [-40, -40]])  
>>> np.multiply(a,b)   # can also do a*b  
array([[ 500, 1200],  
       [2100, 3200]])  
>>> np.divide(a,b)   # can also do a/b  
array([[0.2       , 0.33333333],  
       [0.42857143, 0.5       ]])  
>>> np.dot(a,b)   # can also do a@b  
array([[1900, 2200],  
       [4300, 5000]])  


# numpy stat operations  
>>> a  
array([[10, 20],  
       [30, 40]])  
>>> np.mean(a)  
np.float64(25.0)  
>>> np.std(a)  
np.float64(11.180339887498949)  
>>> np.var(a)  
np.float64(125.0)  
>>> np.linalg.det(a)  
np.float64(-200.0000000000001)  
>>> np.linalg.matrix_rank(a)  
np.int64(2)  
>>> a.diagonal()  
array([10, 40])  
>>> a.diagonal(offset=-1)  
array([30])  
>>> a.trace()  
np.int64(50)  
>>> eigen_vals, eigen_vectors = np.linalg.eig(a)  
>>> eigen_vals, eigen_vectors  
(array([-3.72281323, 53.72281323]), array([[-0.82456484, -0.41597356],  
       [ 0.56576746, -0.90937671]]))  
>>> a = np.array([1,2])  
>>> b = np.array([3,4])  
>>> c = np.linalg.norm(a-b) # euclidean dist  
np.float64(2.8284271247461903)  
>>> a = np.array([1,0,1])  
>>> b = np.array([1,0,-1])  
>>> angle = np.arccos(a@b)  
np.float64(1.5707963267948966)  
>>> angle*(180/np.pi)  
np.float64(90.0)  
>>> x = np.array(np.arange(16)).reshape((4,4))  
>>> x  
array([[ 0,  1,  2,  3],  
       [ 4,  5,  6,  7],  
       [ 8,  9, 10, 11],  
       [12, 13, 14, 15]])  
>>> u,s,v = np.linalg.svd(x) # Singular Value Decomposition  
>>> u  
array([[-0.09184212, -0.83160389,  0.53515573,  0.11665482],  
       [-0.31812733, -0.44586433, -0.80049034,  0.24334177],  
       [-0.54441254, -0.06012478, -0.00448651, -0.836648  ],  
       [-0.77069775,  0.32561478,  0.26982112,  0.47665141]])  
>>> s  
array([3.51399637e+01, 2.27661021e+00, 1.69970911e-15, 8.07447880e-17])  
>>> v  
array([[-0.42334086, -0.47243254, -0.52152422, -0.57061589],  
       [ 0.72165263,  0.27714165, -0.16736932, -0.6118803 ],  
       [-0.22125808,  0.66846675, -0.67315927,  0.2259506 ],  
       [ 0.50104377, -0.50314233, -0.49684666,  0.49894522]])  

๐Ÿ“ŒPoints to remember: