Sub topic 1
Starting point: what binary classification gives you
Multiclass and multilabel models are built from binary parts. This chapter lists what a binary classifier produces and which of those pieces the rest of the tutorial reuses or replaces.
Prerequisites
You should be comfortable with the following:
- Logistic regression at a basic level.
- Train/test splits and cross-validation.
- Precision, recall and F1 for one positive class.
- Basic Python and NumPy.
The binary pipeline
A binary classifier turns features into one number, then into a label. The steps are:
- The model computes a raw score, called a logit, from the features.
- A sigmoid squashes the logit into a probability between 0 and 1. This is the probability of the positive class.
- A decision rule compares the probability with a threshold, usually 0.5, and outputs 0 or 1.
Only one probability is needed. The negative class gets 1 minus that value, so the two outputs always sum to 1.
Seeing the threshold at work
The code below trains a logistic regression and applies three thresholds to the same probabilities.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import precision_score, recall_score, f1_score
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, stratify=y, random_state=0
)
model = make_pipeline(
StandardScaler(), LogisticRegression(max_iter=1000)
).fit(X_train, y_train)
proba = model.predict_proba(X_test)[:, 1] # P(class 1)
for threshold in (0.3, 0.5, 0.7):
pred = (proba >= threshold).astype(int)
print(
f"threshold {threshold}: "
f"precision {precision_score(y_test, pred):.3f}, "
f"recall {recall_score(y_test, pred):.3f}, "
f"F1 {f1_score(y_test, pred):.3f}"
)Lowering the threshold usually raises recall and lowers precision. The model stays the same. Only the decision rule changes. This idea returns later, when each label gets its own threshold.
What carries over and what changes
| Binary piece | Multiclass | Multilabel | Column |
|---|---|---|---|
| Target | One column of 0 and 1 | One integer from 0 to K-1 | A matrix of K columns of 0 and 1 |
| Output | One sigmoid | Softmax over K scores | K independent sigmoids |
| Loss | Binary cross-entropy | Categorical cross-entropy | Binary cross-entropy per label |
| Decision | One threshold | Argmax | A threshold per label |
| Metrics | Precision, recall, F1 | Confusion matrix, averaged F1 | Hamming loss, subset accuracy, averaged F1 |
Each later chapter fills in one row of this table.
Common errors to avoid
- Treating 0.5 as a law. It is a default. With imbalanced data or unequal error costs, another threshold often fits better.
- Reading
predict_probacolumns the wrong way. Column order followsmodel.classes_. In a binary problem, column 1 is the positive class only when the labels are 0 and 1. - Judging with accuracy alone. On a 95/5 split, a model that always predicts the majority class scores 95% accuracy and finds no positives.
- Assuming probabilities are well calibrated. A score of 0.8 does not always mean the event happens 80% of the time. Calibration gets its own chapter.
Exercise
Using the code above, you lower the threshold from 0.5 to 0.3 without retraining. Which metric do you expect to rise, which to fall, and why does the model itself not change?
Show answer
Recall usually rises, because more samples with moderate probabilities are labelled positive. Precision usually falls, because some of those extra positives are wrong. The model's weights and its probabilities stay the same. Only the decision rule that turns probabilities into labels has changed.