Nearest Neighbors
You'll build a classifier with no training step at all, just a distance measure and a vote, and find out why scaling the columns changes the answer.
About
k-nearest neighbors is the one model you can explain completely in a sentence: to label a new point, find the k labeled points closest to it and let them vote. There's nothing to train and nothing to tune except k, which makes it the right place to learn what a classifier is before a library hides it from you. Use a small public dataset with a handful of numeric columns and three or four classes, like the iris flowers or the wine set from the UCI repository, both free. What counts is your own distance and vote code rather than a library call, and a clear look at what happens when one column is measured in thousands and another in tenths.
At a glance
- Per requirement
- 15 XP
- Completion bonus
- 75 XP
- Requirements
- 5
- Takes
- One evening
- Skill level
- Beginner (done it a few times)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- On your own
Requirements
-
1
Load a small labeled dataset and look at it before you model it. Pick one with 100 to 300 rows, four to thirteen numeric columns and three or four classes. Print the first ten rows, the count per class, and the min and max of each column. If one class is 80 percent of the rows, pick another dataset, because this one would teach you the wrong lesson. Take a screenshot of the printout. Photo
-
2
Write the distance function and the vote by hand. Euclidean distance between two rows is the square root of the sum of the squared differences. For each test row, compute its distance to every training row, sort, take the closest k and count their labels. Use a plain loop and no library classifier. The whole thing fits in 40 lines. Take a screenshot of the code. Photo
-
3
Hold back a fifth of the rows and score k equals 1, 3, 5, 11 and 21 on both sides. Shuffle first, then split, because many public datasets are sorted by class and an unshuffled split puts one whole class in the test set. Report accuracy on the training rows and on the held-out rows for each k in a small table. k equals 1 scores 100 percent on training rows, because every point is its own nearest neighbor, and it's usually the worst on held-out rows; seeing that gap is the point. Take a screenshot of the table. Photo
-
4
Scale every column to the same range and score again. Subtract each column's mean and divide by its standard deviation, computed on the training rows only. Rerun the same five values of k. On most datasets accuracy moves by several points, because unscaled distance is dominated by whichever column has the biggest numbers. Take a screenshot of the before and after tables side by side. Photo
-
5
Find one test row the model gets wrong and show its neighbors. Print the misclassified row, its true label, and the k neighbors that outvoted it with their distances. Then say in two or three sentences why those neighbors were close. Usually two classes overlap in the columns you have, and no value of k will separate them. Post the printout with your sentences. Photo