Decision Boundaries
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 8 of 58.
A decision boundary is where a model's answer changes. To see it, ask the model at every spot of the chart, laid out as a grid:
np.meshgrid pairs every x with every y. Flatten the pairs into rows, predict them all, and fold the answers back into the grid's shape:
xx, yy = np.meshgrid(np.linspace(2, 16, 100), np.linspace(0.5, 6.5, 100))
grid = np.column_stack([xx.flatten(), yy.flatten()])
zz = model.predict(grid).reshape(xx.shape)
ax.contourf(xx, yy, zz, levels=[-0.5, 0.5, 1.5], colors=["C0", "C1"], alpha=0.3)contourf fills each class code's area: levels puts borders halfway between codes, colors matches the dots. Draw the dots after it, on top.
Challenge
EasyA vending machine sorts coins by size and weight. The starter fits k-NN on coins.csv (class codes 0 to 2); draw_coins(ax) draws them.
- Make a 100 by 100 grid
xx,yy: 18 to 26 mm across, 3 to 9 g up. - Predict every point into
zz, shaped like the grid. - Fill the map, colors C0 to C2, then draw the coins and show.
Like this one:
Another country's coins, so other regions.
Coddy describes your map.
Try it yourself
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier
# One row per coin the machine measured: diameter (mm), weight (g), value in cents
coins = pd.read_csv("coins.csv")
X = np.column_stack([coins["diameter_mm"], coins["weight_g"]])
# Class codes: 0 for 10c, 1 for 20c, 2 for 50c
y = coins["value_c"].map({10: 0, 20: 1, 50: 2})
names = ["10c", "20c", "50c"]
model = KNeighborsClassifier(n_neighbors=5)
model.fit(X, y)
# Draws the coins on ax, one color per class: C0, C1, C2
def draw_coins(ax):
for code in range(3):
ax.scatter(X[y == code, 0], X[y == code, 1], s=20, label=names[code])
ax.legend()
fig, ax = plt.subplots()
ax.set_title("Coin Sorter Map")
ax.set_xlabel("Diameter (mm)")
ax.set_ylabel("Weight (g)")
# 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