MoonSay /Tutorials/Machine Learning Interview #17
Contents
  1. 01Introduction
  2. 02The rule
  3. 03Scale first
  4. 04Choosing k
  5. 05Interview follow-ups
  6. 06Common misconceptions
  7. 07Interview questions and model answers
  8. 08Summary
  9. References
k-Nearest Neighbors/Chapter 08 of 08

08Summary

  • The rule. Distance to every training point, \operatorname{dist}(x,x_i)=\sqrt{\sum_j(x_j-x_{ij})^2}; the k closest; a vote, \hat p(c\mid x)=\frac1k\sum_{i\in N_k(x)}\mathbb I(y_i=c), or the average for regression. No training: the model is the data (the new wine: 4 votes to 1, cultivar one).
  • Scale first. A measurement’s expected share of the squared distance is \sigma_j^2/\sum_l\sigma_l^2, so the biggest numbers decide (proline: 95\%). Standardize, z_j=(x_j-\mu_j)/\sigma_j, with training statistics only (5 neighbors: 72\%\to95\%).
  • Choosing k. k=1 memorizes (training accuracy 100\%, high variance); a large k blurs the classes (high bias). Error =\sigma^2+\text{bias}^2+\sigma^2/k. Choose k by cross-validation, keep all training data, test once (k=23: 53 of 54).
  • Follow-ups. Prediction costs order n\times d (k-d trees help in low dimensions); high dimensions make neighbors far; with unlimited data, one neighbor’s error is at most twice the Bayes error.