Dive deep into NumPy, Python's essential library for scientific computing, with theory, hands-on code, and expert tips from Ram Sir.
Start Learning NumPyRam Sir is a Python data science educator with 2+ years of experience in scientific computing and analytics. He has guided thousands in mastering Python, NumPy, and data-driven problem solving.
NumPy's core is the ndarray, a fast, space-efficient multidimensional array. Arrays are created in many ways:
import numpy as np
a = np.array([1, 2, 3]) # 1D array
b = np.zeros((2, 3)) # 2x3 zero array
c = np.ones(5) # 1D array of ones
d = np.eye(3) # 3x3 identity matrix
e = np.arange(0, 10, 2) # Even numbers [0,2,4,6,8]
f = np.linspace(0, 1, 5) # 5 evenly spaced numbers from 0 to 1
g = np.full((2,2), 7) # 2x2 array filled with 7
a.shape # Dimensions (tuple)
a.dtype # Data type
a.size # Number of elements
a.ndim # Number of dimensions
a.itemsize # Size of one item (bytes)
NumPy arrays are homogeneous and vastly faster than native Python lists for large data.
NumPy supports vectorized operations, broadcasting, and aggregation:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a + b # [5, 7, 9]
d = a * 2 # [2, 4, 6]
e = a ** 2 # [1, 4, 9]
f = a + 10 # [11, 12, 13]
arr = np.array([[1,2,3],[4,5,6]])
arr.sum() # 21
arr.mean(axis=0) # [2.5, 3.5, 4.5]
arr.min() # 1
arr.max(axis=1) # [3, 6]
arr.std() # Standard deviation
arr.reshape((3,2)) # Change shape
arr.T # Transpose
arr.flatten() # 1D copy
arr.ravel() # 1D view (if possible)
Broadcasting allows arithmetic between differently-shaped arrays, e.g., adding a vector to every row of a matrix.
Access and manipulate data efficiently:
a = np.array([10, 20, 30, 40, 50])
a[1] # 20
a[1:4] # [20, 30, 40]
a[-1] # 50
mat = np.array([[1,2,3],[4,5,6]])
mat[0,2] # 3
mat[:,1] # [2,5]
a = np.array([10, 20, 30, 40, 50])
mask = a > 25
a[mask] # [30, 40, 50]
a = np.array([10, 20, 30, 40])
idx = [0, 2]
a[idx] # [10, 30]
Slicing returns views (not copies) when possible. Changing a slice may affect the original array!
NumPy provides many universal functions (ufuncs) and mathematical/statistical tools:
a = np.array([0, np.pi/2, np.pi])
np.sin(a) # [0., 1., 0.]
np.exp(a) # [1., 4.81, 23.1...]
np.log(a+1) # [0., 1.18, 1.54]
a = np.array([1, 2, 3, 4, 5])
np.median(a) # 3.0
np.percentile(a, 80) # 4.2
np.corrcoef(a, a) # Correlation matrix
mat = np.array([[1,2],[3,4]])
np.linalg.inv(mat) # Inverse
np.dot(mat, mat) # Matrix multiplication
np.linalg.eig(mat) # Eigenvalues/vectors
NumPy's rich set of mathematical/statistical functions make it ideal for scientific computing and analytics.
Go further with NumPy's advanced features:
np.random.seed(42)
np.random.rand(2,3) # 2x3 array, uniform [0,1)
np.random.randn(4) # 4 samples, standard normal
np.random.randint(0, 10, 5) # 5 random ints [0,10)
a = np.arange(3) # [0,1,2]
b = np.arange(3)[:,np.newaxis] # [[0],[1],[2]]
a + b # 3x3 grid: pairwise sums
import numpy.ma as ma
a = np.array([1, 2, -99, 4])
masked = ma.masked_values(a, -99)
masked.mean() # Ignores -99
Explore np.save, np.load for saving arrays, and np.vectorize for custom vectorized operations.
Results will appear here...
Explore the most important NumPy functions with examples:
a = np.zeros((3,2))
b = np.linspace(0,1,5)
x = np.arange(6).reshape(2,3)
y = x.flatten()
a = np.array([1,2,3,4])
idx = np.where(a%2==0)
x = np.random.randn(5)
m = np.mean(x)
A = np.eye(2)
B = np.linalg.inv(A)
np.random.seed(0)
data = np.random.randint(0,10,4)