0
0
NumPydata~10 mins

Broadcasting rules in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Broadcasting rules
Start with two arrays
Compare shapes from right
Check dimension sizes
If sizes equal or one is 1
Yes
Broadcast smaller dimension
Repeat for all dimensions
If all dimensions compatible
Yes
Arrays broadcasted to common shape
Perform element-wise operation
End
Broadcasting compares array shapes from right to left, expanding dimensions of size 1 to match the other array, allowing element-wise operations on different shaped arrays.
Execution Sample
NumPy
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([10,20,30])
C = A + B
print(C)
Adds a 2x3 array and a 1x3 array by broadcasting the smaller array across rows.
Execution Table
StepArray A shapeArray B shapeCompare dimsBroadcast actionResulting shapeOperation
1(2, 3)(3,)Compare last dims: 3 and 3No change needed(2, 3)Ready
2(2, 3)(3,)Broadcast B's missing dim to 1Add leading 1 to B shape(2, 3)Ready
3(2, 3)(1, 3)Dims compatible (2 vs 1, 3 vs 3)Broadcast B's first dim from 1 to 2(2, 3)Ready
4(2, 3)(2, 3)Shapes matchElement-wise addition(2, 3)Compute C = A + B
5N/AN/AN/AN/A(2, 3)Output: [[11 22 33] [14 25 36]]
💡 Broadcasting completes when all dimensions are compatible or expanded to match.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
A.shape(2, 3)(2, 3)(2, 3)(2, 3)
B.shape(3,)(3,)(1, 3)(2, 3)
CN/AN/AN/A[[11 22 33] [14 25 36]]
Key Moments - 3 Insights
Why does numpy add a leading 1 to B's shape from (3,) to (1,3)?
Because broadcasting compares shapes from right to left, and B has fewer dimensions, numpy adds a leading 1 to align dimensions for comparison (see execution_table step 2).
What happens if dimensions are not equal and neither is 1?
Broadcasting fails with an error because dimensions must be equal or one must be 1 to broadcast (not shown here, but implied by exit condition in execution_table).
How does broadcasting affect the actual data in arrays?
Broadcasting does not copy data but virtually expands dimensions of size 1 to match the other array for element-wise operations (see variable_tracker for shape changes).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 3, what is B's shape after broadcasting?
A(1, 3)
B)3 ,1(
C(3,)
D(2,)
💡 Hint
Check the 'Resulting shape' column in step 3 of execution_table.
At which step does numpy add a leading dimension to B's shape?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Broadcast action' column describing shape changes.
If B was shape (2, 1) instead of (3,), what would be the resulting shape after broadcasting with A (2,3)?
A(3, 3)
B(2, 1)
C(2, 3)
D(2, 2)
💡 Hint
Broadcasting expands dimensions of size 1 to match the other array's size.
Concept Snapshot
Broadcasting rules:
- Compare shapes from right to left
- Dimensions must be equal or one must be 1
- Add leading 1s to smaller shape if needed
- Expand dimensions of size 1 to match
- Enables element-wise operations on different shaped arrays
Full Transcript
Broadcasting in numpy allows arrays with different shapes to be used together in operations. It works by comparing the shapes of the arrays from right to left. If the dimensions are equal or one is 1, numpy can broadcast the smaller array by virtually expanding its dimensions. For example, adding a (2,3) array to a (3,) array works because numpy treats the (3,) array as (1,3) and then broadcasts it to (2,3). This lets numpy perform element-wise addition without copying data. If dimensions are incompatible, broadcasting fails. This visual trace showed step-by-step how shapes are compared and expanded, how variables change, and how the final result is computed.