knn() — k-Nearest Neighbors

Label a new point by asking its nearest neighbours to vote.

knn(k, training_data)

Key idea

Definition

the new point to classify
its k nearest training points
how many neighbours vote
the predicted class

In plain words

New kid in class? Look at the 5 students sitting nearest and guess they like what most of them like.

Where you'd use it

Frequently asked questions

Why scale features for KNN?
It uses distances, so an unscaled large-range feature dominates.
How do I pick k?
By cross-validation — small k overfits, large k underfits.