Challenge - 5 Problems
Distance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Euclidean distance matrix
What is the output of the following code that computes Euclidean distances between two sets of points?
SciPy
import numpy as np from scipy.spatial import distance A = np.array([[0, 0], [1, 1]]) B = np.array([[1, 0], [2, 2]]) result = distance.cdist(A, B, 'euclidean') print(result)
Attempts:
2 left
💡 Hint
Remember Euclidean distance is the straight line distance between points.
✗ Incorrect
The Euclidean distance between points in A and B is calculated pairwise. For example, distance between [0,0] and [1,0] is 1, and between [0,0] and [2,2] is sqrt(2^2 + 2^2) = 2.828. Option A matches the correct matrix values.
❓ data_output
intermediate1:30remaining
Number of distances computed with cityblock metric
Given two arrays A and B, how many distances are computed by distance.cdist(A, B, 'cityblock')?
SciPy
import numpy as np from scipy.spatial import distance A = np.array([[1, 2], [3, 4], [5, 6]]) B = np.array([[7, 8], [9, 10]]) result = distance.cdist(A, B, 'cityblock') print(result.shape[0] * result.shape[1])
Attempts:
2 left
💡 Hint
The output shape is (number of rows in A, number of rows in B).
✗ Incorrect
distance.cdist computes distances between every pair of points from A and B. A has 3 points, B has 2 points, so total distances = 3 * 2 = 6.
🔧 Debug
advanced2:00remaining
Identify the error in distance.cdist usage
What error will this code raise and why?
SciPy
import numpy as np from scipy.spatial import distance A = np.array([[1, 2, 3], [4, 5, 6]]) B = np.array([[7, 8]]) result = distance.cdist(A, B, 'euclidean')
Attempts:
2 left
💡 Hint
Check the shape of A and B carefully.
✗ Incorrect
distance.cdist requires both input arrays to have the same number of features (columns). Here, A has 3 columns but B has only 2, causing a ValueError.
❓ visualization
advanced2:30remaining
Visualizing cosine distances between points
Which option correctly plots the cosine distance matrix heatmap between points in A and B?
SciPy
import numpy as np from scipy.spatial import distance import matplotlib.pyplot as plt A = np.array([[1, 0], [0, 1]]) B = np.array([[1, 1], [-1, 0]]) dist_matrix = distance.cdist(A, B, 'cosine') plt.imshow(dist_matrix, cmap='viridis') plt.colorbar() plt.show()
Attempts:
2 left
💡 Hint
Cosine distance values range between 0 and 1 and are best shown as a heatmap.
✗ Incorrect
The code computes cosine distances and visualizes them as a heatmap using imshow. This shows the pairwise cosine distances clearly.
🧠 Conceptual
expert2:00remaining
Understanding distance.cdist with custom metric
Which statement about using a custom metric function with distance.cdist is TRUE?
Attempts:
2 left
💡 Hint
Think about how distance.cdist computes pairwise distances.
✗ Incorrect
distance.cdist allows a custom metric function that takes two 1-D arrays (points) and returns a single scalar distance value.