Classification Models

A classification model predicts a category: spam or not, which of ten digits, which subset of tags. It is the counterpart of regression, which predicts a number, and the three shapes it takes are binary, multiclass and multi-label.

The usual next step is a list of algorithms โ€” logistic regression, decision trees, random forests, SVMs, k-nearest neighbors, naive Bayes, neural networks. That list is accurate and slightly misleading, because nearly every family on it has a regression twin. What makes a model a classifier is the target it was fitted to.

Binarizing a number costs you answers

import numpy as np
from sklearn.datasets import make_regression
from sklearn.linear_model import LogisticRegression, Ridge
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split

X, y = make_regression(n_samples=8000, n_features=12, n_informative=6, noise=15.0, random_state=0)
xtr, xte, ytr, yte = train_test_split(X, y, test_size=0.4, random_state=0)
cut = np.median(ytr)
"""The same linear family, the same features. Only the target's type differs - and one
   route throws the numeric target away before training, the other after."""

classifier = LogisticRegression(max_iter=5000).fit(xtr, (ytr > cut).astype(int))
regressor = Ridge(alpha=1.0).fit(xtr, ytr)
labels = (yte > cut).astype(int)
print(f"  trained on the binary label     AUROC {roc_auc_score(labels, classifier.predict_proba(xte)[:, 1]):.4f}")
print(f"  trained on the number, then cut AUROC {roc_auc_score(labels, regressor.predict(xte)):.4f}")
print(f"  the regressor also answers the numeric question: R2 {regressor.score(xte, yte):.4f}")

print("\n  and the cut point is a choice, not a property of the data")
for q in (0.5, 0.7, 0.9):
    t = np.quantile(ytr, q)
    lab = (yte > t).astype(int)
    c = LogisticRegression(max_iter=5000).fit(xtr, (ytr > t).astype(int))
    print(f"    cut at the {q:.0%} quantile   positives {lab.mean():.4f}"
          f"   classifier AUROC {roc_auc_score(lab, c.predict_proba(xte)[:, 1]):.4f}"
          f"   regressor AUROC {roc_auc_score(lab, regressor.predict(xte)):.4f}")
#   trained on the binary label     AUROC 0.9967
#   trained on the number, then cut AUROC 0.9967
#   the regressor also answers the numeric question: R2 0.9900
#
#   and the cut point is a choice, not a property of the data
#     cut at the 50% quantile   positives 0.4991   classifier AUROC 0.9967   regressor AUROC 0.9967
#     cut at the 70% quantile   positives 0.2978   classifier AUROC 0.9968   regressor AUROC 0.9969
#     cut at the 90% quantile   positives 0.1047   classifier AUROC 0.9985   regressor AUROC 0.9987

A common move when the business question is “will this be above the threshold” is to label each row above or below and train a classifier. The alternative is to predict the number and apply the threshold afterward.

On the ranking question the two tie exactly โ€” AUROC 0.9967 both ways. But the regressor also reports \(R^2\) 0.9900 on the number itself, and the last table shows it matching or beating the classifier at every cut point from the median to the 90th percentile, from a single fit.

The classifier answers one question: above this line or not. In this example, the regressor supplies a useful ranking for every tested cutoff and retains a prediction of the numeric outcome. A conditional-mean prediction alone does not determine the probability of exceeding a cutoff; that requires a predictive distribution or a separate probability model. Discarding the number before training loses the numeric answer for no measured ranking gain here.

Which also makes the cut point visible as what it is โ€” a choice. Moving it from the median to the 90th percentile takes the positive rate from 0.4991 to 0.1047, and everything downstream that depends on class balance moves with it.

The case for binarizing early is real but narrower than it is usually applied: when the numeric outcome is not actually measured (only the category was recorded), when the relationship is wildly non-monotonic around the threshold, or when a classifier’s calibrated probability is the deliverable.

Reading the metrics

Accuracy is the first number quoted and the least informative on imbalanced data โ€” where positives are 8.40% of rows, flagging nothing scores 0.9160. The confusion matrix, precision, recall and their harmonic mean describe what happens to the class you care about.

  • Precision and recall move with the threshold, not the model. One fitted model produced precision from 0.5271 to 0.9864 as the cut-off moved โ€” so a reported pair without its threshold is not reproducible.
  • In multiclass, micro F1 equals accuracy exactly. Quoting both fills two columns with one fact; macro averaging is what reveals a class the model has abandoned.
  • AUROC is threshold-free and blind to calibration โ€” a model can rank perfectly and be wrong about every probability by a factor of ten.

Classification models are everywhere it matters โ€” disease from symptoms, fraud from transactions, churn from behavior, spam from text, objects from pixels. In each of those the metric that counts is the one attached to the decision, and the threshold that produces it is chosen by someone rather than by the algorithm.

References


Discover more from Insightful Data Lab

Subscribe to get the latest posts sent to your email.

Similar Posts

Questions, corrections, or additional insights?

This site uses Akismet to reduce spam. Learn how your comment data is processed.