K-Nearest Neighbors
Study guides: Exam SRM, Exam MAS-II, Exam ACIA-CAPSTONE
K-Nearest Neighbors (KNN) is a nonparametric statistical learning method that predicts the response of a new observation x_0 from the K training observations closest to it. For regression, the prediction is the average of their responses:
\hat{f}(x_0) = \frac{1}{K}\sum_{i \in \mathcal{N}_0} y_i
\mathcal{N}_0is the set of theKtraining observations nearest tox_0, usually by Euclidean distance.
To find \mathcal{N}_0, compute the distance from x_0 to every training observation, rank them, and select the K smallest. The predictors are standardized first, because a distance depends on their units. KNN estimates no parameters. It keeps the training data and does its work at prediction time, which can be slow with a large training set. It also degrades with many predictors, because distances in high dimensions become uninformative.
The training data are
(x, y) = (1, 10),(3, 14),(4, 15),(7, 20)and(9, 30). What does KNN regression withK = 3predict atx_0 = 5?
- The distances from
x_0are4,2,1,2and4.- The three nearest are
x = 4,3and7, with responses15,14and20.\hat{f}(5) = (15 + 14 + 20)/3 = 16.33.
KNN classification
For classification, the estimated probability of class j at x_0 is
\hat{P}(Y = j \mid X = x_0) = \frac{1}{K}\sum_{i \in \mathcal{N}_0} I(y_i = j)
I(y_i = j)is1wheny_i = jand0otherwise.
KNN predicts the class with the largest estimated probability, the majority class among the K neighbors. With K = 1 it assigns the class of the single closest training observation.
The 3 nearest neighbors of a new claim are labelled Fraud, Fraud and Not Fraud. What are the estimated probability of fraud and the predicted class?
- Two of the three neighbors are Fraud, so
\hat{P}(\text{Fraud} \mid X = x_0) = 2/3 = 0.667.- The predicted class is Fraud.
Choosing K
A small K gives a flexible fit, with low bias and high variance, and a jagged decision boundary. A large K gives a smooth fit, with high bias and low variance. When K = n, every neighborhood is the whole training set, so KNN predicts the training data's majority class for every observation. K is chosen by cross-validation.
A binary classifier is fitted with
K = 1and withK = 50. How do the decision boundaries differ?
- With
K = 1the boundary is highly irregular, and the training error is zero.- With
K = 50the boundary is much smoother, and it misclassifies some training observations.
The Bayes classifier
The Bayes classifier assigns x_0 to the class j with the largest true \Pr(Y = j \mid X = x_0). KNN estimates this classifier, which can't be computed in practice because the true conditional probabilities are unknown. The Bayes error rate is the lowest possible test error rate:
1 - E\left[\max_j \Pr(Y = j \mid X)\right]
- The expectation averages over the values of
X.
At one point x_0 the Bayes classifier's error probability is 1 - \max_j \Pr(Y = j \mid X = x_0). For two classes with p = \Pr(Y = 1 \mid X = x_0), that is \min(p, 1 - p). On a set of test points, the Bayes classifier's expected error rate is the average of these error probabilities, worked from the true conditional probabilities rather than from the test labels.
At two test points the true probabilities of class 1 are
0.7and0.4. What is the Bayes classifier's expected error rate on them?
- At the first point it predicts class 1, with error probability
1 - 0.7 = 0.3.- At the second it predicts class 0, with error probability
0.4.- The expected error rate is
(0.3 + 0.4)/2 = 0.35.
Unstandardized predictors A distance on unstandardized predictors is driven by the one with the largest units: a
\$1{,}000difference in claim amount swamps a 10-year difference in age. Standardize the predictors before finding neighbors.