The threshold
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.
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.
The “Inside Ordito” series
- Chapter 1 The line of the five stores
- Chapter 2 A simulated store
- Chapter 3 Where the leak hides
- Chapter 4 The valley of the five stores
- Chapter 5 The threshold
- Chapter 6 Why was it returned?
- Chapter 7 Build a tree
- Chapter 8 Boosting step by step
- Chapter 9 The margin
- Workshop A Price is place
- Chapter 10 Shapley by hand
- Chapter 11 Rotate the cloud
- Chapter 12 Groups and dense areas
- Chapter 13 Isolating a point
- Chapter 14 Naive forecasts
- Workshop B A year in production
- Chapter 15 The ink stain
- Chapter 16 Backpropagation