Scoring a Model
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 4 of 58.
Is a model any good? Its accuracy, the share of test rows it gets right, says. Set it beside a lazy guess:
78% of this library's test loans came back on time, so answering on time every time scores 0.78. That is the baseline.
from sklearn.metrics import accuracy_score
pred = model.predict(X_test)
print(accuracy_score(y_test, pred))
print(model.score(X_test, y_test))
common = y_train.value_counts().index[0]
print((y_test == common).mean())This prints 0.88, 0.88, 0.78. score predicts and counts in one call; the baseline always answers the most common training label.
A model earns its place only by beating the baseline.
Challenge
EasyA beekeeper's sensors watch 160 hives in hives.csv; swarm says whether each hive swarmed. Can one sensor warn her? The starter reads a sensor's name and fits a model on that sensor alone.
- Print its test accuracy, rounded to 3 decimals.
- Print the baseline, naming the label it always answers.
- Print whether the model beats it.
For another apiary and weight_change_kg:
Model on weight_change_kg: 0.775
Always "no": 0.7
Beats the baseline: yesTry it yourself
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
# One row per hive: its sensors, and whether it swarmed
hives = pd.read_csv("hives.csv")
sensor = input()
X = hives[[sensor]]
y = hives["swarm"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
model = KNeighborsClassifier()
model.fit(X_train, y_train)
# 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