# k-Nearest Neighbours and Naive Bayes

> Two simple, instructive models: similarity-based prediction and fast probabilistic classification with independence assumptions. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/knn-and-naive-bayes
Last updated: 2026-11-28

Two simple, instructive models: similarity-based prediction and fast probabilistic classification with independence assumptions.

Beginner · about 4 hours · difficulty 2/5

Learn first: [Logistic Regression and Linear Classifiers](https://www.internhack.in/roadmaps/tracks/machine-learning/logistic-regression)

These models are rarely your final choice on tabular data, but they are fast baselines, teach key concepts and remain useful in specific niches.

### k-nearest neighbours

- Predicts from the labels of the `k` closest training points; no real training step.
- Needs **scaled features** and a sensible distance metric.
- Small `k` overfits (noisy), large `k` underfits (too smooth).
- Suffers from the **curse of dimensionality**: in many dimensions all points look equally far apart.
- The same idea powers vector search over embeddings (approximate nearest neighbours).

### Naive Bayes

- Applies Bayes' rule assuming features are independent given the class.
- **Multinomial NB** on word counts is a classic, very strong baseline for text classification.
- **Gaussian NB** for continuous features, **Bernoulli NB** for binary ones.
- Trains in one pass and handles huge vocabularies easily; probabilities tend to be poorly calibrated.

**Good enough to move on** when you can explain why kNN degrades in high dimensions and build a text classifier with TF-IDF or counts plus naive Bayes.

## Key concepts

- **Distance metrics**
- **Choosing k**
- **Curse of dimensionality**
- **Multinomial naive Bayes for text**
- **Gaussian and Bernoulli NB**
- **Approximate nearest neighbour search**

## Mini project

Build an SMS spam filter on the UCI SMS Spam Collection with CountVectorizer and multinomial naive Bayes, and compare it with kNN on TF-IDF features.

## Check yourself

What does the 'naive' in naive Bayes assume, and why does the model often work well anyway?

## Free resources

- [scikit-learn: Nearest neighbors](https://scikit-learn.org/stable/modules/neighbors.html) (scikit-learn)
- [scikit-learn: Naive Bayes](https://scikit-learn.org/stable/modules/naive_bayes.html) (scikit-learn)

Previous: [Logistic Regression and Linear Classifiers](https://www.internhack.in/roadmaps/tracks/machine-learning/logistic-regression) · Next: [Decision Trees](https://www.internhack.in/roadmaps/tracks/machine-learning/decision-trees)

## More in Classical Supervised Learning

- [Framing ML Problems and the scikit-learn API](https://www.internhack.in/roadmaps/tracks/machine-learning/ml-problem-framing)
- [Linear Regression and Regularization](https://www.internhack.in/roadmaps/tracks/machine-learning/linear-regression)
- [Logistic Regression and Linear Classifiers](https://www.internhack.in/roadmaps/tracks/machine-learning/logistic-regression)
- [k-Nearest Neighbours and Naive Bayes](https://www.internhack.in/roadmaps/tracks/machine-learning/knn-and-naive-bayes)
- [Decision Trees](https://www.internhack.in/roadmaps/tracks/machine-learning/decision-trees)
- [Bagging and Random Forests](https://www.internhack.in/roadmaps/tracks/machine-learning/random-forests)
- [Gradient Boosting with XGBoost and LightGBM](https://www.internhack.in/roadmaps/tracks/machine-learning/gradient-boosting)
- [Support Vector Machines and Kernels](https://www.internhack.in/roadmaps/tracks/machine-learning/support-vector-machines)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
