Bird
Raised Fist0
NumPydata~10 mins

Common broadcasting patterns in NumPy - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Common broadcasting patterns
Start with arrays of different shapes
↓
Compare shapes from right to left
↓
Check dimension compatibility
↓
Broadcast arrays
↓
Perform element-wise operation
↓
Result array
Broadcasting compares array shapes from right to left, checks if dimensions are compatible (equal or 1), then stretches arrays to match shapes for 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
Add a 2x3 array A to a 1D array B with 3 elements using broadcasting.
Execution Table
StepA shapeB shapeBroadcasting CheckBroadcasted ShapesOperationResult
1(2,3)(3,)Compare dims: 3 vs 3 (compatible), 2 vs missing (B treated as 1)(2,3), (2,3)Add element-wise[[11,22,33],[14,25,36]]
2N/AN/ANo more dims to compareN/AFinishResult shape (2,3)
💡 Broadcasting succeeds because B's shape (3,) is compatible with A's shape (2,3) by stretching B along the first dimension.
Variable Tracker
VariableStartAfter BroadcastingFinal
A[[1,2,3],[4,5,6]]Same shape (2,3)Same shape (2,3)
B[10,20,30]Broadcasted to [[10,20,30],[10,20,30]] shape (2,3)Broadcasted shape (2,3)
CUndefinedComputed element-wise sum[[11,22,33],[14,25,36]] shape (2,3)
Key Moments - 2 Insights
Why can a 1D array with shape (3,) be added to a 2D array with shape (2,3)?
Because broadcasting treats the missing dimension in the 1D array as 1, allowing it to stretch along that dimension to match the 2D array shape, as shown in execution_table step 1.
What happens if dimensions are not compatible during broadcasting?
An error occurs because arrays cannot be broadcasted. This is shown in the concept flow where 'No' leads to 'Raise error'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 1, what is the broadcasted shape of B?
A(3,)
B(1,3)
C(2,3)
D(2,1)
💡 Hint
Check the 'Broadcasted Shapes' column in execution_table step 1.
At which step does the broadcasting check confirm compatibility?
AStep 1
BStep 2
CNo step, error raised
DBefore step 1
💡 Hint
Look at the 'Broadcasting Check' column in execution_table.
If B had shape (2,1) instead of (3,), what would be the broadcasted shape when added to A (2,3)?
A(2,1)
B(2,3)
C(3,2)
DError
💡 Hint
Broadcasting allows dimensions of size 1 to stretch to match the other array's dimension.
Concept Snapshot
Common Broadcasting Patterns in numpy:
- Compare shapes from right to left.
- Dimensions are compatible if equal or one is 1.
- Missing dimensions treated as 1.
- Arrays stretch to match shapes.
- Enables element-wise operations on different shaped arrays.
Full Transcript
Broadcasting in numpy allows arrays of different shapes to be used together in operations. It compares their shapes from right to left. If dimensions are equal or one is 1, they are compatible. Missing dimensions are treated as 1. Arrays are virtually stretched to match shapes, enabling element-wise operations. For example, adding a 2x3 array to a 1D array of length 3 works because the 1D array is stretched along the missing dimension. If dimensions are not compatible, numpy raises an error. This process is key to writing efficient numpy code without manually reshaping arrays.

Practice

(1/5)
1. What does broadcasting in NumPy allow you to do?
easy
A. Perform operations on arrays of different shapes without explicit loops
B. Only add arrays of the exact same shape
C. Convert arrays into lists automatically
D. Sort arrays in ascending order

Solution

  1. Step 1: Understand broadcasting concept

    Broadcasting lets NumPy perform element-wise operations on arrays with different shapes by automatically expanding the smaller array.
  2. 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.
  3. Final Answer:

    Perform operations on arrays of different shapes without explicit loops -> Option A
  4. Quick Check:

    Broadcasting = operations on different shapes [OK]
Hint: Broadcasting means math on different shaped arrays without loops [OK]
Common Mistakes:
  • Thinking broadcasting requires arrays to have the same shape
  • Confusing broadcasting with sorting or type conversion
  • Assuming broadcasting changes array data types
2. Which of the following is the correct way to add a 1D array to each row of a 2D array using broadcasting?
easy
A. result = matrix + vector.T where matrix.shape = (3,4) and vector.shape = (4,)
B. result = matrix + vector where matrix.shape = (3,4) and vector.shape = (3,)
C. result = matrix + vector where matrix.shape = (3,4) and vector.shape = (4,)
D. result = matrix + vector.reshape(3,1) where matrix.shape = (3,4) and vector.shape = (4,)

Solution

  1. 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.
  2. Step 2: Evaluate options

    result = matrix + vector where matrix.shape = (3,4) and vector.shape = (4,) matches this pattern. result = matrix + vector where matrix.shape = (3,4) and vector.shape = (3,) tries to add a (3,) vector to (3,4) matrix which fails. result = matrix + vector.T where matrix.shape = (3,4) and vector.shape = (4,) uses transpose incorrectly. result = matrix + vector.reshape(3,1) where matrix.shape = (3,4) and vector.shape = (4,) reshapes vector wrongly.
  3. Final Answer:

    result = matrix + vector where matrix.shape = (3,4) and vector.shape = (4,) -> Option C
  4. Quick Check:

    Vector shape matches columns for row-wise addition [OK]
Hint: Add vector matching columns to matrix rows for broadcasting [OK]
Common Mistakes:
  • Using vector shape matching rows instead of columns
  • Trying to transpose 1D vector which has no effect
  • Reshaping vector incorrectly before addition
3. What is the output of the following code?
import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([10, 20, 30])
C = A + B
print(C)
medium
A. [[11 22 33] [14 25 36]]
B. [[11 22 33] [14 25 36] [10 20 30]]
C. [[10 20 30] [10 20 30]]
D. ValueError due to shape mismatch

Solution

  1. Step 1: Understand shapes of A and B

    A has shape (2,3), B has shape (3,). B will broadcast across rows of A.
  2. 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].
  3. Final Answer:

    [[11 22 33] [14 25 36]] -> Option A
  4. Quick Check:

    Broadcast adds vector to each matrix row [OK]
Hint: Add vector to each matrix row when vector shape matches columns [OK]
Common Mistakes:
  • Expecting an extra row in output
  • Confusing broadcasting with stacking arrays
  • Thinking shape mismatch causes error here
4. Identify the error in this code snippet:
import numpy as np
X = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array([10, 20, 30])
result = X + y
medium
A. y should be reshaped to (3,1) before addition
B. No error, code runs fine
C. X and y must have the same number of columns
D. Shapes (3,2) and (3,) cannot broadcast together

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    No error, code runs fine -> Option B
  4. Quick Check:

    Broadcasting allows (3,2) + (3,) by treating (3,) as (3,1) [OK]
Hint: Trailing dimensions must match or be 1 for broadcasting [OK]
Common Mistakes:
  • Assuming broadcasting works if first dims match
  • Not checking trailing dimension compatibility
  • Thinking reshaping y to (3,1) fixes addition here
5. Given a 2D array 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?
hard
A. M + v.T
B. M + v
C. M + v.reshape(1,4)
D. M + v.reshape(4,1)

Solution

  1. 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.
  2. Step 2: Check each option

    M + v.reshape(4,1) reshapes v to (4,1), allowing broadcasting with M (4,3). M + v tries to add (4,) to (4,3) which broadcasts incorrectly. M + v.reshape(1,4) reshapes v to (1,4), incompatible. M + v.T uses transpose on 1D array which has no effect.
  3. Final Answer:

    M + v.reshape(4,1) -> Option D
  4. Quick Check:

    Reshape vector to (rows,1) to add to columns [OK]
Hint: Reshape vector to (rows,1) to add to matrix columns [OK]
Common Mistakes:
  • Adding vector without reshaping when shapes differ
  • Using reshape(1, rows) instead of (rows,1)
  • Assuming transpose changes 1D array shape
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard