0
0
SciPydata~10 mins

Distance metrics (euclidean, cosine, manhattan) in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Distance metrics (euclidean, cosine, manhattan)
Start with two points
Choose distance metric
Euclidean: sqrt(sum((x_i - y_i)^2))
Cosine: 1 - (dot(x,y) / (||x||*||y||))
Manhattan: sum(|x_i - y_i|)
Calculate distance
Output distance value
We start with two points, pick a distance type, calculate it, then get the distance value.
Execution Sample
SciPy
from scipy.spatial import distance

x = [1, 2, 3]
y = [4, 5, 6]

print(distance.euclidean(x, y))
print(distance.cosine(x, y))
print(distance.cityblock(x, y))
Calculate Euclidean, Cosine, and Manhattan distances between two 3D points.
Execution Table
StepMetricCalculationIntermediate ValuesResult
1Euclideansqrt((1-4)^2 + (2-5)^2 + (3-6)^2)Differences squared: 9, 9, 95.196152422706632
2Cosine1 - (dot(x,y) / (||x|| * ||y||))dot=32, ||x||=3.7417, ||y||=8.77490.025368153802923787
3Manhattansum(|1-4| + |2-5| + |3-6|)Absolute differences: 3, 3, 39
4EndAll distances calculated--
💡 All three distances computed for given points, execution ends.
Variable Tracker
VariableStartAfter EuclideanAfter CosineAfter ManhattanFinal
x[1, 2, 3][1, 2, 3][1, 2, 3][1, 2, 3][1, 2, 3]
y[4, 5, 6][4, 5, 6][4, 5, 6][4, 5, 6][4, 5, 6]
euclidean_distanceNone5.1961524227066325.1961524227066325.1961524227066325.196152422706632
cosine_distanceNoneNone0.0253681538029237870.0253681538029237870.025368153802923787
manhattan_distanceNoneNoneNone99
Key Moments - 3 Insights
Why is cosine distance close to zero even though points look far apart?
Cosine distance measures angle difference, not length. See execution_table row 2: dot product and norms show vectors point similarly, so cosine distance is small.
Why do Euclidean and Manhattan distances give different values?
Euclidean uses squares and square root (row 1), Manhattan sums absolute differences (row 3). They measure distance differently, so results differ.
Are the original points changed during calculation?
No, variable_tracker shows x and y stay the same throughout all steps.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 1, what is the Euclidean distance calculated?
A5.196
B9
C0.025
D3
💡 Hint
Check the 'Result' column in execution_table row 1.
At which step does the condition for cosine distance calculation appear?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for cosine metric details in execution_table.
If we change point y to [1, 2, 3], what happens to the Manhattan distance?
AIt becomes 5.196
BIt becomes 0
CIt becomes 9
DIt becomes 1
💡 Hint
Check variable_tracker for Manhattan distance and think about absolute differences.
Concept Snapshot
Distance metrics measure how far points are.
Euclidean: straight line distance (sqrt of sum squares).
Cosine: angle difference (1 - cosine similarity).
Manhattan: sum of absolute differences.
Use scipy.spatial.distance for easy calculation.
Full Transcript
We start with two points x and y. We pick a distance metric: Euclidean, Cosine, or Manhattan. For Euclidean, we square differences, sum them, then take the square root. For Cosine, we calculate the dot product and norms, then find 1 minus their ratio. For Manhattan, we sum absolute differences. Variables x and y stay unchanged. The distances are calculated step-by-step and output. Beginners often confuse cosine distance with length; it measures angle difference. Euclidean and Manhattan differ because one uses squares and root, the other sums absolute values. The code example uses scipy to compute these distances easily.