freecoding.school100% FREE · NO SIGNUP
Tensor TownISSUE #19 of 120

knn · k-nearest neighbors

NeuraVSThe Overfit Ogre
Neura saysk-Nearest Neighbors classifies a point by the majority vote of its closest neighbors.

kNN is the simplest learner: to classify a new point, find the k closest training points and take their majority label (or average for regression). There's no real "training" — it stores the data and computes distances at prediction time ("lazy" learning). Intuitive and surprisingly effective, but slow at scale and very sensitive to feature scaling and the choice of k.

Power-ups you unlock

The Overfit Ogre attacks — common mistakes

Boss battleExplain why feature scaling matters so much for kNN.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre>new point → find 5 nearest training points
majority label wins
must scale features; choose k carefully</pre></body></html>
▶ Open the interactive comic issue
‹ Naive Bayes · Probability Over FeaturesNeural Networks · The Perceptron Origin ›