typestar

Classification metrics in Python

Accuracy alone hides the interesting failures: look at precision and recall too.

from sklearn.metrics import (accuracy_score, classification_report,
                             f1_score, precision_score, recall_score)

y_true = [0, 1, 1, 0, 1, 1, 0, 1]
y_pred = [0, 1, 0, 0, 1, 1, 1, 1]

print(f"accuracy  {accuracy_score(y_true, y_pred):.3f}")
print(f"precision {precision_score(y_true, y_pred):.3f}")
print(f"recall    {recall_score(y_true, y_pred):.3f}")
print(f"f1        {f1_score(y_true, y_pred):.3f}")
print(classification_report(y_true, y_pred, digits=3))

How it works

  1. Precision is how often a positive call is right.
  2. Recall is how much of the positive class you found.
  3. F1 is their harmonic mean, and the report prints all three.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
454
Tokens
135
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 74 seconds.

Type this snippet

Step 1 of 4 in Scoring, step 16 of 25 in Machine learning with scikit-learn.

← Previous Next →