NumPy Mastery
NumPy is the core library for scientific computing in Python. Vector operations based on ndarray are tens to hundreds of times faster than pure Python.
Installation and Import
pip install numpy
import numpy as np
Creating ndarray
# Create from list
a = np.array([1, 2, 3, 4, 5]) # 1D
b = np.array([[1, 2, 3], [4, 5, 6]]) # 2D
print(a.shape) # (5,)
print(b.shape) # (2, 3)
print(b.dtype) # int64
print(b.ndim) # 2
# Special arrays
np.zeros((3, 4)) # 3x4 filled with 0
np.ones((2, 3)) # 2x3 filled with 1
np.eye(4) # 4x4 identity matrix
np.full((3, 3), 7) # 3x3 filled with 7
# Range arrays
np.arange(0, 10, 2) # [0 2 4 6 8]
np.linspace(0, 1, 5) # [0. 0.25 0.5 0.75 1. ]
# Random
np.random.seed(42)
np.random.rand(3, 3) # uniform distribution [0, 1)
np.random.randn(3, 3) # standard normal distribution
np.random.randint(1, 100, (3, 3)) # random integers
Indexing and Slicing
a = np.array([10, 20, 30, 40, 50])
b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Basic indexing
print(a[0]) # 10
print(a[-1]) # 50
print(b[1, 2]) # 6 (row 1, column 2)
# Slicing
print(a[1:4]) # [20 30 40]
print(b[:, 1]) # [2 5 8] (column 1 of all rows)
print(b[0:2, 1:]) # [[2 3] [5 6]]
# Boolean indexing (conditional filtering)
a = np.array([10, 25, 3, 48, 12, 37])
mask = a > 20
print(a[mask]) # [25 48 37]
print(a[a > 20]) # same: [25 48 37]
# Fancy indexing (index array)
idx = np.array([0, 2, 4])
print(a[idx]) # [10 3 12]
Broadcasting
# Scalar operations (applied to all elements)
a = np.array([1, 2, 3, 4, 5])
print(a * 2) # [2 4 6 8 10]
print(a ** 2) # [ 1 4 9 16 25]
print(a > 3) # [False False False True True]
# Element-wise operations (same shape)
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
print(x + y) # [5 7 9]
print(x * y) # [ 4 10 18]
# Broadcasting: auto-expand when shapes differ (following rules)
a = np.array([[1, 2, 3], [4, 5, 6]]) # shape (2, 3)
b = np.array([10, 20, 30]) # shape (3,) → (1, 3) → (2, 3)
print(a + b)
# [[11 22 33]
# [14 25 36]]
# Column vector broadcasting
col = np.array([[100], [200]]) # shape (2, 1) → (2, 3)
print(a + col)
# [[101 102 103]
# [204 205 206]]
Vector Operations
# Math functions
a = np.array([1, 4, 9, 16, 25], dtype=float)
print(np.sqrt(a)) # [1. 2. 3. 4. 5.]
print(np.log(a)) # natural logarithm
print(np.exp(np.array([0, 1, 2]))) # [1. 2.718 7.389]
# Statistical functions
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.sum()) # 21 (total)
print(a.sum(axis=0)) # [5 7 9] (column-wise sum)
print(a.sum(axis=1)) # [ 6 15] (row-wise sum)
print(a.mean()) # 3.5
print(a.std()) # standard deviation
print(a.min(), a.max()) # 1 6
print(a.argmin()) # 0 (index of minimum)
print(a.argmax()) # 5
# Sorting
a = np.array([3, 1, 4, 1, 5, 9, 2, 6])
print(np.sort(a)) # [1 1 2 3 4 5 6 9]
print(np.argsort(a)) # sorted indices
Linear Algebra
# Matrix multiplication (dot product)
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A @ B) # matrix multiply [[19 22] [43 50]]
print(np.dot(A, B)) # same
# Transpose
print(A.T) # [[1 3] [2 4]]
# Inverse matrix
print(np.linalg.inv(A))
# Determinant
print(np.linalg.det(A)) # -2.0
# Eigenvalues/eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(A)
# Solve system of equations: Ax = b
b = np.array([5, 11])
x = np.linalg.solve(A, b) # x = [1. 2.]
# Dot product (vector)
v1 = np.array([1, 2, 3])
v2 = np.array([4, 5, 6])
print(np.dot(v1, v2)) # 32
print(np.linalg.norm(v1)) # vector magnitude (L2 norm)
Array Transformation
a = np.arange(12)
# reshape — maintains total number of elements
b = a.reshape(3, 4) # (12,) → (3, 4)
c = a.reshape(2, 2, 3) # (12,) → (2, 2, 3)
d = a.reshape(-1, 3) # -1 auto-calculated → (4, 3)
# flatten / ravel
print(b.flatten()) # 1D copy
print(b.ravel()) # 1D view (shares memory)
# Stacking (concatenate / stack)
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
np.concatenate([x, y]) # [1 2 3 4 5 6]
np.vstack([x, y]) # [[1 2 3] [4 5 6]]
np.hstack([x.reshape(-1,1), y.reshape(-1,1)]) # column-wise combine
# Splitting
np.split(np.arange(9), 3) # [array([0,1,2]), array([3,4,5]), array([6,7,8])]
Practical Example — Image Processing
# Image = array of pixel values
image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8) # 100x100 RGB
# Grayscale conversion (weighted average)
gray = (0.299 * image[:,:,0] + 0.587 * image[:,:,1] + 0.114 * image[:,:,2]).astype(np.uint8)
# Brightness adjustment (with clipping)
bright = np.clip(image.astype(int) + 50, 0, 255).astype(np.uint8)
# Horizontal flip
flipped = image[:, ::-1, :]
Summary
| Feature | Method |
|---|---|
| Create array | np.array(), np.zeros(), np.arange() |
| Conditional filter | Boolean indexing a[a > 0] |
| Element-wise ops | +, -, *, /, ** |
| Axis aggregation | .sum(axis=0), .mean(axis=1) |
| Matrix multiply | @ operator or np.dot() |
| Shape transform | .reshape(), .flatten() |
NumPy's core strength is vectorization — applying operations to entire arrays without loops.