2.3.16Tree-Based & Instance Methods

Distance metrics (Euclidean, Manhattan, cosine)

1,836 words8 min readdifficulty · medium2 backlinks

What is a distance metric? (First principles)


The Minkowski family (derive them ALL from one formula)


Cosine similarity & distance (a DIFFERENT idea: angle, not length)

Figure — Distance metrics (Euclidean, Manhattan, cosine)

Worked Examples


Common Mistakes (Steel-manned)


Flashcards

What single parent formula generates Euclidean, Manhattan, and Chebyshev?
Minkowski distance dp=(iaibip)1/pd_p=(\sum_i|a_i-b_i|^p)^{1/p}, with p=2,1,p=2,1,\infty respectively.
Write the Euclidean distance formula.
i(aibi)2\sqrt{\sum_i (a_i-b_i)^2} — from the Pythagorean theorem.
Write the Manhattan distance formula.
iaibi\sum_i |a_i-b_i| — sum of absolute axis-wise differences (grid walk).
Derive cosine similarity from the dot product.
ab=abcosθcosθ=abab\mathbf{a}\cdot\mathbf{b}=\|\mathbf{a}\|\|\mathbf{b}\|\cos\theta \Rightarrow \cos\theta=\frac{\mathbf{a}\cdot\mathbf{b}}{\|\mathbf{a}\|\|\mathbf{b}\|}.
How do you turn cosine similarity into a distance?
distance =1cosθ=1-\cos\theta, ranging 00 (same direction) to 22 (opposite).
Which metric ignores vector magnitude and why is that useful?
Cosine — it measures angle/orientation only; ideal for text/TF-IDF where doc length shouldn't matter.
Which metric axiom does cosine distance violate?
The triangle inequality (so it's a dissimilarity, not a true metric).
Why must you scale features before Euclidean/Manhattan kNN?
Large-unit features dominate the sum, distorting "closeness"; standardize with z=(xμ)/σz=(x-\mu)/\sigma.
For the same two points, which is larger: Euclidean or Manhattan?
Manhattan \ge Euclidean (grid walk is never shorter than the straight diagonal).
What does Minkowski pp\to\infty give?
Chebyshev distance maxiaibi\max_i|a_i-b_i|.

Recall Feynman: explain to a 12-year-old

Imagine two houses on a map. Euclidean distance is how far a bird flies straight between them. Manhattan distance is how far a taxi drives on the streets — it can't fly, so it goes across then up, which is longer. Cosine is totally different: forget "how far," ask "are they in the same direction from the town center?" Two friends both walking east are going the "same way" even if one is 1 km out and the other 10 km out. A computer decides which houses are "neighbors" using one of these rulers — and picking the wrong ruler picks the wrong friends.


Connections

Concept Map

needs to define

IS the model's

must satisfy

include

parent formula of

p=1

p=2

p to infinity

derived from

satisfies

satisfies

violates

ignores

k-Nearest Neighbors

Distance Metric

Inductive Bias

Metric Axioms

Triangle Inequality

Minkowski Distance order p

Manhattan L1 grid-walk

Euclidean L2 straight-line

Chebyshev max-diff

Pythagorean Theorem

Cosine Distance angle-based

Magnitude

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, kNN jaise "instance-based" models kuch train nahi karte — bas saara data yaad rakhte hain, aur prediction ke time poochte hain "sabse paas wale points kaun se hain?". Lekin "paas" ka matlab tabhi banta hai jab hum distance metric define karein — yani ek ruler. Alag ruler → alag neighbors → alag answer. Isliye metric hi model ka asli inductive bias hai.

Teen main rulers hain. Euclidean (L2) = seedhi line, Pythagoras se aata hai: (aibi)2\sqrt{\sum(a_i-b_i)^2}. Socho ek chidiya udd ke seedha jaati hai. Manhattan (L1) = grid pe taxi ki tarah, sirf axis ke along chalti hai, diagonal cut nahi kar sakti: aibi\sum|a_i-b_i| — isiliye ye hamesha Euclidean se bada ya barabar hota hai. Dono ek hi parent Minkowski formula ke bacche hain, bas p=2p=2 ya p=1p=1 daalo.

Cosine bilkul alag soch hai — ye "kitni door" nahi, "kis direction mein" poochta hai. Dot product se derive: cosθ=abab\cos\theta = \frac{a\cdot b}{\|a\|\|b\|}, aur distance =1cosθ= 1-\cos\theta. Text data (TF-IDF) mein bahut kaam ka, kyunki document ki length (magnitude) matter nahi karti, sirf topic ki direction. Jaise "cat cat cat" aur "cat cat cat cat cat" same direction mein hain — cosine bolega bilkul same.

Do bade practical points yaad rakhna: (1) Euclidean/Manhattan use karne se pehle features ko scale/standardize karo, warna bade units wala feature (jaise income) sab kuch hijack kar lega. (2) Cosine similarity ko distance mein convert karna mat bhoolo (1cosθ1-\cos\theta), warna kNN ulte neighbors chun lega. Bas yeh dhyan rakha toh distance metrics easy hain.

Test yourself — Tree-Based & Instance Methods

Connections