0
0
NumPydata~10 mins

1D and 2D broadcasting in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - 1D and 2D broadcasting
Start with arrays
Check shapes
Align dimensions
Broadcast smaller array
Perform element-wise operation
Output result
Broadcasting matches array shapes by expanding smaller arrays to perform element-wise operations.
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)
Add a 2D array and a 1D array using broadcasting to get a 2D result.
Execution Table
StepA shapeB shapeBroadcasted B shapeOperationResult element example
1(2,3)(3,)(2,3)Broadcast B to match AB becomes [[10,20,30],[10,20,30]]
2(2,3)(3,)(2,3)Add A and broadcasted B element-wiseC[0,0] = 1 + 10 = 11
3(2,3)(3,)(2,3)Add A and broadcasted B element-wiseC[1,2] = 6 + 30 = 36
4(2,3)(3,)(2,3)Final result C[[11,22,33],[14,25,36]]
💡 Broadcasting completes when shapes align and operation finishes.
Variable Tracker
VariableStartAfter BroadcastFinal
A[[1,2,3],[4,5,6]][[1,2,3],[4,5,6]][[1,2,3],[4,5,6]]
B[10,20,30][[10,20,30],[10,20,30]][[10,20,30],[10,20,30]]
CN/AN/A[[11,22,33],[14,25,36]]
Key Moments - 2 Insights
Why does B get repeated to match A's shape?
Because B has shape (3,) and A has (2,3), numpy repeats B along the missing dimension to (2,3) so element-wise addition works, as shown in execution_table step 1.
Can broadcasting happen if shapes are incompatible?
No, broadcasting requires dimensions to be equal or one of them to be 1. Here, B's shape (3,) is compatible with A's (2,3) because 3 matches and 2 can be broadcasted.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the shape of B after broadcasting at step 1?
A(2,3)
B(3,)
C(1,3)
D(3,2)
💡 Hint
Check the 'Broadcasted B shape' column at step 1 in execution_table.
At which step does the element-wise addition start in the execution_table?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the first step mentioning 'Add A and broadcasted B element-wise'.
If B was shape (2,1) instead of (3,), what would happen to broadcasting?
AB would broadcast to (3,2)
BBroadcasting would fail due to incompatible shapes
CB would broadcast to (2,3) by repeating the second dimension
DB would remain (2,1) and no operation possible
💡 Hint
Recall broadcasting rules: dimension 1 can be stretched to match 3.
Concept Snapshot
1D and 2D broadcasting in numpy:
- Smaller array expands to match larger array shape
- Dimensions must be equal or 1
- Enables element-wise operations without explicit loops
- Example: (2,3) + (3,) broadcasts (3,) to (2,3)
- Result shape matches the larger array
Full Transcript
This lesson shows how numpy broadcasting works between 1D and 2D arrays. We start with two arrays: A with shape (2,3) and B with shape (3,). Because B has fewer dimensions, numpy repeats it along the missing dimension to shape (2,3). Then element-wise addition happens between A and broadcasted B. The final result is a (2,3) array where each element is the sum of corresponding elements from A and B. Broadcasting requires dimensions to be equal or one of them to be 1. This allows simple, efficient operations without manual loops.