Skip to content
ML to LLMs
Labs / Classical ML

K-nearest neighbors

Move a query point, change K and the distance metric, and see what feature scaling does.

Theory and worked examples: K-nearest neighbors

K-nearest neighbors lab

Synthetic customers: age vs income. Click or drag anywhere to move the query point.

KNN neighborhood around a query point20406050100150age (years)income (thousand $)
did not buy (circles)bought (squares)neighborhood (k-th distance)
In raw units, income spans about 150 while age spans about 50, so distance is dominated by income: the neighborhood is a thin horizontal band of similar incomes, regardless of age. Turn on scaling and compare the leave-one-out accuracy (now 76.7%).