0
0
SciPydata~10 mins

Distance matrix computation in SciPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the function that computes pairwise distances.

SciPy
from scipy.spatial.distance import [1]
Drag options to blanks, or click blank then click option'
Aeuclidean
Bcdist
Cpdist
Dsquareform
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing cdist which is for distances between two different sets.
Choosing squareform which converts distance vectors to matrices.
2fill in blank
medium

Complete the code to compute the pairwise Euclidean distances of points in array X.

SciPy
distances = [1](X, metric='euclidean')
Drag options to blanks, or click blank then click option'
Apdist
Bcdist
Csquareform
Deuclidean
Attempts:
3 left
💡 Hint
Common Mistakes
Using cdist with only one array causes an error.
Using squareform directly on the data instead of on distances.
3fill in blank
hard

Fix the error in the code to convert the condensed distance vector d to a square matrix.

SciPy
distance_matrix = [1](d)
Drag options to blanks, or click blank then click option'
Asquareform
Bcdist
Ceuclidean
Dpdist
Attempts:
3 left
💡 Hint
Common Mistakes
Using pdist or cdist which expect raw data, not condensed distances.
Using euclidean which is a metric, not a function to convert formats.
4fill in blank
hard

Fill both blanks to compute the distance matrix between two different sets X and Y using the cosine metric.

SciPy
dist_matrix = [1](X, [2], metric='cosine')
Drag options to blanks, or click blank then click option'
Acdist
Bpdist
CY
DX
Attempts:
3 left
💡 Hint
Common Mistakes
Using pdist which only takes one array.
Passing X as the second argument instead of Y.
5fill in blank
hard

Fill all three blanks to create a dictionary mapping each point's index to its Euclidean distance from the origin (0,0).

SciPy
origin = [0, 0]
distances = {i: [1](point, [2]) for i, point in enumerate(X)}
sorted_distances = sorted(distances.items(), key=lambda x: x[[3]])
Drag options to blanks, or click blank then click option'
Acdist
Borigin
C1
Deuclidean
Attempts:
3 left
💡 Hint
Common Mistakes
Using cdist which requires arrays, not single points.
Sorting by x[0] which sorts by index, not distance.