Challenge - 5 Problems
Broadcasting Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of broadcasting distance calculation
What is the output of this code that calculates Euclidean distances between points using broadcasting?
NumPy
import numpy as np points = np.array([[1, 2], [4, 6], [7, 8]]) diffs = points[:, np.newaxis, :] - points[np.newaxis, :, :] distances = np.sqrt(np.sum(diffs ** 2, axis=2)) print(np.round(distances, 2))
Attempts:
2 left
💡 Hint
Remember Euclidean distance is sqrt((x2-x1)^2 + (y2-y1)^2). Broadcasting helps subtract all pairs at once.
✗ Incorrect
The code uses broadcasting to subtract each point from every other point, then sums squared differences and takes square roots. The resulting matrix shows distances between points. Option A matches the correct calculated distances.
❓ data_output
intermediate1:30remaining
Shape of broadcasted arrays for distance matrix
Given points array shape (4, 3), what is the shape of the array after broadcasting for pairwise distance calculation using diffs = points[:, np.newaxis, :] - points[np.newaxis, :, :]?
NumPy
import numpy as np points = np.random.rand(4, 3) diffs = points[:, np.newaxis, :] - points[np.newaxis, :, :] print(diffs.shape)
Attempts:
2 left
💡 Hint
Broadcasting adds a new axis to compare each point with every other point.
✗ Incorrect
points[:, np.newaxis, :] adds a new axis at position 1, making shape (4,1,3). points[np.newaxis, :, :] adds a new axis at position 0, shape (1,4,3). Subtracting broadcasts to (4,4,3).
🔧 Debug
advanced2:00remaining
Identify the error in broadcasting distance code
What error will this code raise when calculating pairwise distances using broadcasting?
NumPy
import numpy as np points = np.array([[1, 2], [3, 4]]) diffs = points[:, :, np.newaxis] - points[np.newaxis, :, :] distances = np.sqrt(np.sum(diffs ** 2, axis=2)) print(distances)
Attempts:
2 left
💡 Hint
Check the shapes of arrays before subtraction.
✗ Incorrect
points[:, :, np.newaxis] changes shape to (2, 2, 1) but points[np.newaxis, :, :] is (1, 2, 2). These shapes cannot broadcast together for subtraction, causing ValueError.
🚀 Application
advanced2:00remaining
Calculate Manhattan distance matrix using broadcasting
Which code snippet correctly calculates the Manhattan (L1) distance matrix between points using broadcasting?
Attempts:
2 left
💡 Hint
Manhattan distance sums absolute differences along each dimension.
✗ Incorrect
Option D correctly broadcasts to get pairwise differences, takes absolute values, and sums along the last axis to get Manhattan distances. Other options have wrong axes or operations.
🧠 Conceptual
expert1:30remaining
Why broadcasting is efficient for distance matrices
Why is broadcasting preferred over explicit loops when computing distance matrices for large datasets?
Attempts:
2 left
💡 Hint
Think about how numpy handles operations internally compared to Python loops.
✗ Incorrect
Broadcasting leverages numpy's optimized C code to perform operations on whole arrays at once, which is much faster and more memory efficient than looping in Python. It does not automatically parallelize or reduce dimensionality.