K Nearest Neighbors: learn by comparing with similar examples.
KNN is one of the most intuitive machine learning algorithms: to predict a new point, look at the most similar past examples and let them vote or average.
Opening intuition
Quick question
If your three closest friends all recommend the same restaurant, how much does that influence your choice?
That is the everyday intuition of KNN: nearby examples influence the decision.
Imagine a new customer enters our dataset. Instead of learning coefficients or a tree, KNN asks:
If most similar customers churned, classify this customer as likely churn. If their average bill was high, predict a high bill.
The prediction depends on nearby known examples.
Session roadmap
KNN basics
Classification, regression, decision process, and when KNN is useful.
2Distance metrics
Euclidean, Manhattan, Minkowski, cosine similarity, scaling, metric choice, irrelevant features, and leakage-safe pipelines.
3Choosing K
Bias-variance tradeoff, validation curves, outliers, class imbalance, curse of dimensionality, weighted neighbors, and fast neighbor search.
4KNN imputation
How KNN fills missing values using similar rows, with procedure and simulation.
Core idea in one line
| Task | Aggregation | Output |
|---|---|---|
| Classification | Majority vote | Class label |
| Regression | Mean or weighted mean | Numeric value |
| Imputation | Mean/mode of neighbor values | Missing value replacement |