Contents
07Interview questions and model answers
Q1How does k-nearest neighbors work?
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?
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?
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?
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?
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?
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?
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?
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?
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?
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.