Introduction
Broadcasting enables NumPy to work with arrays of different shapes during arithmetic operations.
Basic Broadcasting
import numpy as np
# Scalar to array
arr = np.array([1, 2, 3])
print(arr + 5) # [6, 7, 8]
# Array to 1D
a = np.array([[1], [2], [3]])
b = np.array([1, 2, 3])
print(a + b)
# [[2, 3, 4],
# [3, 4, 5],
# [4, 5, 6]]
Broadcasting Rules
- Dimensions match from right to left
- Size must be 1 or equal
- If size is 1, it stretches to match
A = np.ones((3, 4))
B = np.arange(4).reshape(1, 4)
C = np.arange(3).reshape(3, 1)
print(A + B) # Works
print(A + C) # Works
Practical Examples
# Normalize columns
X = np.random.randn(100, 5)
X_normalized = (X - X.mean(axis=0)) / X.std(axis=0)
# Outer product
u = np.arange(4)
v = np.arange(3)[:, np.newaxis]
outer = u + v # 3x4 matrix
Practice Problems
- Normalize data using broadcasting
- Compute outer products
- Add vector to each row/column
- Implement batch operations
- Create multiplication table