Setting Decision Thresholds

Most classifiers output probabilities, and the threshold that converts those probabilities into a class label is a critical design choice that should be tuned to your application's needs.


The Default Threshold and Why to Change It

By default, classifiers use a threshold of 0.5 — predict class 1 if P > 0.5. This is rarely optimal, especially with imbalanced classes or when false positives and false negatives have very different costs.

Precision-Recall Tradeoff

Lowering the threshold increases recall (catch more true positives) but decreases precision (more false positives). Raising it does the opposite. In medical screening you often prefer high recall; in spam filtering you prefer high precision. The right balance depends entirely on the cost of each error type.

Finding an Optimal Threshold

<pre><code class="language-python">from sklearn.linear_model import LogisticRegression from sklearn.datasets import load_breast_cancer from sklearn.model_selection import train_test_split from sklearn.metrics import precision_recall_curve, f1_score import numpy as np X, y = load_breast_cancer(return_X_y=True) X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=0) clf = LogisticRegression(max_iter=5000).fit(X_tr, y_tr) scores = clf.predict_proba(X_te)[:, 1] precisions, recalls, thresholds = precision_recall_curve(y_te, scores) f1_scores = 2 * precisions[:-1] * recalls[:-1] / (precisions[:-1] + recalls[:-1] + 1e-8) best_thresh = thresholds[np.argmax(f1_scores)] print(f"Best threshold by F1: {best_thresh:.3f}")</pre>

Class-Weighted Approaches

An alternative to threshold tuning is to train with class_weight='balanced', which adjusts the loss to weight minority classes more heavily and implicitly shifts the model toward a better threshold.

When to Use Each Approach

Threshold tuning is post-hoc and flexible — you can adapt to business requirements without retraining. Class weighting is baked into training and produces a model whose probabilities already reflect the cost asymmetry. For the most control, tune both: train with class weights, then apply threshold tuning on a validation set.