0
0
SciPydata~20 mins

Distance computation (distance.cdist) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Distance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
[[1.41421356 2.23606798]
 [1.         1.41421356]]
B
[[1.         2.23606798]
 [1.41421356 1.        ]]
C
[[1.         2.23606798]
 [1.         1.41421356]]
D
[[1.         2.82842712]
 [1.         1.41421356]]
Attempts:
2 left
💡 Hint
Remember Euclidean distance is the straight line distance between points.
data_output
intermediate
1: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])
A3
B5
C2
D6
Attempts:
2 left
💡 Hint
The output shape is (number of rows in A, number of rows in B).
🔧 Debug
advanced
2: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')
ANo error, runs successfully
BValueError: A and B must have the same number of columns
CIndexError: index out of bounds
DTypeError: Unsupported metric 'euclidean'
Attempts:
2 left
💡 Hint
Check the shape of A and B carefully.
visualization
advanced
2: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()
AA heatmap with values close to 0 and 1, showing cosine distances between points.
BA bar chart of distances between points.
CA line plot of distances over index.
DA scatter plot of points A and B.
Attempts:
2 left
💡 Hint
Cosine distance values range between 0 and 1 and are best shown as a heatmap.
🧠 Conceptual
expert
2:00remaining
Understanding distance.cdist with custom metric
Which statement about using a custom metric function with distance.cdist is TRUE?
Adistance.cdist does not support custom metric functions.
BThe custom metric function can accept arrays of any shape and return an array of distances.
CThe custom metric function must accept two 1-D arrays and return a scalar distance.
DThe custom metric function must return a boolean indicating if points are equal.
Attempts:
2 left
💡 Hint
Think about how distance.cdist computes pairwise distances.