0
0
Data Analysis Pythondata~20 mins

Broadcasting rules in Data Analysis Python - 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 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.