0
0
SciPydata~20 mins

Distance matrix computation in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Distance Matrix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
[[ 0.  7. 14.]
 [ 7.  0.  7.]
 [14.  7.  0.]]
B
[[0 5 10]
 [5 0 5]
 [10 5 0]]
C
[[ 0.  5. 10.]
 [ 5.  0.  5.]
 [10.  5.  0.]]
D
[[ 0.  3.  6.]
 [ 3.  0.  4.]
 [ 6.  4.  0.]]
Attempts:
2 left
💡 Hint
Recall that Euclidean distance between (x1,y1) and (x2,y2) is sqrt((x2-x1)^2 + (y2-y1)^2).
data_output
intermediate
1: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)
A16
B8
C4
D12
Attempts:
2 left
💡 Hint
Distance matrix between n points is an n x n matrix.
🔧 Debug
advanced
2: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)
ANo error, prints the correct distance matrix
BTypeError: unsupported operand type(s) for -: 'list' and 'list'
CValueError: setting an array element with a sequence
DNameError: name 'distance_matrix' is not defined
Attempts:
2 left
💡 Hint
Check if input is accepted as list of lists or needs to be numpy array.
🧠 Conceptual
advanced
1: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?
AEuclidean distance
BManhattan (cityblock) distance
CHamming distance
DCosine distance
Attempts:
2 left
💡 Hint
Check scipy.spatial.distance_matrix documentation for supported metrics.
🚀 Application
expert
2: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)
A(2, 3)
B(0, 2)
C(0, 1)
D(1, 3)
Attempts:
2 left
💡 Hint
Look for the smallest non-zero distance after ignoring diagonal.