python 16 lines · 1 tab

Confusion matrix diagnostics for threshold selection

1 tab
import numpy as np
from sklearn.metrics import confusion_matrix

probabilities = model.predict_proba(X_valid)[:, 1]
thresholds = np.linspace(0.1, 0.9, 9)

for threshold in thresholds:
    predictions = (probabilities >= threshold).astype(int)
    tn, fp, fn, tp = confusion_matrix(y_valid, predictions).ravel()
    print({
        'threshold': round(float(threshold), 2),
        'tp': int(tp),
        'fp': int(fp),
        'fn': int(fn),
        'tn': int(tn),
    })
1 file · python Explain with highlit

Thresholds are policy decisions disguised as numbers. I use confusion matrices to make those tradeoffs concrete for stakeholders: how many risky accounts we block, how many fraud attempts slip through, and how much manual review load is created. This is often where model work turns into operational design.

Share this code

Here's the card — post it anywhere.

Confusion matrix diagnostics for threshold selection — share card
Link copied