0
0
NumPydata~20 mins

Broadcasting with higher dimensions in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 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 broadcasting addition with 3D and 2D arrays
What is the output of the following code snippet?
NumPy
import numpy as np

A = np.ones((2,3,4))
B = np.arange(4)
result = A + B
print(result)
A
[[[1. 2. 3. 4.]
  [2. 3. 4. 5.]
  [3. 4. 5. 6.]]

 [[1. 2. 3. 4.]
  [2. 3. 4. 5.]
  [3. 4. 5. 6.]]]
B
[[[1. 1. 1. 1.]
  [2. 3. 4. 5.]
  [3. 4. 5. 6.]]

 [[1. 1. 1. 1.]
  [2. 3. 4. 5.]
  [3. 4. 5. 6.]]]
C
[[[1. 2. 3. 4.]
  [1. 2. 3. 4.]
  [1. 2. 3. 4.]]

 [[2. 3. 4. 5.]
  [2. 3. 4. 5.]
  [2. 3. 4. 5.]]]
D
[[[1. 2. 3. 4.]
  [1. 2. 3. 4.]
  [1. 2. 3. 4.]]

 [[1. 2. 3. 4.]
  [1. 2. 3. 4.]
  [1. 2. 3. 4.]]]
Attempts:
2 left
💡 Hint
Remember how numpy broadcasts arrays with different shapes by matching dimensions from the right.
data_output
intermediate
1:30remaining
Shape after broadcasting multiplication
Given the arrays below, what is the shape of the result after multiplication?
NumPy
import numpy as np

X = np.ones((4,1,3))
Y = np.arange(3)
result = X * Y
print(result.shape)
A(4, 1, 3)
B(4, 3, 3)
C(4, 3)
D(3, 4, 3)
Attempts:
2 left
💡 Hint
Check how numpy aligns dimensions from the right and broadcasts accordingly.
🔧 Debug
advanced
1:30remaining
Identify the error in broadcasting with incompatible shapes
What error will the following code raise?
NumPy
import numpy as np

A = np.ones((2,3,4))
B = np.arange(5)
result = A + B
AIndexError: index 4 is out of bounds for axis 0 with size 4
BTypeError: unsupported operand type(s) for +: 'int' and 'numpy.ndarray'
CValueError: operands could not be broadcast together with shapes (2,3,4) (5,)
DNo error, outputs a (2,3,5) array
Attempts:
2 left
💡 Hint
Check if the shapes can be broadcasted according to numpy rules.
🚀 Application
advanced
2:30remaining
Using broadcasting to normalize a 3D array
You have a 3D array representing 2 samples, each with 3 features and 4 observations. You want to subtract the mean of each feature across observations from the array. Which code correctly does this using broadcasting?
NumPy
import numpy as np

arr = np.array([[[1,2,3,4],[5,6,7,8],[9,10,11,12]],
                [[13,14,15,16],[17,18,19,20],[21,22,23,24]]])

# Compute mean and subtract
A
mean = arr.mean(axis=2, keepdims=True)
result = arr - mean
B
mean = arr.mean(axis=1, keepdims=True)
result = arr - mean
C
mean = arr.mean(axis=0, keepdims=True)
result = arr - mean
D
mean = arr.mean(axis=2)
result = arr - mean
Attempts:
2 left
💡 Hint
Think about which axis corresponds to observations and how to keep dimensions for broadcasting.
🧠 Conceptual
expert
2:00remaining
Understanding broadcasting rules with higher dimensions
Given arrays A with shape (3,1,5,1) and B with shape (1,4,1,6), what is the shape of the result when computing A + B?
A(3, 4, 1, 6)
B(3, 4, 5, 6)
C(3, 1, 5, 6)
DBroadcasting error due to incompatible shapes
Attempts:
2 left
💡 Hint
Align shapes from the right and apply broadcasting rules dimension-wise.