Back to Classic ML Mind Map
中文·English
📊 Classic MLID: distance-metrics

Distance Metrics & Indexing

距离度量与 KD-Tree
🎯Core Definition
Distance metrics define similarity in feature space and directly drive the results of KNN, clustering, and retrieval. Common metrics: Euclidean distance d2(x,y)=i(xiyi)2d_2(x, y) = \sqrt{\sum_i (x_i - y_i)^2} (L2, most natural when all dimensions share scale); Manhattan distance d1(x,y)=ixiyid_1(x, y) = \sum_i |x_i - y_i| (L1, more robust to outliers and more stable in high dimensions); cosine similarity cosθ=xyxy\cos \theta = \frac{x \cdot y}{\Vert x \Vert \Vert y \Vert} (direction only, magnitude-invariant — the default for TF-IDF/embedding retrieval); Minkowski distance dp(x,y)=(ixiyip)1/pd_p(x, y) = \left( \sum_i |x_i - y_i|^p \right)^{1/p} unifies them (p=1p=1 Manhattan, p=2p=2 Euclidean).Indexing: a KD-Tree recursively bisects space at coordinate medians, giving O(logn)O(\log n)-scale low-dimensional queries; a Ball-Tree partitions with hyperspheres and degrades more gracefully in higher dimensions. Feature scaling matters: variables with larger magnitudes dominate the distance unless z-scored (or min-max normalized) first.
💡Use Cases
the similarity core of KNN and K-Means, text/vector retrieval (cosine), recommendation recall, anomaly detection; interviews often ask when to choose Euclidean vs cosine and how KD-Tree works.
Key Problems Solved
no single metric covers all semantics — Euclidean assumes equal, commensurable dimensions; cosine removes document-length bias in text; L1 resists outliers; and in high dimensions LpL_p norms lose contrast as pp grows (relative distances converge), which is why cosine/Manhattan are preferred there. KD-Tree/Ball-Tree cut naive O(nd)O(nd) KNN queries to O(logn)O(\log n) scale in low dimensions, but degrade in high dimensions where ANN schemes are needed.
🎯5 High-Frequency Exam Points
1
Write the Euclidean, Manhattan, and cosine similarity formulas; how does Minkowski distance unify them?
2
Why is cosine similarity cosθ=xyxy\cos \theta = \frac{x \cdot y}{\Vert x \Vert \Vert y \Vert} invariant to vector length? Why is it standard for text retrieval?
3
Euclidean vs cosine vs Manhattan — when to use each? Why is L1 more stable than L2 in high dimensions?
4
How does a KD-Tree build and query? Why does it degrade toward a linear scan in high dimensions?
5
Why is feature scaling essential for distances? How do different scales distort them? What is the standardization formula?
📖 In-depth Guide:📄 clustering-and-knn
Updated 2026-08-12
🎯
Test Your Knowledge: Practice Questions for "Distance Metrics & Indexing"
Single choice pitfall questions with instant feedback and mistake tracking.
🚀 Start Card Practice
Previous CardKNN & K SelectionNext CardPCA & SVD

🔗 More Classic ML Knowledge Cards

AdaBoost DerivationBagging & Random ForestBaum-Welch (HMM EM)GBDT Negative Gradient