Sub topic 6
Heuristics and the existing system as baselines
This chapter assumes you can fit scikit-learn models, know the metric you will report, and have a fixed train/test split. Constant predictors and simple models give you a floor. A heuristic or the system already in production often gives you a harder, more relevant target.
Why a heuristic baseline matters
A heuristic is a hand-written rule that needs no training, such as "flag an order if it is more than five times the customer's average". A majority-class predictor only tells you what you get from knowing nothing. A heuristic tells you what a person with domain knowledge and ten minutes could do. If your model cannot beat it, the model adds little.
The existing system is the strongest heuristic baseline. It may be a rules engine, a vendor tool, or a team of human reviewers. Whatever it is, the business already runs on it, so your model has to beat it to be worth deploying.
Choosing the baseline
The diagram shows how to pick which baseline to build first.
Building a heuristic baseline
The rule below is learned from training data only. It finds the feature most correlated with the label and flags the top 10% of that feature. It is then scored on the same test split as the model, with the same metrics.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import balanced_accuracy_score, recall_score
from sklearn.model_selection import train_test_split
X, y = make_classification(n_samples=2000, weights=[0.95, 0.05], random_state=0)
X_train, X_test, y_train, y_test = train_test_split(
X, y, stratify=y, random_state=0
)
# Heuristic: flag the top 10% of the single most label-correlated feature.
corr = [np.corrcoef(X_train[:, j], y_train)[0, 1] for j in range(X_train.shape[1])]
j = int(np.argmax(np.abs(corr)))
sign = np.sign(corr[j])
threshold = np.quantile(sign * X_train[:, j], 0.9)
def rule(features):
return (sign * features[:, j] > threshold).astype(int)
model = LogisticRegression(max_iter=1000).fit(X_train, y_train)
for name, pred in [("rule", rule(X_test)), ("logreg", model.predict(X_test))]:
print(
name,
"balanced accuracy:", round(balanced_accuracy_score(y_test, pred), 3),
"recall:", round(recall_score(y_test, pred), 3),
)Tuning the threshold on the training split keeps the test split clean. If you tuned it on the test set, you would be leaking information and the rule would look better than it is.
Using the existing system as a baseline
To score the current system, record what it predicted for each case in your evaluation set and compute the same metric on those predictions. Three points matter in practice:
- Same cases, same metric. Compare on identical rows. A system measured on last year's traffic and a model measured on this year's cannot be compared.
- Selection bias in labels. If the current system decides which cases get reviewed, you only have true labels for cases it flagged. Its misses look like correct "no" decisions. Collect labels on a random sample to correct for this.
- Operational limits. The old system may run in milliseconds or need no maintenance. Note these constraints now, because the cost trade-offs chapter weighs them against accuracy.
Common errors
| Error | Effect | Fix |
|---|---|---|
| Tuning the rule on the test set | Baseline looks too strong, or the leak carries over to the model | Choose thresholds on training data only |
| Comparing on different rows | Difference reflects data, not method | Score both on one fixed split |
| Ignoring the existing system | Model "wins" against a trivial floor but loses in production | Log and score current decisions |
| Trusting labels the system generated | Its errors are hidden | Label a random sample |
Exercise
A fraud team's current rules engine blocks transactions, and analysts only investigate blocked ones. You train a model on the investigated cases and find it beats the rules engine on that data. Why might this result be misleading, and what would you do?
Show answer
The labels exist mainly for transactions the rules engine already flagged, so fraud it missed is labeled as legitimate or missing. The comparison favors the model on cases the engine chose and hides what the engine misses. Collect true labels on a random sample of all transactions, then score both the engine and the model on that sample with the same metric.