DriftAdaptiveClassifier: a substitute on the bench¶
DriftAdaptiveClassifier is not a classifier of its own: it wraps any classifier (here OnlineGaussianNB) with an alarm that watches its mistakes. When the alarm rings, the wrapper starts a fresh copy of the classifier that learns only the new samples, and when that copy proves better than the old one it takes over. So you do not have to know in advance how fast the world changes, which is what the forgetting factor of the naive Bayes asks. The other two classifiers are OnlineGaussianNB and OnlineSoftmaxRegression; the overview is on Online classifiers.
Gamestory¶
London, deep night. Halfway through the chase, the thief completely flips the rules of the game: everything that used to be a sign of a scam is now normal, and vice versa. To avoid being caught off guard, the detective activates DriftAdaptiveClassifier, a team made up of a Starter, an Alarm, and a Substitute.
- 1. The Starter and the Alarm: On the field is the Starter program checking the payments. Next to it is an alarm detector that measures how many mistakes the Starter makes over the last 50 payments (not on a single one, otherwise it would ring for every tiny slip!).
- 2. The siren goes off!: When the thief suddenly changes his trick, the Starter begins making mistakes left and right. The error average shoots up and the Alarm sets off the siren!
- 3. In comes the fresh Substitute: As soon as the alarm rings, the detective sends a brand-new copy of the program (the Substitute) to warm up, studying only the new payments without having its head full of the old wrong rules. As soon as the Substitute proves over the last 50 cases that it is better than the Starter, it takes over command!
In the end, what is DriftAdaptiveClassifier? It is a protective shell that wraps around a normal program: it keeps an eye on its mistakes and, if it realizes the world has changed, trains a fresh substitute on the bench ready to take command!

The figure follows the wrapper through the change at payment 3000. The alarm rings 13 payments after the change and starts the backup; until the backup takes over, at payment 3062, the wrapper answers with the old classifier, which is wrong; from there it is right again. The classifier with alpha=0.1 takes about 200 payments to recover, because its forgetting rate is fixed; the one that never forgets does not recover within the 3000 payments after the change.
A first example: payments that change meaning¶
Half of the payments are fraud. At first a fraud is a large payment (around 400) and a normal one is small (around 60); after 3000 payments they swap. Each payment is first predicted and then learned, and the accuracy is counted before the change, over the 200 payments after it, and late in the stream (payments 5000 to 5900).
import numpy as np
from dense_armor.core.online.drift.detector import CUSUMDriftDetector
from dense_armor.core.online.learn.online_classifiers import (
DriftAdaptiveClassifier,
OnlineGaussianNB,
)
rng = np.random.default_rng(1)
detector = CUSUMDriftDetector(
reference="fixed", radius=20, ref_mult=5, two_sided=False
)
clf = DriftAdaptiveClassifier(
OnlineGaussianNB(), detector, window=50, smooth_window=50
)
hits = []
for i in range(6000):
fraud = int(rng.random() < 0.5)
big = fraud if i < 3000 else 1 - fraud
x = {
"amount": rng.normal(400 if big else 60, 60 if big else 20),
"hour": rng.normal(14, 3),
}
hits.append(clf.predict_one(x) == fraud)
clf.learn_one(x, fraud)
h = np.array(hits, float)
print(
h[500:3000].mean().round(3),
h[3000:3200].mean().round(3),
h[5000:5900].mean().round(3),
)
The wrapper is perfect before the change, right on 68.5 % of the 200 payments after it (the old classifier is in command for the first 62, and the backup after that) and perfect again late in the stream.
How it works¶
DriftAdaptiveClassifier wraps any classifier with a drift detector on the classifier's
own prediction loss. Per sample:
- Predict, then compute the log-loss
−log p(y). - Feed the 50-sample rolling mean of the log-loss to the detector.
- Learn.
- If the detector fires, start a background copy trained only on new samples.
- When the background's accuracy over the last 50 samples beats the main classifier's over the same window, promote the background to main.
The 50-sample rolling mean is not optional in practice: the raw log-loss is near zero on most samples, so its robust scale (MAD) is tiny and a CUSUM fed with the raw value fires on almost every error — hundreds of alarms in 6,000 samples. The rolling mean gives the detector a stable scale.
Repeated alarms¶
After the change the detector rings more than once: in this example it rang 34 times between payment 3014 and payment 3099. A ring while a backup is already running does not restart it: the backup keeps the payments it has already seen, and it takes over as soon as it has window (50) of them and beats the main classifier, here at payment 3062. A first version of the wrapper restarted the backup at every ring; in this example it rang 170 times between payment 3013 and payment 4279, the backup never reached 50 payments without being replaced, and the takeover came only at payment 4276, 1276 payments after the change. That was a defect, and it is fixed.
The parameters¶
| Parameter | Meaning | Default |
|---|---|---|
classifier |
the classifier to wrap; a fresh copy is made for the main and for every backup | (required) |
detector |
a drift detector with update(x) and drift_detected, for example CUSUMDriftDetector |
(required) |
window |
the number of recent samples over which the backup and the main classifier are compared | 50 |
smooth_window |
the number of recent log-losses averaged before they reach the detector | 50 |
Cost, on one CPU core, for a prediction and a learning step of the 6000 payments of the example (best of 3 runs): about 178 µs per payment for the wrapper and 60 µs for the bare naive Bayes, 3 times, because it runs a backup in parallel after an alarm.
API reference¶
DriftAdaptiveClassifier ¶
Bases: Classifier
Classifier with a background copy swapped in on drift detection.
Wraps any Classifier with a drift detector. Per sample:
predict, compute the log-loss -log(p_correct), feed the (smoothed)
log-loss to the detector, learn, and if the detector fires start a
background copy trained only on new samples (a second alarm while a
background copy is running does not restart it, so the copy keeps the
samples it has already seen). Main and background are
both scored prequentially (each predicts a sample before learning it);
when the background's accuracy over the last window samples beats
the main classifier's over the same samples, the background is promoted
to main.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
The wrapped classifier; a fresh copy is trained as the background. |
required | |
detector
|
A drift detector with |
required | |
window
|
int
|
Size of the accuracy sliding window used to compare main and background. |
50
|
smooth_window
|
int
|
Size of the rolling mean applied to the log-loss before feeding it to the detector. The raw log-loss is near zero on most samples, so its robust scale is tiny and every single error looks like a shift; a rolling mean over ~50 samples gives the detector a stable scale. |
50
|
Examples:
>>> from dense_armor.core.online.learn.online_classifiers import (
... OnlineGaussianNB, DriftAdaptiveClassifier,
... )
>>> from dense_armor.core.online.drift.detector import CUSUMDriftDetector
>>> clf = DriftAdaptiveClassifier(
... OnlineGaussianNB(alpha=0.01),
... CUSUMDriftDetector(reference="adaptive", radius=10, ref_mult=3),
... smooth_window=20,
... )
>>> for _ in range(200):
... _ = clf.learn_one({"a": 0.0}, 0)
... _ = clf.learn_one({"a": 1.0}, 1)
>>> clf.predict_one({"a": 0.05})
0
>>> clf.predict_one({"a": 0.95})
1
References
Gama, J., Medas, P., Castillo, G., Rodrigues, P. (2004). Learning with
drift detection. In SBIA. The drift detection method is section 3,
pages 3-4: the error rate p_i is Bernoulli, its standard
deviation is s_i = sqrt(p_i (1 - p_i) / i) (page 3), the minima
are kept while p_i + s_i decreases, the warning level is
p_i + s_i >= p_min + 2 s_min (95%) and the drift level is
p_i + s_i >= p_min + 3 s_min (99%) (page 4).
This class is a variant of DDM, and the difference is declared here:
the monitored statistic is the mean log-loss over the last
smooth_window samples, not the binomial error rate p_i; the
detection itself is delegated to the detector passed in (a
:class:~dense_armor.roles.DriftDetector), so the warning and drift
thresholds are the detector's, not the p_min + 2 s_min and
p_min + 3 s_min of the paper. The hit buffers and the background
swap are this implementation's: when the detector fires, a copy of
the classifier is trained in parallel and replaces the main model if
its recent accuracy is higher.
Source code in dense_armor/core/online/learn/online_classifiers.py
Details¶
learn_one(x, y)takes a dict and a label; every other method is delegated to the main classifier.- The robot version of the same experiment is at the end of Online classifiers.