0
0
SciPydata~10 mins

Distance metrics (euclidean, cosine, manhattan) 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 euclidean distance function from scipy.

SciPy
from scipy.spatial.distance import [1]
Drag options to blanks, or click blank then click option'
Amanhattan
Bcosine
Ceuclidean
Dhamming
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing cosine or manhattan which are different distance metrics.
Trying to import a function not available in scipy.spatial.distance.
2fill in blank
medium

Complete the code to calculate the cosine distance between vectors a and b.

SciPy
distance = [1](a, b)
Drag options to blanks, or click blank then click option'
Acosine
Bmanhattan
Ceuclidean
Dhamming
Attempts:
3 left
💡 Hint
Common Mistakes
Using euclidean distance which measures straight-line distance, not angle.
Using manhattan distance which sums absolute differences.
3fill in blank
hard

Fix the error in the code to correctly calculate Manhattan distance between x and y.

SciPy
dist = [1](x, y)
Drag options to blanks, or click blank then click option'
Acosine
Bmanhattan
Cminkowski
Deuclidean
Attempts:
3 left
💡 Hint
Common Mistakes
Using euclidean distance which calculates straight-line distance.
Using minkowski without specifying the order parameter.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each vector to its Euclidean distance from a reference vector ref_vec.

SciPy
distances = {vec: [1](vec, ref_vec) for vec in vectors if [2](vec, ref_vec) < 5}
Drag options to blanks, or click blank then click option'
Aeuclidean
Bcosine
Cmanhattan
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using different distance functions in the two blanks.
Using len() which returns length, not distance.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each vector to its Manhattan distance from ref_vec, but only include vectors with cosine distance less than 0.5.

SciPy
distances = {vec: [1](vec, ref_vec) for vec in vectors if [2](vec, ref_vec) [3] 0.5}
Drag options to blanks, or click blank then click option'
Aeuclidean
Bmanhattan
Ccosine
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up distance functions in the blanks.
Using wrong comparison operator like > instead of <.