Distance metrics help us measure how far apart two points or objects are. They are useful to compare things in many areas like shopping, sports, or health.
Distance metrics (euclidean, cosine, manhattan) in SciPy
from scipy.spatial import distance # Euclidean distance distance.euclidean(point1, point2) # Cosine distance distance.cosine(point1, point2) # Manhattan distance distance.cityblock(point1, point2)
point1 and point2 are lists or arrays of numbers representing coordinates or features.
Euclidean is straight-line distance, Cosine measures angle difference, Manhattan sums absolute differences.
distance.euclidean([1, 2], [4, 6])
distance.cosine([1, 0, 0], [0, 1, 0])
distance.cityblock([1, 2, 3], [4, 0, 6])
This program calculates three types of distances between two points and prints them with two decimals for clarity.
from scipy.spatial import distance pointA = [3, 4] pointB = [7, 1] # Calculate Euclidean distance euclid = distance.euclidean(pointA, pointB) # Calculate Cosine distance cosine = distance.cosine(pointA, pointB) # Calculate Manhattan distance manhattan = distance.cityblock(pointA, pointB) print(f"Euclidean distance: {euclid:.2f}") print(f"Cosine distance: {cosine:.2f}") print(f"Manhattan distance: {manhattan}")
Euclidean distance is like measuring with a ruler in straight line.
Cosine distance is good when you care about direction, not size.
Manhattan distance is like walking city blocks, adding up all steps.
Distance metrics measure how far or different two points are.
Euclidean is straight-line, Cosine is angle-based, Manhattan sums absolute differences.
Use scipy.spatial.distance functions to calculate these easily in Python.
Practice
Solution
Step 1: Understand distance types
Euclidean distance is the direct straight-line distance between two points.Step 2: Compare with other distances
Cosine measures angle difference, Manhattan sums absolute differences, Jaccard measures set similarity.Final Answer:
Euclidean distance -> Option DQuick Check:
Straight-line distance = Euclidean distance [OK]
- Confusing cosine with Euclidean
- Thinking Manhattan is straight-line
- Mixing Jaccard with numeric distances
Solution
Step 1: Recall scipy import syntax
The correct import path for distance functions is scipy.spatial.distance.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.Final Answer:
from scipy.spatial.distance import euclidean -> Option AQuick Check:
Correct import path = from scipy.spatial.distance import euclidean [OK]
- Using wrong module path
- Incorrect import syntax
- Trying to import from scipy.distance
from scipy.spatial.distance import cosine v1 = [1, 0, 1] v2 = [0, 1, 0] result = cosine(v1, v2) print(round(result, 2))
Solution
Step 1: Calculate cosine similarity basics
Vectors v1 and v2 are orthogonal (no overlap), so cosine similarity is 0.Step 2: Calculate cosine distance
Cosine distance = 1 - cosine similarity = 1 - 0 = 1.0.Final Answer:
1.0 -> Option AQuick Check:
Orthogonal vectors cosine distance = 1.0 [OK]
- Confusing cosine similarity with distance
- Rounding errors
- Assuming zero distance for orthogonal vectors
from scipy.spatial.distance import manhattan a = [1, 2, 3] b = [4, 5] print(manhattan(a, b))
Solution
Step 1: Check input vector lengths
Vectors a and b have different lengths (3 and 2), which is invalid for distance calculation.Step 2: Validate function and import
Import and function name are correct; scipy.spatial.distance.manhattan is valid and accepts lists.Final Answer:
Lists a and b must be the same length -> Option CQuick Check:
Equal length vectors needed for distance [OK]
- Assuming different length lists work
- Confusing function name with cityblock
- Thinking lists are invalid inputs
Options:
A) Euclidean
B) Cosine
C) Manhattan
D) All equal
Solution
Step 1: Calculate Euclidean distance
Euclidean = sqrt((3-1)^2 + (4-1)^2 + (5-1)^2) = sqrt(4 + 9 + 16) = sqrt(29) ≈ 5.39.Step 2: Calculate Manhattan distance
Manhattan = |3-1| + |4-1| + |5-1| = 2 + 3 + 4 = 9.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.Final Answer:
Manhattan -> Option BQuick Check:
Manhattan distance > Euclidean > Cosine distance [OK]
- Assuming Euclidean is always largest
- Confusing cosine distance scale
- Ignoring absolute values in Manhattan
Ready to prove it?
Pass the test and this pattern moves unknown
