Sub topic 1
From model scores to labels
Prerequisites
This chapter assumes you know:
- Binary classification, with a positive class (label 1) and a negative class (label 0).
- Basic Python and NumPy.
- That models such as logistic regression output a probability or score.
You do not need to know ROC curves yet. This chapter sets up the inputs that the rest of the tutorial uses.
Why a model produces scores first
Most binary classifiers do not output a label directly. They compute a number for each example, and a rule then turns that number into a label. The numbers fall into two common kinds:
- A probability estimate, such as the output of
predict_proba. It lies between 0 and 1. - A raw score, such as the output of
decision_function. It can be any real number, and larger means "more likely positive".
Both are called scores in this tutorial. A ROC curve is built from scores and true labels. It is not built from the final predicted labels.
From score to label
The simplest rule compares each score with a cutoff:
label = 1 if score >= cutoff else 0The cutoff is the decision threshold. This chapter only uses it to produce labels. The next chapter looks at how to choose it.
The diagram shows the pipeline.
The dotted line from the score to evaluation matters. Metrics like accuracy use only the predicted label. ROC analysis uses the score itself.
A worked example
This code trains a logistic regression model and produces both scores and labels.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
X, y = make_classification(n_samples=500, n_features=5, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=0, stratify=y
)
model = LogisticRegression().fit(X_train, y_train)
scores = model.predict_proba(X_test)[:, 1] # probability of class 1
labels_default = (scores >= 0.5).astype(int)
labels_low = (scores >= 0.3).astype(int)
print(np.array_equal(labels_default, model.predict(X_test)))
print(labels_default.sum(), labels_low.sum())Three things to notice:
predict_probareturns one column per class. Column 1 is the probability of the positive class, so[:, 1]selects it.model.predictuses a cutoff of 0.5 on the probability. The firstprintshows that our manual rule gives the same labels.- Lowering the cutoff to 0.3 labels more examples as positive. The scores did not change, only the rule. The second
printshows the two positive counts.
Common errors
- Passing labels where scores belong. If you give
predict(X_test)to a ROC function, every example has only two distinct values. The curve then has almost no points and hides most of the model's ranking information. Always passpredict_probaordecision_functionoutput. - Using the wrong column.
predict_proba(X)[:, 0]is the probability of class 0. Using it flips the meaning of the scores. - Treating scores as calibrated probabilities. A score of 0.8 does not always mean an 80% chance of being positive. Many models rank examples well while producing poorly calibrated numbers. ROC analysis depends only on the order of the scores.
Exercise
Exercise
A model gives these scores for four examples: 0.9, 0.6, 0.4, 0.1. With a cutoff of 0.5 and the rule "predict 1 if score >= cutoff", what are the predicted labels? What are they if the cutoff drops to 0.3?
Show answer
With a cutoff of 0.5 the labels are 1, 1, 0, 0. With a cutoff of 0.3 the labels are 1, 1, 1, 0, because 0.4 is now at or above the cutoff. The scores are the same in both cases. Only the cutoff changed.