Menu
CoddyTech

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:

The 80 chilies over a grid of small dots. Each dot is colored by the model's answer there: orange for hot on the left, blue for mild on the right, meeting along a stepped line

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 icon

Challenge

Easy

A 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.

  1. Make a 100 by 100 grid xx, yy: 18 to 26 mm across, 3 to 9 g up.
  2. Predict every point into zz, shaped like the grid.
  3. Fill the map, colors C0 to C2, then draw the coins and show.

Like this one:

A coin sorter map: three soft regions, blue for 10c at the lower left, orange for 5c in the middle, green for 25c on the right, each with its coins on top and a legend

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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Supervised Learning

Practice on your own: Online Python compiler