Broadcasting in NumPy
What is broadcasting?
Section titled “What is broadcasting?”Broadcasting is NumPy’s ability to perform operations on arrays with different shapes by automatically expanding (virtually) smaller arrays.
This is a major reason NumPy code is concise and fast. Nothing is actually copied in memory — NumPy just repeats the smaller array’s values as if it were stretched to fit, without allocating that extra memory.
Simple example: add scalar
Section titled “Simple example: add scalar”import numpy as np
arr = np.array([1, 2, 3])
print(arr + 10) # [11 12 13]Here, the scalar 10 is broadcast to match the shape (3,).
Example: add vector to matrix
Section titled “Example: add vector to matrix”import numpy as np
mat = np.array([
[1, 2, 3],
[4, 5, 6]
])
vec = np.array([10, 20, 30])
print(mat + vec)vec (shape (3,)) broadcasts across each row.
Broadcasting rules (must know)
Section titled “Broadcasting rules (must know)”When operating on two arrays, NumPy compares shapes from the trailing dimension (rightmost first) and works backward.
Two dimensions are compatible when:
- They are equal, OR
- One of them is
1
If dimensions are incompatible → broadcasting error.
Example: column vector + matrix
Section titled “Example: column vector + matrix”import numpy as np
mat = np.array([
[1, 2, 3],
[4, 5, 6]
])
col = np.array([100, 200]).reshape(2, 1)
print(mat + col)col has shape (2, 1) and broadcasts across columns.
Common broadcasting error
Section titled “Common broadcasting error”import numpy as np
mat = np.zeros((2, 3))
vec = np.array([1, 2])
# mat + vec -> ValueError (shapes (2,3) and (2,) not compatible)Fix by reshaping vec to a column vector if that’s what you intend:
vec = vec.reshape(2, 1)
print(mat + vec)flowchart TD A["Compare shapes,
trailing axis first"] --> B{"Axis sizes equal
or one is 1?"} B -- "Yes, for every axis" --> C["Broadcast succeeds
virtually stretch the smaller array"] B -- "No, some axis mismatches" --> D["ValueError:
shapes not aligned"]
Why broadcasting is useful in analytics
Section titled “Why broadcasting is useful in analytics”- Normalize columns:
X / X.max(axis=0) - Center data:
X - X.mean(axis=0) - Apply weights:
X * weights
Continue to: NumPy Arithmetic Operations for element-wise math and matrix operations.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Broadcast a Scalar
Section titled “Exercise 1 – Broadcast a Scalar”Exercise 2 – Broadcast a Row Vector Across a Matrix
Section titled “Exercise 2 – Broadcast a Row Vector Across a Matrix”Exercise 3 – Fix a Shape Mismatch
Section titled “Exercise 3 – Fix a Shape Mismatch”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading