Beginner

NumPy Fundamentals

Master NumPy arrays, vectorized operations, broadcasting, statistical functions, and linear algebra for high-performance numerical computing.

Creating Arrays

Python
import numpy as np

# From Python lists
arr = np.array([1, 2, 3, 4, 5])
matrix = np.array([[1, 2, 3], [4, 5, 6]])

# Built-in creation functions
zeros = np.zeros((3, 4))          # 3x4 matrix of zeros
ones = np.ones((2, 3))            # 2x3 matrix of ones
full = np.full((3, 3), 7)        # 3x3 filled with 7
eye = np.eye(4)                   # 4x4 identity matrix

# Sequences
rng = np.arange(0, 10, 2)        # [0, 2, 4, 6, 8]
lin = np.linspace(0, 1, 5)       # [0, 0.25, 0.5, 0.75, 1.0]

# Array properties
print(matrix.shape)    # (2, 3)
print(matrix.dtype)    # int64
print(matrix.ndim)     # 2
print(matrix.size)     # 6

Array Operations (Vectorized)

Python
a = np.array([1, 2, 3, 4])
b = np.array([10, 20, 30, 40])

# Element-wise operations
print(a + b)       # [11, 22, 33, 44]
print(a * b)       # [10, 40, 90, 160]
print(a ** 2)      # [1, 4, 9, 16]
print(np.sqrt(a))  # [1.0, 1.414, 1.732, 2.0]

# Broadcasting (scalar operations)
print(a * 3)       # [3, 6, 9, 12]
print(a + 100)     # [101, 102, 103, 104]

# Boolean operations
print(a > 2)       # [False, False, True, True]
print(a[a > 2])    # [3, 4] - boolean indexing

Indexing and Slicing

Python
m = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

print(m[0, 1])      # 2 (row 0, col 1)
print(m[1, :])      # [4, 5, 6] (entire row 1)
print(m[:, 2])      # [3, 6, 9] (entire column 2)
print(m[0:2, 1:3]) # [[2,3],[5,6]] (submatrix)

# Fancy indexing
print(m[[0, 2]])    # Rows 0 and 2

Reshaping

Python
arr = np.arange(12)

reshaped = arr.reshape(3, 4)     # 3 rows, 4 columns
reshaped = arr.reshape(3, -1)    # -1 auto-calculates
flat = reshaped.flatten()         # Back to 1D
transposed = reshaped.T           # Transpose

Statistical Functions

Python
data = np.array([14, 23, 8, 45, 31, 17, 29])

print(np.mean(data))    # 23.86
print(np.median(data))  # 23.0
print(np.std(data))     # 11.36
print(np.var(data))     # 129.0
print(np.min(data))     # 8
print(np.max(data))     # 45
print(np.sum(data))     # 167
print(np.percentile(data, 75))  # 75th percentile

# Along an axis
m = np.array([[1, 2], [3, 4]])
print(np.sum(m, axis=0))  # [4, 6] column sums
print(np.sum(m, axis=1))  # [3, 7] row sums

Linear Algebra

Python
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

# Matrix multiplication
print(np.dot(A, B))       # or A @ B
print(A @ B)              # [[19,22],[43,50]]

# Other operations
print(np.linalg.det(A))   # Determinant: -2.0
print(np.linalg.inv(A))   # Inverse matrix
eigenvalues, eigenvectors = np.linalg.eig(A)

Random Numbers

Python
rng = np.random.default_rng(seed=42)

uniform = rng.random((3, 3))          # Uniform [0, 1)
normal = rng.normal(0, 1, (1000,))   # Normal distribution
integers = rng.integers(1, 100, 10)  # Random ints
choice = rng.choice(["a", "b", "c"], 5)  # Random choice

NumPy vs Python Lists

Performance: NumPy arrays are up to 100x faster than Python lists for numerical operations. This is because NumPy uses contiguous memory blocks, fixed data types, and C-level optimizations. Always use NumPy for numerical computation.
FeaturePython ListNumPy Array
SpeedSlow (interpreted loops)Fast (C-compiled vectorized)
MemoryMore overhead per elementCompact, contiguous
OperationsElement-by-element loopsVectorized (whole-array)
TypeMixed types allowedHomogeneous type