Bird
Raised Fist0
SciPydata~20 mins

Distance metrics (euclidean, cosine, manhattan) in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Distance Metrics Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Calculate Euclidean distance between two points
What is the output of this code that calculates the Euclidean distance between points (1, 2) and (4, 6)?
SciPy
from scipy.spatial import distance
point1 = [1, 2]
point2 = [4, 6]
result = distance.euclidean(point1, point2)
print(round(result, 2))
A5.0
B7.0
C4.24
D3.61
Attempts:
2 left
💡 Hint
Euclidean distance is the straight line distance between two points in space.
❓ data_output
intermediate
2:00remaining
Cosine distance between two vectors
What is the cosine distance between vectors [1, 0, -1] and [-1, 0, 1] using scipy?
SciPy
from scipy.spatial import distance
vec1 = [1, 0, -1]
vec2 = [-1, 0, 1]
result = distance.cosine(vec1, vec2)
print(round(result, 2))
A0.0
B1.0
C2.0
D0.5
Attempts:
2 left
💡 Hint
Cosine distance is 1 minus the cosine similarity.
❓ Predict Output
advanced
2:00remaining
Manhattan distance calculation
What is the Manhattan distance between points (3, 5) and (1, 1)?
SciPy
from scipy.spatial import distance
p1 = [3, 5]
p2 = [1, 1]
result = distance.cityblock(p1, p2)
print(result)
A6
B8
C4
D10
Attempts:
2 left
💡 Hint
Manhattan distance sums the absolute differences of coordinates.
❓ visualization
advanced
3:00remaining
Visualize Euclidean vs Manhattan distance
Which option correctly plots the Euclidean and Manhattan distances between points (0,0) and (3,4) on a 2D grid?
SciPy
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
ax.plot([0,3], [0,4], label='Euclidean')
ax.plot([0,3,3], [0,0,4], label='Manhattan')
ax.scatter([0,3], [0,4], color='red')
ax.legend()
plt.show()
AA circle centered at (0,0) with radius 5
BA straight line from (0,0) to (3,0) and a diagonal from (3,0) to (3,4)
CTwo diagonal lines crossing at (3,4)
DA line from (0,0) to (3,4) and an L-shaped path from (0,0) to (3,0) to (3,4)
Attempts:
2 left
💡 Hint
Manhattan distance looks like walking along grid streets, Euclidean is straight line.
🧠 Conceptual
expert
2:30remaining
Understanding cosine distance range
Which statement about cosine distance computed by scipy.spatial.distance.cosine is correct?
ACosine distance ranges from -1 to 1, where -1 means opposite and 1 means identical vectors
BCosine distance ranges from 0 to 2, where 0 means identical and 2 means opposite vectors
CCosine distance is always between 0 and 1, with 1 meaning identical vectors
DCosine distance is the same as Euclidean distance normalized between 0 and 1
Attempts:
2 left
💡 Hint
Recall cosine distance = 1 - cosine similarity.

Practice

(1/5)
1. Which distance metric measures the straight-line distance between two points in space?
easy
A. Jaccard distance
B. Cosine distance
C. Manhattan distance
D. Euclidean distance

Solution

  1. Step 1: Understand distance types

    Euclidean distance is the direct straight-line distance between two points.
  2. Step 2: Compare with other distances

    Cosine measures angle difference, Manhattan sums absolute differences, Jaccard measures set similarity.
  3. Final Answer:

    Euclidean distance -> Option D
  4. Quick Check:

    Straight-line distance = Euclidean distance [OK]
Hint: Straight line means Euclidean distance [OK]
Common Mistakes:
  • Confusing cosine with Euclidean
  • Thinking Manhattan is straight-line
  • Mixing Jaccard with numeric distances
2. Which of the following is the correct way to import the Euclidean distance function from scipy?
easy
A. from scipy.spatial.distance import euclidean
B. import scipy.distance.euclidean
C. from scipy.distance import euclidean
D. import euclidean from scipy.spatial

Solution

  1. Step 1: Recall scipy import syntax

    The correct import path for distance functions is scipy.spatial.distance.
  2. Step 2: Check each option

    from scipy.spatial.distance import euclidean uses correct syntax: from scipy.spatial.distance import euclidean. Others are invalid or incorrect syntax.
  3. Final Answer:

    from scipy.spatial.distance import euclidean -> Option A
  4. Quick Check:

    Correct import path = from scipy.spatial.distance import euclidean [OK]
Hint: Use 'from scipy.spatial.distance import' for distance functions [OK]
Common Mistakes:
  • Using wrong module path
  • Incorrect import syntax
  • Trying to import from scipy.distance
3. What is the output of this code?
from scipy.spatial.distance import cosine
v1 = [1, 0, 1]
v2 = [0, 1, 0]
result = cosine(v1, v2)
print(round(result, 2))
medium
A. 1.0
B. 0.5
C. 0.0
D. 0.71

Solution

  1. Step 1: Calculate cosine similarity basics

    Vectors v1 and v2 are orthogonal (no overlap), so cosine similarity is 0.
  2. Step 2: Calculate cosine distance

    Cosine distance = 1 - cosine similarity = 1 - 0 = 1.0.
  3. Final Answer:

    1.0 -> Option A
  4. Quick Check:

    Orthogonal vectors cosine distance = 1.0 [OK]
Hint: Cosine distance = 1 - cosine similarity [OK]
Common Mistakes:
  • Confusing cosine similarity with distance
  • Rounding errors
  • Assuming zero distance for orthogonal vectors
4. Find the error in this code snippet calculating Manhattan distance:
from scipy.spatial.distance import manhattan
a = [1, 2, 3]
b = [4, 5]
print(manhattan(a, b))
medium
A. Function name should be cityblock, not manhattan
B. Import statement is incorrect
C. Lists a and b must be the same length
D. Manhattan distance requires numpy arrays, not lists

Solution

  1. Step 1: Check input vector lengths

    Vectors a and b have different lengths (3 and 2), which is invalid for distance calculation.
  2. Step 2: Validate function and import

    Import and function name are correct; scipy.spatial.distance.manhattan is valid and accepts lists.
  3. Final Answer:

    Lists a and b must be the same length -> Option C
  4. Quick Check:

    Equal length vectors needed for distance [OK]
Hint: Vectors must be same length for distance functions [OK]
Common Mistakes:
  • Assuming different length lists work
  • Confusing function name with cityblock
  • Thinking lists are invalid inputs
5. You have two points: p1 = [3, 4, 5] and p2 = [1, 1, 1]. Which distance metric will give the largest value?
Options:
A) Euclidean
B) Cosine
C) Manhattan
D) All equal
hard
A. Euclidean
B. Manhattan
C. Cosine
D. All equal

Solution

  1. Step 1: Calculate Euclidean distance

    Euclidean = sqrt((3-1)^2 + (4-1)^2 + (5-1)^2) = sqrt(4 + 9 + 16) = sqrt(29) ≈ 5.39.
  2. Step 2: Calculate Manhattan distance

    Manhattan = |3-1| + |4-1| + |5-1| = 2 + 3 + 4 = 9.
  3. Step 3: Calculate Cosine distance

    Cosine similarity = (3*1 + 4*1 + 5*1) / (sqrt(3^2+4^2+5^2)*sqrt(1^2+1^2+1^2)) = 12 / (7.07*1.73) ≈ 0.98; Cosine distance = 1 - 0.98 = 0.02.
  4. Final Answer:

    Manhattan -> Option B
  5. Quick Check:

    Manhattan distance > Euclidean > Cosine distance [OK]
Hint: Manhattan sums absolute differences, usually largest [OK]
Common Mistakes:
  • Assuming Euclidean is always largest
  • Confusing cosine distance scale
  • Ignoring absolute values in Manhattan
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard