The F1 Score
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 23 of 58.
A model can max out precision or recall by giving up the other. The F1 score blends the two, and stays low unless both are high:
With P for precision and R for recall, F1 is 2 * P * R / (P + R). For a weather model:
from sklearn.metrics import f1_score, classification_report
y_test = ["sun", "sun", "rain", "rain", "snow", "sun", "rain", "snow"]
pred = ["sun", "rain", "rain", "rain", "snow", "sun", "sun", "rain"]
print(f1_score(y_test, pred, average=None)) # one per class, sorted
print(classification_report(y_test, pred))This prints:
[0.57142857 0.66666667 0.66666667]
precision recall f1-score support
rain 0.50 0.67 0.57 3
snow 1.00 0.50 0.67 2
sun 0.67 0.67 0.67 3
accuracy 0.62 8
macro avg 0.72 0.61 0.63 8
weighted avg 0.69 0.62 0.63 8The report scores every class; support counts its test rows. Its weakest row tells you what to fix first.
Challenge
MediumA recycling plant's camera sorts items into glass, metal, paper and plastic bins. The program reads k, and the starter trains the camera's k-NN, fills pred and lists the bins.
- Print the classification report.
- Find the bin with the lowest F1 and print it, then its weaker side, precision or recall, as below (3 decimals).
After the report, for another plant:
Weakest bin: metal, F1 0.667
Its weaker side: recall 0.571Try it yourself
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report, f1_score, precision_score, recall_score
k = int(input())
# One row per item on the belt, and the bin it belongs in
items = pd.read_csv("recycling.csv")
features = ["weight_g", "shine", "see_through", "bend"]
X_train, X_test, y_train, y_test = train_test_split(items[features], items["material"], test_size=0.25, random_state=0)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# The camera's model
model = KNeighborsClassifier(n_neighbors=k)
model.fit(X_train, y_train)
pred = model.predict(X_test)
# The bins, in the order the per-class scores come
bins = model.classes_
# 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