- kNN predicts from the labels of the k closest training points.
- A decision tree splits the feature space into regions and predicts a constant in each leaf.
- Both are non-parametric: their complexity grows with the data, not with a fixed parameter vector.
This lesson follows Support Vector Machines & Kernels and leads into Bagging, Random Forests & Boosting. The probability background is in Probability & Statistics.
1. Local answers
Some questions are easier to answer locally. To classify a new point, look at the examples nearby. To predict a house price, look at recent sales in the same neighborhood. Nearest-neighbor methods turn that intuition into an algorithm.
There is no training phase in the usual sense: the training set is the model. Prediction does the work. That is both the appeal and the cost, because a large dataset means a large, slow lookup.
The animation shows a query point between two classes. With one neighbor the answer is local and unstable; with five, the vote is smoother. A decision tree then takes a different route and cuts the plane into rectangles.
2. k-nearest neighbours
kNN needs a distance. The default is Euclidean distance:
Sort the training points by distance, keep the first k, and take a majority vote for classification. For a probability, use the fraction of the k neighbors in class 1:
The choice of k is a bias-variance knob. k = 1 follows every point and can be noisy. A large k smooths the boundary but can blur a genuine local pattern. An odd k avoids ties in a two-class vote.
Distance also needs scaling. If one feature ranges from 0 to 1 and another from 0 to 10,000, the second feature dominates every distance unless you standardize.
3. Worked example
Use the eight training points from the animation and the query at (0.15, 0.2). The nearest point is class 0; with five neighbors the vote flips.
| k | neighbors | class-1 votes | probability | prediction |
|---|---|---|---|---|
| 1 | 0 | 0 | 0.00 | 0 |
| 5 | 0, 1, 1, 1, 1 | 4 | 0.80 | 1 |
Worked example: the neighborhood decides
The nearest point has label 0, so 1-NN predicts class 0. The five nearest points contain four class-1 examples, so 5-NN predicts class 1 with probability 0.8.
The tree trained on the same points has depth 1 and 2 leaves. At the query it predicts a class-1 probability of 0.00. Different inductive biases can give different answers on the same data.
A kNN boundary changes every time a point moves. A tree changes only when a split changes. That stability is one reason trees are easier to inspect; it is also why a single tree can be brittle.
4. Decision trees
A decision tree asks a sequence of yes/no questions: is x₁ ≤ 0.4? Is x₂ ≤ 0.8? Each answer sends the example down a branch. A leaf predicts the majority class of the training points that reached it.
The tree is a partition of the input space into axis-aligned rectangles. It is non-parametric because the number of leaves can grow with the data. A shallow tree underfits; a deep tree can carve out a rectangle around every point.
Trees are easy to read when they are small. At scale, a deep tree with hundreds of leaves is no longer a simple explanation, even though it is still a white-box model in principle.
5. Splits and impurity
A good split makes the children purer. Gini impurity is one common measure:
For a binary node with fraction p of class 1, G = 2p(1−p). It is 0 for a pure node and 0.5 when the classes are evenly mixed. A split is scored by the weighted average of its children:
The tree greedily chooses the split with the lowest weighted impurity, then recurses. Greedy is not globally optimal, but it is fast and works well in practice.
Why does a tree stop splitting a pure node?
6. Compare the two
kNN boundaries are determined by distance; tree boundaries are axis-aligned. kNN can represent a diagonal boundary with many nearby points, while a tree needs a staircase of rectangles. A tree can ignore an irrelevant feature once it finds a better split; kNN gives every feature a say in the distance.
Both are sensitive to the data and both can overfit. kNN with k=1 and a deep tree can memorize the training set. Cross-validation and a held-out set are still the way to choose k and depth.
Ensembles address a weakness shared by both: a single high-variance model changes a lot when the data change. The next lesson averages many trees.
7. Where they fail
kNN suffers in high dimensions: distances become less informative as the number of features grows, and the neighborhood may no longer be local. It also needs a meaningful distance metric and can be dominated by irrelevant features.
Trees are unstable and biased toward splits that happen to look good on the training sample. They extrapolate poorly because every leaf predicts a constant. Class imbalance can bias the majority vote unless you weight or resample.
The next lesson builds many trees and combines them, then adds boosting, which builds trees sequentially instead of independently.
Next on the recommended path: Bagging, Random Forests & Boosting
8. Going deeper
kNN can be accelerated with spatial indexes such as k-d trees and ball trees, or approximated with locality- sensitive hashing. The exact nearest neighbor may be unnecessary when the data are noisy.
Tree training can use entropy instead of Gini, and pruning can control complexity after growing. Cost-complexity pruning removes branches that do not justify their leaves. Missing-value routing and categorical splits are implementation details that matter in practice.
The bias-variance story is visible in both methods. A small k or a deep tree has low bias and high variance; a large k or a shallow tree has the reverse. The next lesson attacks the variance directly.
9. Practice
Why is k=1 often a bad default on noisy data?
What is the Gini impurity when a node has 8 class-0 and 2 class-1 examples?
Why can a tree represent XOR with depth 2 but a single stump cannot?
10. Resources
The MIT chapter covers nearest neighbors, tree models, splitting criteria, and ensembles. The scikit-learn tree chapter covers CART, impurity, pruning, and practical limits. Both links were opened and checked for this lesson.
Continue the track
Open the playground for this topic
Decision Boundary Playground →Add and drag points, switch between linear, quadratic, and neural models, and watch the boundary and both accuracy scores respond.
The mathematics behind this topic: Probability & statistics.