Re-scoring
Re-scoring is a change applied to a model’s score after the model has produced it: a calibration step, a business rule, a boost, a second-stage ranker. It is attractive because it changes what the system outputs without retraining the base model. What a re-score can change depends on its form. A strictly increasing map keeps the ranking and changes only what the scores mean, so it can repair calibration but not ordering. Improving the ordering takes information the base score lacks, supplied by a learned rule or by an input the model never saw.
Ordering, ties and learned corrections
A useful distinction is whether the map strictly preserves ordering, merges scores, or changes ordering using a learned rule.
The base model sees only the first three of four features, and its probabilities are raised to the power 1.7, a monotone distortion that keeps the ranking and miscalibrates the scores. A 20,000-row calibration set fits the corrections and a separate 20,000-row test set produces every number below. ece averages the gap between the observed rate and the mean score over ten equal-width score bins, weighted by bin size.
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.isotonic import IsotonicRegression
from sklearn.metrics import roc_auc_score
rng = np.random.default_rng(3030)
def ece(y, p, bins=10):
edges = np.linspace(0, 1, bins + 1)
idx = np.clip(np.searchsorted(edges, p, "right") - 1, 0, bins - 1)
return float(sum(abs(y[idx == b].mean() - p[idx == b].mean()) * (idx == b).mean()
for b in range(bins) if (idx == b).sum() > 0))
def make(m):
X = rng.normal(size=(m, 4))
logit = 1.0 * X[:, 0] - 0.7 * X[:, 1] + 0.5 * X[:, 2] + 0.8 * X[:, 3] - 0.3
return X, (rng.random(m) < 1 / (1 + np.exp(-logit))).astype(int)
Xtr, ytr = make(40000)
base = LogisticRegression(max_iter=5000).fit(Xtr[:, :3], ytr)
Xc, yc = make(20000) # calibration set: fits the corrections
Xte, yte = make(20000) # test set: every number in the table
raw_c = base.predict_proba(Xc[:, :3])[:, 1] ** 1.7
p_te = base.predict_proba(Xte[:, :3])[:, 1] ** 1.7
Three re-scores are applied to the test scores. The isotonic map is fitted on the calibration set’s distorted scores and labels. The random nudge adds Gaussian noise with standard deviation 0.1 and uses no labels. The second stage is a logistic regression on the distorted score and the fourth feature, fitted on the calibration set, so it can use information the base model never saw.
cal = IsotonicRegression(out_of_bounds="clip").fit(raw_c, yc)
iso = cal.predict(p_te)
noise = np.clip(p_te + 0.1 * rng.normal(size=len(p_te)), 1e-6, 1 - 1e-6)
stack = LogisticRegression(max_iter=5000).fit(
np.column_stack([raw_c, Xc[:, 3]]), yc)
informed = stack.predict_proba(np.column_stack([p_te, Xte[:, 3]]))[:, 1]
print("%-40s %10s %10s" % ("re-score", "AUC", "ECE"))
for label, q in (("none", p_te), ("isotonic recalibration", iso),
("a random nudge of 0.1", noise),
("a second stage that reads feature 4", informed)):
print("%-40s %10.4f %10.4f" % (label, roc_auc_score(yte, q), ece(yte, q)))
re-score AUC ECE
none 0.7674 0.1571
isotonic recalibration 0.7668 0.0147
a random nudge of 0.1 0.7404 0.1539
a second stage that reads feature 4 0.8122 0.0142
Isotonic recalibration cuts ECE from 0.1571 to 0.0147 and leaves AUC almost unchanged (0.7674 to 0.7668), while the random nudge lowers AUC to 0.7404 and leaves ECE near 0.1539. Recalibration may be strictly increasing, in which case all ordering and AUC are preserved, or nondecreasing, as with isotonic regression, which can create ties and change AUC. The random perturbation worsens ranking in this example; that observation does not classify every possible post-processing rule.
The second stage raises AUC to 0.8122 in this example. New features are one route to improvement, but not the only one: a learned nonmonotone map can correct a systematically misordered score using labels, and a second-stage model can use existing features differently. Distinguish absence of new input features from absence of new information learned during fitting. Validate calibration and ranking independently.
A boost is not a size
The most common re-score in a catalog is a flat addition to the score of items that deserve a push. What that constant is worth depends entirely on how crowded the score is where the item happens to sit.
The examples that follow continue in the same Python session. Here relevance is a score in (0, 1), 15% of the items are promoted by a flat +0.20, and moved is the number of places an item gains in the ranking. The table groups promoted items by their original score band and reports the mean places gained next to the mean number of catalog items scored within 0.20 above each promoted item, which is the stretch of the ranking a +0.20 boost can jump across.
rng = np.random.default_rng(4711)
n = 5000
relevance = 1 / (1 + np.exp(-rng.normal(0.0, 1.4, n)))
boost = 0.20
promoted = rng.random(n) < 0.15
after = np.where(promoted, relevance + boost, relevance)
rank_before = np.argsort(np.argsort(-relevance))
rank_after = np.argsort(np.argsort(-after))
moved = rank_before - rank_after
srt = np.sort(relevance)
window = (np.searchsorted(srt, relevance + boost, "right")
- np.searchsorted(srt, relevance, "right"))
print("%-26s %16s %22s" % ("promoted item sits at", "places gained", "items within 0.20 above"))
edges = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
for lo, hi in zip(edges[:-1], edges[1:]):
m = promoted & (relevance >= lo) & (relevance < hi)
if m.sum() == 0:
continue
print("%-26s %16.0f %22.0f"
% ("score %.1f to %.1f" % (lo, hi), moved[m].mean(), window[m].mean()))
promoted item sits at places gained items within 0.20 above
score 0.0 to 0.2 894 1048
score 0.2 to 0.4 989 1163
score 0.4 to 0.6 939 1096
score 0.6 to 0.8 902 1045
score 0.8 to 1.0 366 422
The same constant gives promoted items in the 0.2โ0.4 score band a mean gain of 989 places, versus 366 in the 0.8โ1.0 band, a factor of 2.7 across score bands — and the band where it does least is the top of the page, where a boost is usually meant to help. The places gained follow the last column: an item can only pass the items that lie within 0.20 above its score, and near the top there are few of them (422 against 1,163), so the same +0.20 has little to pass.
An additive boost on an uncalibrated score is not a policy anyone can state in a sentence. If the intent is a fixed number of places, move ranks. If the intent is a fixed odds ratio, add in log-odds on a calibrated score. Both of those survive being read out in a meeting; +0.2 does not.
Adding to a probability is not combining evidence
The other common pattern is folding a side signal into a score: a fraud probability of 0.7 becomes 0.85 after the customer risk profile is taken into account.
A side signal risk also drives the label but is unseen by the base model. On the test set the base score is compared with the nudge score + 0.15 * risk and with a logistic regression on the base log-odds and the risk signal, fitted on a separate holdout set. The last line counts test cases above 0.8.
rng = np.random.default_rng(5959)
def make_with_risk(m):
X = rng.normal(size=(m, 3))
risk = rng.normal(size=m)
logit = 0.9 * X[:, 0] - 0.6 * X[:, 1] + 0.4 * X[:, 2] + 0.8 * risk - 1.2
return X, risk, (rng.random(m) < 1 / (1 + np.exp(-logit))).astype(int)
def log_odds(q):
return np.log(q / (1 - q))
Xtr, rtr, ytr = make_with_risk(50000)
base = LogisticRegression(max_iter=5000).fit(Xtr, ytr)
Xh, rh, yh = make_with_risk(20000)
Xte, rte, yte = make_with_risk(20000)
p = base.predict_proba(Xte)[:, 1]
bump = np.clip(p + 0.15 * rte, 1e-6, 1 - 1e-6)
combo = LogisticRegression(max_iter=5000).fit(
np.column_stack([log_odds(base.predict_proba(Xh)[:, 1]), rh]), yh)
principled = combo.predict_proba(np.column_stack([log_odds(p), rte]))[:, 1]
print("%-44s %10s %10s" % ("", "AUC", "ECE"))
for label, q in (("base score", p),
("nudged: score + 0.15 * risk profile", bump),
("combined in log-odds, fitted", principled)):
print("%-44s %10.4f %10.4f" % (label, roc_auc_score(yte, q), ece(yte, q)))
print("test cases scoring above 0.8, of %d: base %d, nudged %d"
% (len(p), (p > 0.8).sum(), (bump > 0.8).sum()))
AUC ECE
base score 0.7477 0.0061
nudged: score + 0.15 * risk profile 0.7988 0.0295
combined in log-odds, fitted 0.8014 0.0107
test cases scoring above 0.8, of 20000: base 155, nudged 487
Both versions use the same extra information and both raise AUC. In this simulation only the fitted combination keeps calibration error low, and that matters when downstream rules are written in score units. The ad-hoc addition takes binned calibration error from 0.0061 to 0.0295, and a review queue sized for scores above 0.8 would take 155 of the 20,000 test cases under the base score and 487 under the nudge, on the same cases. The binned error is a summary over the whole test set; it does not show that any single score is correct.
Adding in log-odds is a natural way to combine signals when they carry roughly independent evidence about the outcome, and the weight on the new term is something to fit rather than to pick. In this example the fitted weights, on the log-odds scale, account for the difference between the last two rows at the cost of one extra regression on a holdout. Other combination rules, such as a model trained on both signals together, are also legitimate, and which one suits a system depends on how the signals relate and on what the downstream decision needs.
A re-score patches the output. A patch that keeps working can point to information the model should have learned, as the fourth feature did in the first example, so keep a note of which patches are earning their keep; each one makes the score harder to reason about. The alternative repair, reweighting the training data, can approximate a threshold change under constant class weighting in suitable settings, while changing probability calibration; other weighting objectives can have different effects.
References
- scikit-learn documentation,
sklearn.isotonic.IsotonicRegression. - scikit-learn documentation,
sklearn.linear_model.LogisticRegression. - scikit-learn, Probability calibration. Source
Discover more from Insightful Data Lab
Subscribe to get the latest posts sent to your email.
