0
0
SciPydata~10 mins

Distance computation (distance.cdist) 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 distances between two sets of points.

SciPy
from scipy.spatial.distance import [1]

points1 = [[0, 0], [1, 1]]
points2 = [[1, 0], [2, 2]]
distances = [1](points1, points2)
print(distances)
Drag options to blanks, or click blank then click option'
Apdist
Bcdist
Ceuclidean
Dsquareform
Attempts:
3 left
💡 Hint
Common Mistakes
Using pdist instead of cdist
Importing euclidean directly instead of cdist
Not importing the function at all
2fill in blank
medium

Complete the code to compute Euclidean distances between two sets of points using cdist.

SciPy
from scipy.spatial.distance import cdist

points1 = [[0, 0], [1, 1]]
points2 = [[1, 0], [2, 2]]
distances = cdist(points1, points2, metric=[1])
print(distances)
Drag options to blanks, or click blank then click option'
A'cosine'
B'hamming'
C'euclidean'
D'cityblock'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cityblock' which computes Manhattan distance
Using 'cosine' which computes cosine similarity
Using metric without quotes
3fill in blank
hard

Fix the error in the code to correctly compute Manhattan distances between two sets of points.

SciPy
from scipy.spatial.distance import cdist

points1 = [[0, 0], [1, 1]]
points2 = [[1, 0], [2, 2]]
distances = cdist(points1, points2, metric=[1])
print(distances)
Drag options to blanks, or click blank then click option'
Amanhattan
B'manhattan'
Ccityblock
D'cityblock'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing metric without quotes
Using 'manhattan' which is not recognized
Using cityblock without quotes
4fill in blank
hard

Fill both blanks to create a dictionary of distances from points1 to points2 using cosine metric and convert the result to a list.

SciPy
from scipy.spatial.distance import cdist

points1 = [[1, 0], [0, 1]]
points2 = [[1, 1], [0, 0]]
dist_matrix = cdist(points1, points2, metric=[1])
dist_list = dist_matrix.[2]()
print(dist_list)
Drag options to blanks, or click blank then click option'
A'cosine'
Btolist
Cflatten
D'euclidean'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'euclidean' instead of 'cosine'
Using flatten() which returns a flattened array, not a list
Not calling tolist() as a method
5fill in blank
hard

Fill all three blanks to compute Chebyshev distances between two sets of points and print the maximum distance value.

SciPy
from scipy.spatial.distance import cdist

points1 = [[1, 2], [3, 4]]
points2 = [[5, 6], [7, 8]]
dist_matrix = cdist(points1, points2, metric=[1])
max_dist = dist_matrix.[2]()
print(max_dist[3])
Drag options to blanks, or click blank then click option'
A'chebyshev'
Bmax
C()
Dmax()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'chebyshev' without quotes
Using max() as an attribute without parentheses
Using max() twice or incorrectly