Back to Classic ML Mind Map
中文·English
📊 Classic MLID: knn

KNN & K Selection

KNN 与 K 选择
🎯Core Definition
K-nearest neighbors (KNN) is a non-parametric lazy-learning algorithm: training only stores the data (zero training cost); at prediction time it finds the KK closest training samples to the input — majority voting for classification, mean for regression.Distance-weighted voting often improves robustness: wi=1d(x,xi)w_i = \tfrac{1}{d(x, x_i)} or wi=1d(x,xi)2w_i = \tfrac{1}{d(x, x_i)^2}, giving nearby neighbors more say; the decision boundary is set by the training points' Voronoi regions.Too-small K → high-variance overfitting (a single neighbor decides), too-large K → high-bias underfitting (distant classes intrude); cross-validation picks K.Curse of dimensionality: in high dimensions samples are sparse, all pairwise distances converge, the relative spread dmaxdmind_{\max} - d_{\min} collapses, neighbors are almost equidistant and discrimination degrades toward random — mitigated by dimensionality reduction or feature selection.
💡Use Cases
classification/regression baselines on small, low-dimensional data, recall stage of recommendation/retrieval, interpretable online prediction; interviews focus on why KNN fails in high dimensions and on complexity analysis.
Key Problems Solved
no training, non-parametric decision surfaces, incremental updates, and per-sample explanations; the catch is that all computation is deferred to prediction — naive inference costs O(nd)O(nd), reduced to O(logn)O(\log n) scale in low dimensions via KD-Tree/Ball-Tree but degrading to a linear scan in high dimensions; it is also scale-sensitive and requires z-score standardization first, otherwise large-magnitude features dominate the distance.
🎯5 High-Frequency Exam Points
1
What is the KNN classification/regression procedure? Why is it called lazy learning? What actually happens at train time?
2
What errors come from too-small vs too-large K? How to choose K? How does distance weighting wi=1/d(x,xi)w_i = 1/d(x, x_i) help?
3
What is the curse of dimensionality? Why do neighbors degenerate in high dimensions (relative distances converge)? Mitigations?
4
How does KNN differ from K-Means? Naive prediction complexity O(nd)O(nd) vs KD-Tree-accelerated inference?
5
Why is KNN sensitive to feature scaling? What preprocessing is required? Outputs for classification vs regression?
📖 In-depth Guide:📄 clustering-and-knn
Updated 2026-08-12
🎯
Test Your Knowledge: Practice Questions for "KNN & K Selection"
Single choice pitfall questions with instant feedback and mistake tracking.
🚀 Start Card Practice
Previous CardGMM Soft AssignmentNext CardDistance Metrics & Indexing

🔗 More Classic ML Knowledge Cards

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