Common broadcasting patterns in NumPy - Time & Space Complexity
When using NumPy, broadcasting lets us do math on arrays of different shapes without extra loops.
We want to know how the time to run these operations grows as arrays get bigger.
Analyze the time complexity of the following code snippet.
import numpy as np
A = np.ones((1000, 1000))
B = np.arange(1000).reshape(1000, 1)
C = A + B # Broadcasting B to match A's shape
This code adds a 1000x1000 array to a 1000x1 array by broadcasting the smaller array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Element-wise addition over the full 1000x1000 array.
- How many times: Once for each of the 1,000,000 elements in the result.
As the arrays get bigger, the number of additions grows with the total number of elements in the output.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 100 (10x10) |
| 100 | 10,000 (100x100) |
| 1000 | 1,000,000 (1000x1000) |
Pattern observation: Operations grow roughly with the product of the output array dimensions.
Time Complexity: O(n*m)
This means the time grows proportionally to the total number of elements in the output array.
[X] Wrong: "Broadcasting makes the operation run only as fast as the smaller array."
[OK] Correct: Even though broadcasting avoids copying data, the operation still processes every element in the output array, so time depends on the output size.
Understanding broadcasting time helps you explain how vectorized code scales and why it's efficient compared to loops.
What if we changed the smaller array to have shape (1, m) instead of (n, 1)? How would the time complexity change?
Practice
Solution
Step 1: Understand broadcasting concept
Broadcasting lets NumPy perform element-wise operations on arrays with different shapes by automatically expanding the smaller array.Step 2: Compare options
Only Perform operations on arrays of different shapes without explicit loops correctly describes this feature. Options A, B, and C describe unrelated behaviors.Final Answer:
Perform operations on arrays of different shapes without explicit loops -> Option AQuick Check:
Broadcasting = operations on different shapes [OK]
- Thinking broadcasting requires arrays to have the same shape
- Confusing broadcasting with sorting or type conversion
- Assuming broadcasting changes array data types
Solution
Step 1: Check shapes for broadcasting
Adding a 1D array of shape (4,) to each row of a (3,4) matrix works because the vector matches the number of columns.Step 2: Evaluate options
result = matrix + vectorwherematrix.shape = (3,4)andvector.shape = (4,)matches this pattern.result = matrix + vectorwherematrix.shape = (3,4)andvector.shape = (3,)tries to add a (3,) vector to (3,4) matrix which fails.result = matrix + vector.Twherematrix.shape = (3,4)andvector.shape = (4,)uses transpose incorrectly.result = matrix + vector.reshape(3,1)wherematrix.shape = (3,4)andvector.shape = (4,)reshapes vector wrongly.Final Answer:
result = matrix + vector where matrix.shape = (3,4) and vector.shape = (4,) -> Option CQuick Check:
Vector shape matches columns for row-wise addition [OK]
- Using vector shape matching rows instead of columns
- Trying to transpose 1D vector which has no effect
- Reshaping vector incorrectly before addition
import numpy as np A = np.array([[1, 2, 3], [4, 5, 6]]) B = np.array([10, 20, 30]) C = A + B print(C)
Solution
Step 1: Understand shapes of A and B
A has shape (2,3), B has shape (3,). B will broadcast across rows of A.Step 2: Perform element-wise addition
Each row of A adds B: [1+10, 2+20, 3+30] = [11, 22, 33], [4+10, 5+20, 6+30] = [14, 25, 36].Final Answer:
[[11 22 33] [14 25 36]] -> Option AQuick Check:
Broadcast adds vector to each matrix row [OK]
- Expecting an extra row in output
- Confusing broadcasting with stacking arrays
- Thinking shape mismatch causes error here
import numpy as np X = np.array([[1, 2], [3, 4], [5, 6]]) y = np.array([10, 20, 30]) result = X + y
Solution
Step 1: Check shapes of X and y
X shape is (3,2), y shape is (3,). Broadcasting rules require trailing dimensions to match or be 1.Step 2: Analyze broadcasting compatibility
Here, last dims are 2 (X) and 1 (y) after broadcasting rules, so y broadcasts along columns. The addition works without error.Final Answer:
No error, code runs fine -> Option BQuick Check:
Broadcasting allows (3,2) + (3,) by treating (3,) as (3,1) [OK]
- Assuming broadcasting works if first dims match
- Not checking trailing dimension compatibility
- Thinking reshaping y to (3,1) fixes addition here
M of shape (4,3) and a 1D array v of shape (4,), how can you add v to each column of M using broadcasting?Solution
Step 1: Understand shapes and desired operation
M is (4,3), v is (4,). To add v to each column, v must be reshaped to (4,1) to broadcast along columns.Step 2: Check each option
M + v.reshape(4,1)reshapes v to (4,1), allowing broadcasting with M (4,3).M + vtries to add (4,) to (4,3) which broadcasts incorrectly.M + v.reshape(1,4)reshapes v to (1,4), incompatible.M + v.Tuses transpose on 1D array which has no effect.Final Answer:
M + v.reshape(4,1) -> Option DQuick Check:
Reshape vector to (rows,1) to add to columns [OK]
- Adding vector without reshaping when shapes differ
- Using reshape(1, rows) instead of (rows,1)
- Assuming transpose changes 1D array shape
Ready to prove it?
Pass the test and this pattern moves unknown
