Challenge - 5 Problems
Distance Matrix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Euclidean distance matrix computation
What is the output of this code that computes the Euclidean distance matrix between points?
SciPy
import numpy as np from scipy.spatial import distance_matrix points = np.array([[0, 0], [3, 4], [6, 8]]) dist_mat = distance_matrix(points, points) print(dist_mat)
Attempts:
2 left
💡 Hint
Recall that Euclidean distance between (x1,y1) and (x2,y2) is sqrt((x2-x1)^2 + (y2-y1)^2).
✗ Incorrect
The distances between points (0,0), (3,4), and (6,8) are 5, 10, and 5 respectively, forming the symmetric matrix shown in option C.
❓ data_output
intermediate1:30remaining
Number of elements in Euclidean distance matrix
Given 4 points in 2D space, how many elements are in the Euclidean distance matrix computed by scipy.spatial.distance_matrix?
SciPy
import numpy as np from scipy.spatial import distance_matrix points = np.array([[1,2], [3,4], [5,6], [7,8]]) dist_mat = distance_matrix(points, points) print(dist_mat.size)
Attempts:
2 left
💡 Hint
Distance matrix between n points is an n x n matrix.
✗ Incorrect
With 4 points, the distance matrix is 4 rows by 4 columns, so total elements = 4*4 = 16.
🔧 Debug
advanced2:00remaining
Identify the error in distance matrix computation code
What error does this code raise when computing a distance matrix?
SciPy
import numpy as np from scipy.spatial import distance_matrix points = [[0, 0], [1, 1], [2, 2]] dist_mat = distance_matrix(points, points) print(dist_mat)
Attempts:
2 left
💡 Hint
Check if input is accepted as list of lists or needs to be numpy array.
✗ Incorrect
scipy.spatial.distance_matrix accepts list of lists as input and converts internally, so no error occurs.
🧠 Conceptual
advanced1:30remaining
Which distance metric is NOT supported by scipy.spatial.distance_matrix?
Which of these distance metrics cannot be directly computed using scipy.spatial.distance_matrix function?
Attempts:
2 left
💡 Hint
Check scipy.spatial.distance_matrix documentation for supported metrics.
✗ Incorrect
scipy.spatial.distance_matrix only computes Euclidean distance. Other metrics like Manhattan, Cosine, or Hamming require scipy.spatial.distance.pdist or cdist with metric parameter.
🚀 Application
expert2:30remaining
Find the closest pair of points using distance matrix
Given this code, which option correctly identifies the indices of the closest pair of points?
SciPy
import numpy as np from scipy.spatial import distance_matrix points = np.array([[1, 2], [4, 6], [1, 3], [7, 8]]) dist_mat = distance_matrix(points, points) np.fill_diagonal(dist_mat, np.inf) min_index = np.unravel_index(np.argmin(dist_mat), dist_mat.shape) print(min_index)
Attempts:
2 left
💡 Hint
Look for the smallest non-zero distance after ignoring diagonal.
✗ Incorrect
Points at indices 0 and 2 are closest with distance 1, so min_index is (0, 2).