Menu
CoddyTech

Recap: Skateboard Repairs

Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 40 of 58.

challenge icon

Challenge

Medium

A skate park's rental desk wants a model that spots returned boards needing repair. The program reads the largest k to try.

  1. Split off 25% as test rows (random_state=0).
  2. 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.
  3. Grid-search a tree's max_depth over depths (cv=5, random_state=0) and print the best.
  4. The higher mean wins: print Winner: k-NN or Winner: tree with 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.822

Coddy then describes your chart. Theirs looks like this:

Bars titled Skateboard Repairs: Accuracy by k, one per k from 1 to 13, axis from 0.6 to 0.9. They wobble near 0.72 for small k, then climb to 0.785 at k = 13

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

Practice on your own: Online Python compiler