Module 7: KNN, Decision Trees, and Ensembles

This module explores non-parametric and tree-based models. K-Nearest Neighbors uses similarity to make predictions. Decision trees partition the feature space using interpretable rules. Ensemble methods combine multiple models for better performance.

Learning Objectives

  • Apply k-Nearest Neighbors for classification and understand distance metrics.
  • Explain how decision trees partition data using entropy and information gain.
  • Describe the tradeoffs between model depth and overfitting in trees.
  • Compare ensemble methods: bagging, random forests, and gradient boosting.
  • Implement tree-based models using scikit-learn.
0 of 8 stages complete
1 · Watch

The module in two minutes

An animated overview of the module’s through-line. Watch it before the lecture — it is the map, not the territory.

Lecture shorts

Short lectures and code walkthroughs from the instructor that reinforce this module. Some open partway into a longer video — that chapter is the part matched to this module.

Weak Classifiers, parts 1–3 — wisdom of the crowd, weak learners, ensembles
Overfitting, part 3 — why k-NN and unconstrained trees memorize
Intro talk: random forest on the Iris data
Applied theory: rank aggregation

Visual shorts

Short silent animations, one idea each. Press play, or open one full screen. All visual shorts

Bagging · full screen
Gradient boosting · full screen
What counts as near · full screen
Permutation importance · full screen
How deep to grow a tree · full screen
Growing a decision tree · full screen
2 · Listen

Orientation audio

Neighbors, trees, and ensembles — where each wins, and why boosting is the tabular baseline.

3 · Read & play

The through-line

This module breaks the pattern on purpose. Not every model is a parametric function trained by descent — two other families matter, and one of them wins most tabular-data competitions to this day.

K-nearest neighbors barely counts as training: store the data, and at prediction time answer with the majority vote of the k closest stored examples. It is simple, surprisingly strong, and a standing lesson in how far "similar inputs get similar outputs" goes — plus a warning about what "close" even means in many dimensions.

Decision trees ask learned questions — is income over 50k? is age under 30? — and route each example down branches to an answer. The learning is in choosing questions: entropy measures the impurity of a group, and the tree greedily picks the split with the largest information gain. The result is a model you can read aloud, and one that will happily memorize noise if grown deep enough.

Ensembles fix the tree's variance with statistics. A random forest trains many trees on bootstrapped samples with random feature subsets and averages them — individually noisy, collectively stable. Gradient boosting goes the other way: build small trees in sequence, each fitting the errors of the sum so far. Forests tame variance; boosting attacks bias; both routinely beat a single carefully tuned tree.

The practical takeaway is scope: on tables of features, tuned boosting is often the strongest baseline you can field, and it should be the first serious model you reach for there — neural networks earn their keep on images, text, and audio, which the coming modules turn to.

Figure · The k dial

Thirty stored examples, two classes, a few noise points on the wrong side. The colored field is the decision boundary: every location answers with the majority vote of its k nearest stored examples. Turn k.

At k = 1 training accuracy is 100% by construction — each point is its own nearest neighbor — and the boundary carves an island around every noise point: memorization, drawn. Raising k smooths the vote, sacrifices the noise points, and generalizes better. No parameters were trained at any k; the data is the model, and k is the smoothness dial.

Key concepts

  • KNN: Predict based on similar examples
  • Decision Trees: Partition space with learned rules
  • Entropy: Measure of impurity/disorder
  • Random Forests: Ensemble of decorrelated trees
  • Gradient Boosting: Sequential error correction
4 · Code

The lecture notebook

Six parts, from scratch before any framework. Run every cell; change things and rerun — the notebook is the lecture.

In Colab: File → Save a copy in Drive keeps your work.

5 · Practice

Exercise

Attempt the exercise before opening the solution — the attempt is where the learning happens.

6 · Check

Knowledge check

The quiz is self-graded and open-book. If a question surprises you, the corresponding lecture part is the fastest fix.

7 · Go deeper

Reference and discussion

8 · Done

Finish the module

Complete the stages above, then claim the finish here.