How k-NN Decides
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 6 of 58.
This chapter opens k-NN up. By its end, you will draw its map for any data, like this one for mild and hot chilies:
To label a new chili, k-nearest neighbors finds the k training chilies closest to it, and they vote:
Closest means the smallest distance, the one you met in section 2. You choose k:
model = KNeighborsClassifier(n_neighbors=5)n_neighbors is k, 5 unless you say otherwise. With two classes, an odd k can never tie.
Challenge
EasyAn astronomy club sorts the stars in stars.csv into dwarfs and giants by temperature and magnitude. The starter reads a new star and k.
- Find the k nearest stars by hand: add a
distancecolumn, then sort. - Print how many of them are giants, then the vote's winner.
- Check with a k-NN model of that k, fitted on every star.
For another club's star and k = 5:
Giants among the nearest 5: 4
By hand: giant
Model: giantTry it yourself
import numpy as np
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
# One row per star: temperature in thousand kelvin, magnitude (smaller is brighter), dwarf or giant
stars = pd.read_csv("stars.csv")
X = stars[["temp_kK", "magnitude"]]
y = stars["type"]
# The new star, and k
temp = float(input())
mag = float(input())
k = int(input())
new_star = pd.DataFrame({"temp_kK": [temp], "magnitude": [mag]})
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Supervised Learning
1Your First Model
Learning from ExamplesTrain and Test SetsFit and PredictScoring a ModelRecap: Seed Sorter2Nearest Neighbors
How k-NN DecidesChoosing kDecision BoundariesScaling for NeighborsRecap: Wine Neighbors5Measuring Classifiers
The Confusion MatrixPrecision and RecallThe F1 ScoreImbalanced ClassesROC CurvesRecap: Screening Test11Project: Penguin Species
Meet the PenguinsPrepare the DataChoose a ModelTune the WinnerShip the ModelPractice on your own: Online Python compiler