KNN
Cheat Sheet
Everything you need on one page. Perfect for revision, interviews, and quick reference.
Everything you need on one page. Perfect for revision, interviews, and quick reference.
Euclidean is the default. Manhattan is robust to outliers. Minkowski generalizes both ($p=1$ Manhattan, $p=2$ Euclidean). Cosine ignores magnitude.
Majority voting treats all $K$ neighbors equally. Weighted voting gives closer neighbors more influence. Add $\epsilon$ to avoid division by zero.
For regression, KNN averages the target values of the $K$ closest neighbors. Inverse-distance weighting often improves accuracy by reducing the effect of distant neighbors.
Feature scaling is critical for KNN. Without it, features with larger ranges dominate the distance calculation. Always fit on training data, then transform test data using the same parameters.
A: Because it performs no training. It simply stores the dataset and defers all computation to prediction time, unlike eager learners (e.g., SVM, logistic regression) that build a model during training.
A: Common strategies include using an odd $K$, applying distance-weighted voting so closer neighbors break the tie, or randomly selecting among the tied classes.
A: Brute force is $O(n \cdot d)$ per query. KD-Trees and Ball Trees reduce this to $O(d \cdot \log n)$ on average. LSH provides approximate $O(1)$ amortized lookup.
A: KNN relies on distance. If features have different scales, those with larger ranges dominate the distance. Scaling ensures all features contribute equally.
A: Choose Naive Bayes for high-dimensional sparse data (text), fast training, and when features are roughly independent. Choose KNN for low-dimensional data with complex, non-linear boundaries.
A: In high dimensions, distances between all points converge, making nearest neighbors no more meaningful than random points. This degrades KNN's performance significantly.
A: Non-parametric. It makes no assumptions about the underlying data distribution and the model complexity grows with the training set size.
A: Use distance-weighted voting so closer neighbors have more influence, oversample the minority class, undersample the majority class, or use algorithms like SMOTE before applying KNN.