cat /en/the-threshold/
MDN-119:28
SERIES Inside Ordito · Chapter 5

The threshold

· Artificial Intelligence · 5 min read

Logistic regression gives each loyalty-card customer a churn probability: 0.07, 0.31, 0.82. But marketing has to decide whom to call, and a probability is not a decision. Between the two there is the threshold.

The threshold, the confusion matrix, the ROC curve

You choose a threshold, and all the customers above it become “at risk”. Comparing with what really happened fills in the confusion matrix: those who churn and were flagged (true positives), those who churn and slipped through, those who stay but were flagged for nothing.

From here, two measures. Precision: among those flagged, how many really churn. Recall: among those who churn, how many were flagged. Lowering the threshold raises recall and lowers precision. The ROC curve draws all the thresholds together, and the area under the curve, the AUC, measures how well the model ranks the customers, whatever threshold you choose.

In the lab, move the threshold over 3,000 customers from the test set: the line moves between the two distributions and the point slides along the ROC curve.

DEMO The threshold · about this demo · full screen ↗

Three experiments

The default threshold. With 0.5 the model flags few customers, the ones it is almost sure about. On the full test set: 3,317 customers, 62% really churn, but only 28% of those who churn are found.

Lowering the threshold. With 0.2, close to the true share of churners, the flagged customers become 13,071: recall rises to 73%, precision falls to 41%.

The budget decides. Marketing has 3,000 discount vouchers for 36,229 customers. The threshold is then the one that lets through the 3,000 most at risk: 62.7% really churn, against 20% for a random choice. That is the lift, about three times: an AUC translated into euros.

The same experiment in Python

You need the ordito.py module from the repository, which is downloaded with !wget -q https://raw.githubusercontent.com/mid079/machine-learning-teoria-pratica/main/ordito.py.

import numpy as np
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from ordito import carica, tabella_clienti

clienti = carica("clienti")
scontrini = carica("scontrini", ["id_scontrino", "id_cliente", "data_ora", "canale", "totale"])
train, test = tabella_clienti("2024-07-01", clienti, scontrini), tabella_clienti("2025-07-01", clienti, scontrini)
variabili = ["acquisti", "spesa", "quota_online", "giorni_ultimo", "eta", "mesi_iscritto", "app", "consenso_marketing"]
modello = make_pipeline(SimpleImputer(strategy="median"), StandardScaler(), LogisticRegression(max_iter=2000))
p = modello.fit(train[variabili], train["abbandona"]).predict_proba(test[variabili])[:, 1]
y = test["abbandona"].to_numpy()

for soglia in [0.5, 0.2, np.sort(p)[-3000]]:          # the last one: the 3,000 most at risk
    segnalati = p >= soglia
    precisione, richiamo = y[segnalati].mean(), y[segnalati].sum() / y.sum()
    print(f"threshold {soglia:.3f}: {segnalati.sum():5d} customers, precision {precisione:.1%}, recall {richiamo:.1%}")

In the book

This lab accompanies chapter 5, “Logistic regression”: the sigmoid, the log-odds, cross-entropy and its gradient, gradient descent from scratch, reading the coefficients, the confusion matrix, the ROC curve, imbalanced classes and SMOTE.

COVER COMING SOON
mltp · 2026

Machine Learning. Teoria, matematica e pratica degli algoritmi

Michele di Nuzzo · in Italian

Every algorithm in two steps: first derive it, with the mathematics it needs and an example worked by hand, then write it from scratch in NumPy. Only at the end use it with scikit-learn.

[the book’s page] [shop links coming soon]

The “Inside Ordito” series