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 07 of 08

07Interview questions and model answers

10 questions · click a question to see a brief answer
Q1How does k-nearest neighbors work?
Brief answer

To predict for a new point, compute its distance to every training point, usually Euclidean, take the k closest, and let them vote: the majority class for classification, the average value for regression. The share of votes for each class estimates its probability near the point.

Q2Is it parametric? What does “lazy learning” mean?
Brief answer

Nonparametric: it has no fixed set of parameters, it keeps the whole training set, and its flexibility grows with the data. Lazy: nothing is learned in advance; all the work happens at prediction time.

Q3Why must you scale the features, and how?
Brief answer

The Euclidean distance adds raw squared differences, and a feature’s expected contribution is twice its variance, so the feature with the biggest numbers dominates. Standardize each feature, (x-\mu)/\sigma, or rescale it to [0,1], with statistics computed on the training data only. On the wine data, 5-nearest neighbors goes from 72\% to 95\%.

Q4How do you choose k?
Brief answer

By cross-validation on the training data: for each candidate k, average the held-out accuracy over the folds (scaling inside each fold), pick the best, keep all the training data with that k, and test once. Odd values avoid ties between two classes.

Q5What happens with k=1, and with k=n?
Brief answer

k=1: zero training error, a jagged boundary with islands around single points, high variance (overfitting). k=n: every point gets the majority class of the training set, maximal bias (underfitting).

Q6How does k trade bias against variance?
Brief answer

For regression, the expected error at x_0 is \sigma^2+\bigl[f(x_0)-\frac1k\sum_lf(x_{(l)})\bigr]^2+\sigma^2/k: averaging more neighbors divides the variance by k, but farther neighbors make the bias grow. The effective number of parameters is n/k.

Q7What is the cost of training and of prediction? How can you speed it up?
Brief answer

Training is free; each prediction costs order n\times d, and memory is n\times d. Tree indexes such as the k-d tree make the search logarithmic in low dimensions; in high dimensions use approximate nearest-neighbor search, or reduce the dimension first.

Q8Why does it struggle in high dimensions?
Brief answer

The curse of dimensionality: to hold a fixed fraction of the data, a neighborhood must cover most of the range of every axis (0.79 of each axis for 10\% of the data in ten dimensions), and the nearest and farthest points become almost equally far. Neighbors stop being similar. Use fewer or better features, or learn the distance.

Q9How good can one nearest neighbor be?
Brief answer

Cover and Hart (1967): with unlimited data its error is at most R^*(2-\frac{C}{C-1}R^*)\le2R^*, twice the Bayes error, the lowest error any classifier can reach. With finite data, especially in high dimensions, it can be far from that.

Q10What is the difference between k-nearest neighbors and k-means?
Brief answer

k-nearest neighbors is a supervised classifier or regressor; k is the number of neighbors that vote. k-means is unsupervised clustering; k is the number of clusters. Both rely on a distance, so both need scaled features.