Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Broadcasting rules in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Broadcasting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of adding arrays with broadcasting
What is the output of this Python code using NumPy broadcasting?
Data Analysis Python
import numpy as np

arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([10, 20, 30])
result = arr1 + arr2
print(result)
A
[[11 22 33]
 [14 25 36]]
BRaises ValueError due to shape mismatch
C
[[10 20 30]
 [10 20 30]]
D
[[11 22 33]
 [14 25 36]
 [10 20 30]]
Attempts:
2 left
💡 Hint
Remember how NumPy broadcasts a 1D array to match the shape of a 2D array when adding.
❓ data_output
intermediate
2:00remaining
Shape after broadcasting in multiplication
Given these arrays, what is the shape of the result after multiplication with broadcasting?
Data Analysis Python
import numpy as np

arr1 = np.ones((3, 1, 4))
arr2 = np.ones((1, 5, 1))
result = arr1 * arr2
print(result.shape)
A(3, 1, 5, 4)
B(3, 5, 4)
C(1, 5, 4)
DRaises ValueError due to incompatible shapes
Attempts:
2 left
💡 Hint
Broadcasting aligns dimensions from the right and expands dimensions of size 1.
🔧 Debug
advanced
2:00remaining
Identify the broadcasting error
What error does this code raise and why?
Data Analysis Python
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([[1, 2], [3, 4]])
result = arr1 + arr2
print(result)
AOutput: [[2 4 6] [4 6 8]]
BTypeError: unsupported operand type(s) for +: 'int' and 'list'
CValueError: operands could not be broadcast together with shapes (3,) (2,2)
DOutput: [[2 4] [4 6] [4 6]]
Attempts:
2 left
💡 Hint
Check if the shapes of the arrays can be broadcasted according to the rules.
❓ visualization
advanced
2:00remaining
Visualize broadcasting with arrays
Which option correctly describes the broadcasting process when adding these arrays?
Data Analysis Python
import numpy as np

arr1 = np.array([[1], [2], [3]])  # shape (3,1)
arr2 = np.array([10, 20, 30])     # shape (3,)
result = arr1 + arr2
print(result)
Aarr2 is reshaped to (1,3) and broadcasted to (3,3), arr1 is broadcasted to (3,3), result shape is (3,3)
Barr1 is broadcasted to (3,3), arr2 is broadcasted to (3,3), result shape is (3,3)
CRaises ValueError due to incompatible shapes (3,1) and (3,)
Darr2 is broadcasted to (3,1), arr1 remains (3,1), result shape is (3,1)
Attempts:
2 left
💡 Hint
Remember that a 1D array of shape (3,) can be treated as (1,3) for broadcasting with (3,1).
🧠 Conceptual
expert
2:00remaining
Understanding broadcasting rules with multiple dimensions
Given arrays with shapes (4,1,3,1) and (1,5,1,7), what is the resulting shape after broadcasting?
A(4, 1, 3, 7)
B(1, 5, 3, 1)
CRaises ValueError due to incompatible shapes
D(4, 5, 3, 7)
Attempts:
2 left
💡 Hint
Broadcasting compares dimensions from right to left, expanding singleton dimensions or matching equal sizes.

Practice

(1/5)
1. What does broadcasting do when working with arrays of different shapes in Python's NumPy?
easy
A. It stretches smaller arrays to match the shape of larger arrays without copying data.
B. It copies smaller arrays to make them the same size as larger arrays.
C. It only works if arrays have exactly the same shape.
D. It deletes extra elements from larger arrays to match smaller ones.

Solution

  1. Step 1: Understand broadcasting concept

    Broadcasting allows operations on arrays with different shapes by stretching the smaller array.
  2. Step 2: Identify how broadcasting works

    It does not copy data but virtually stretches the smaller array to match the larger one.
  3. Final Answer:

    It stretches smaller arrays to match the shape of larger arrays without copying data. -> Option A
  4. Quick Check:

    Broadcasting = stretching without copying [OK]
Hint: Broadcasting stretches smaller arrays without copying data [OK]
Common Mistakes:
  • Thinking broadcasting copies data
  • Believing arrays must have same shape
  • Assuming larger arrays get shrunk
2. Which of the following code snippets correctly applies broadcasting to add a 1D array to each row of a 2D array in NumPy?
easy
A. result = arr2d + arr1d[:, np.newaxis]
B. result = arr2d.flatten() + arr1d
C. result = arr2d + arr1d.reshape(-1, 1)
D. result = arr2d + arr1d

Solution

  1. Step 1: Understand shapes of arrays

    arr2d is 2D (m,n), arr1d is 1D (n,). Adding arr1d directly to arr2d adds arr1d to each row by broadcasting.
  2. Step 2: Check reshaping effects

    Reshaping arr1d to column vector (A,C) causes shape incompatibility (m,n) + (n,1); flattening arr2d (D) leads to incompatible 1D shapes.
  3. Final Answer:

    result = arr2d + arr1d -> Option D
  4. Quick Check:

    Direct addition broadcasts 1D to rows [OK]
Hint: Add 1D array directly to 2D to broadcast rows [OK]
Common Mistakes:
  • Reshaping 1D array to column vector
  • Flattening the 2D array arr2d
  • Confusing row vs column broadcasting
3. What is the output of the following code?
import numpy as np
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([10, 20, 30])
result = arr1 + arr2
print(result)
medium
A. [[11 22 33] [14 25 36]]
B. [[11 22 33] [14 25 36] [10 20 30]]
C. [[11 12 13] [14 15 16]]
D. Error due to shape mismatch

Solution

  1. Step 1: Identify shapes of arrays

    arr1 shape is (2,3), arr2 shape is (3,). Broadcasting stretches arr2 to (2,3).
  2. Step 2: Perform element-wise addition

    Add each element of arr2 to corresponding column in arr1: [[1+10,2+20,3+30],[4+10,5+20,6+30]].
  3. Final Answer:

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

    Broadcasting adds arr2 to each row [OK]
Hint: Broadcast 1D array across rows for addition [OK]
Common Mistakes:
  • Expecting error due to shape difference
  • Confusing addition with multiplication
  • Misreading output shape
4. The following code throws a ValueError. What is the main reason?
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([1, 2, 3])
result = arr1 + arr2
print(result)
medium
A. arr1 should be 1D to add with arr2.
B. arr2 must be reshaped to (2,1) before addition.
C. Shapes (2,2) and (3,) are incompatible for broadcasting.
D. Addition operator '+' cannot be used with arrays.

Solution

  1. Step 1: Check shapes of arrays

    arr1 shape is (2,2), arr2 shape is (3,). Broadcasting requires trailing dimensions to be equal or 1.
  2. Step 2: Identify broadcasting rule violation

    Last dimensions 2 and 3 differ and neither is 1, so broadcasting fails causing ValueError.
  3. Final Answer:

    Shapes (2,2) and (3,) are incompatible for broadcasting. -> Option C
  4. Quick Check:

    Broadcasting needs compatible trailing dimensions [OK]
Hint: Check trailing dimensions for broadcasting compatibility [OK]
Common Mistakes:
  • Assuming any shapes can broadcast
  • Thinking reshaping arr2 fixes without correct shape
  • Believing '+' can't add arrays
5. You have a 3D array arr3d with shape (2, 3, 4) and a 1D array arr1d with shape (4,). You want to add arr1d to each 2D slice of arr3d along the last axis. Which code correctly uses broadcasting to do this?
hard
A. result = arr3d + arr1d.reshape(2, 3, 4)
B. result = arr3d + arr1d.reshape(1, 1, 4)
C. result = arr3d + arr1d.reshape(4, 1, 1)
D. result = arr3d + arr1d.reshape(1, 4, 1)

Solution

  1. Step 1: Understand shapes and broadcasting rules

    arr3d shape is (2,3,4), arr1d shape is (4,). To add along last axis, arr1d must broadcast to (1,1,4).
  2. Step 2: Reshape arr1d for correct broadcasting

    Reshape arr1d to (1,1,4) so it broadcasts across first two dimensions and aligns with last dimension.
  3. Step 3: Confirm addition

    Addition with arr3d + arr1d.reshape(1,1,4) works correctly by broadcasting arr1d along first two axes.
  4. Final Answer:

    result = arr3d + arr1d.reshape(1, 1, 4) -> Option B
  5. Quick Check:

    Reshape 1D to (1,1,last_dim) for 3D broadcasting [OK]
Hint: Reshape 1D to (1,1,last_dim) to add along last axis [OK]
Common Mistakes:
  • Not reshaping arr1d or wrong reshape shape
  • Confusing axis order in reshape
  • Assuming direct addition always works
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