0
0
NumPydata~20 mins

Broadcasting for distance matrices in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Broadcasting Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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))
A
[[0.   5.   8.49]
 [5.   0.   3.61]
 [8.49 3.61 0.  ]]
B
[[0.   3.61 5.  ]
 [3.61 0.   8.49]
 [5.   8.49 0.  ]]
C
[[0.   4.24 7.07]
 [4.24 0.   5.  ]
 [7.07 5.   0.  ]]
D
[[0.   6.4  9.2 ]
 [6.4  0.   4.5 ]
 [9.2  4.5  0.  ]]
Attempts:
2 left
💡 Hint
Remember Euclidean distance is sqrt((x2-x1)^2 + (y2-y1)^2). Broadcasting helps subtract all pairs at once.
data_output
intermediate
1: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)
A(4, 4, 3)
B(3, 3, 4)
C(3, 4, 4)
D(4, 3, 4)
Attempts:
2 left
💡 Hint
Broadcasting adds a new axis to compare each point with every other point.
🔧 Debug
advanced
2: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)
AIndexError: too many indices for array
BNo error, prints distance matrix
CValueError: operands could not be broadcast together
DTypeError: unsupported operand type(s) for -: 'int' and 'int'
Attempts:
2 left
💡 Hint
Check the shapes of arrays before subtraction.
🚀 Application
advanced
2:00remaining
Calculate Manhattan distance matrix using broadcasting
Which code snippet correctly calculates the Manhattan (L1) distance matrix between points using broadcasting?
A
diffs = points[:, np.newaxis, :] - points[np.newaxis, :, :]
distances = np.sqrt(np.sum(diffs ** 2, axis=1))
B
diffs = points[:, :, np.newaxis] - points[np.newaxis, :, :]
distances = np.sum(np.abs(diffs), axis=1)
C
diffs = points[:, np.newaxis, :] + points[np.newaxis, :, :]
distances = np.sum(np.abs(diffs), axis=2)
D
diffs = points[:, np.newaxis, :] - points[np.newaxis, :, :]
distances = np.sum(np.abs(diffs), axis=2)
Attempts:
2 left
💡 Hint
Manhattan distance sums absolute differences along each dimension.
🧠 Conceptual
expert
1:30remaining
Why broadcasting is efficient for distance matrices
Why is broadcasting preferred over explicit loops when computing distance matrices for large datasets?
ABroadcasting automatically parallelizes computations across multiple CPUs without extra code.
BBroadcasting uses vectorized operations that run faster and use less memory than explicit Python loops.
CBroadcasting reduces the dimensionality of data, making calculations simpler.
DBroadcasting avoids the need to import external libraries like numpy.
Attempts:
2 left
💡 Hint
Think about how numpy handles operations internally compared to Python loops.