Recap: Skateboard Repairs
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 40 of 58.
Challenge
MediumA skate park's rental desk wants a model that spots returned boards needing repair. The program reads the largest k to try.
- Split off 25% as test rows (
random_state=0). - For each k in
ks, cross-validate k-NN on the training rows (cv=5). Draw the means as bars and print the best k. - Grid-search a tree's
max_depthoverdepths(cv=5,random_state=0) and print the best. - The higher mean wins: print
Winner: k-NNorWinner: treewith its test accuracy.
For another park and 13:
Best k: 13 (mean 0.785)
Best depth: 5 (mean 0.763)
Winner: k-NN, test accuracy 0.822Coddy then describes your chart. Theirs looks like this:
Try it yourself
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
# The largest k to try, like 15
max_k = int(input())
# One row per returned skateboard: rentals since its last service, wear scores from 0 to 10
# and cracks in the deck; 1 in repair means it went to the workshop
boards = pd.read_csv("skateboards.csv")
features = ["rentals", "wheel_wear", "bearing_noise", "grip_wear", "truck_wobble", "deck_cracks"]
X = boards[features]
y = boards["repair"]
# Every feature is a small count or score, so k-NN needs no scaler here
ks = list(range(1, max_k + 1, 2))
depths = [2, 3, 4, 5, 6, 7, 8]
fig, ax = plt.subplots()
ax.set_title("Skateboard Repairs: Accuracy by k")
ax.set_xlabel("k (neighbors)")
ax.set_ylabel("Mean CV accuracy")
ax.set_xticks(ks)
ax.set_ylim(0.6, 0.9) # starts at 0.6 so small differences show
# Write your code below
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