Online Learning
What if your data never stops?
A fraud model cannot wait forever for the next full retraining cycle.
Imagine a bank receiving thousands of new transactions every minute. Customer behaviour changes, new fraud patterns appear, and fresh labelled examples keep arriving. Retraining the entire model from zero after every small change would be wasteful.
Online learning gives us another option: keep the model's current knowledge and update it a little as new information arrives.
Most introductory Machine Learning examples use batch learning: collect a dataset, train a model, deploy it, and retrain later. That approach is often the simplest and safest choice.
Online learning, also called incremental learning, is different. The model can continue learning from new labelled examples or mini-batches without throwing away everything it has already learned.
The core loop
Important idea
Online learning does not mean that every prediction system must retrain after every single event. Updates may happen one sample at a time or in small mini-batches. The defining idea is that the model can continue learning from new data without starting from zero each time.
The Big Idea: Do Not Start From Zero
Suppose a model has already learned from the first 1,000 observations. Now 100 new labelled observations arrive. A full batch retraining process may train again using all 1,100 observations. An incremental learner can instead start from the model it already has and update it using the new information.
Full retraining idea
Incremental idea
Mental model: online learning is usually about old model state + new data → updated model state. The algorithm remembers what it has already learned and adjusts from there.
Batch Learning vs Online Learning
Batch Learning
Typical flow: collect data → train → deploy → retrain later.
The model normally stays fixed between retraining cycles.
Often simpler to reproduce, validate, and govern.
Online Learning
Typical flow: receive new data → predict → update → continue.
The model can adapt incrementally as labelled data becomes available.
Useful for streams, very large datasets, or changing environments.
| Question | Batch | Online / Incremental |
|---|---|---|
| How is the model updated? | Periodic retraining | Incremental updates |
| Must all data fit in memory? | Not always, but commonly assumed | No; can process small chunks |
| Can it react quickly to change? | Depends on retraining frequency | Potentially, if feedback arrives quickly |
| Operational complexity | Usually lower | Usually higher |
Do Not Confuse These Terms
Online learning
The model itself is updated incrementally as new labelled examples or feedback become available.
Streaming inference
Predictions are made on a live stream, but the model may remain completely fixed. Streaming prediction is not automatically online learning.
Out-of-core learning
Data is processed in chunks because the full dataset is too large for RAM. The dataset can be historical and fixed; it does not have to be a live stream.
Reinforcement learning
An agent chooses actions and learns from rewards. It is a different learning paradigm, even though learning may also happen sequentially.
A Tiny Numerical Example: Updating Without Recalculating Everything
Before updating a Machine Learning model, look at the same idea using something familiar: a running mean. Suppose we have already seen the values 10, 20, 30. Their mean is 20. Now a new value, 40, arrives.
Old mean
(10 + 20 + 30) / 3 = 20
Incremental update
new mean = old mean + (new value - old mean) / new count
= 20 + (40 - 20) / 4
= 25
We reached the same mean as (10 + 20 + 30 + 40) / 4, but the update rule only needed the previous state, the new value, and the count. Incremental Machine Learning algorithms use the same broad principle, although their state and update rules are more sophisticated.
From Intuition to Model Updates
Many incremental linear models can be understood using a Stochastic Gradient Descent style update:
wnew = wold − η × gradient
model parameter
learning rate
direction of local loss change
Suppose the current weight is 0.50, the learning rate is 0.10, and the gradient from the new example is −0.40.
w_new = 0.50 − 0.10 × (−0.40)
w_new = 0.50 + 0.04
w_new = 0.54
The model has changed a little after seeing new information. Online learning repeats this type of update many times. The exact update depends on the algorithm and its loss function.
How Often Can We Update?
One sample at a time
Update after every labelled observation. This can adapt quickly but may react strongly to noisy observations.
Mini-batch updates
Collect a small chunk such as 32, 100, or 1000 observations and update once per chunk. This is often more computationally convenient.
Periodic incremental updates
Update every few minutes, hours, or days without retraining from scratch. The right cadence depends on feedback delay and business needs.
See partial_fit() in the Simplest Possible Way
Scikit-learn's StandardScaler can update its running mean and variance with partial_fit(). This gives us a clean bridge from the running-mean intuition to a real API.
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
batch_1 = [[10], [20], [30]]
scaler.partial_fit(batch_1)
print(scaler.mean_)
batch_2 = [[40], [50]]
scaler.partial_fit(batch_2)
print(scaler.mean_)Output
[20.]
[30.]Why does the mean become 30?
(10 + 20 + 30 + 40 + 50) / 5 = 30
The second call updated the scaler's existing statistics instead of forgetting the first batch.
Repeated fit()
Conceptually starts a new fitting procedure and generally replaces previously learned estimator state.
partial_fit()
Continues incremental learning from the estimator's current state using the next sample or mini-batch.
Python: Incremental Learning with partial_fit()
Scikit-learn provides several estimators with a partial_fit() method. Each call updates an already-existing model using the supplied mini-batch instead of restarting training from the beginning.
For classifiers such as SGDClassifier, the complete set of possible classes must be supplied on the first partial_fit() call. Later calls can omit it.
The example below uses an initial 200-sample warm-up period. The scaler is fitted only on that historical chunk and then frozen. The remaining 1000 samples arrive in 100-sample batches.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.linear_model import SGDClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
X, y = make_classification(
n_samples=1200,
n_features=10,
n_informative=6,
n_redundant=2,
class_sep=1.2,
flip_y=0.03,
random_state=42,
)
warmup = 200
batch_size = 100
classes = np.array([0, 1])
# Use only historical data to learn scaling statistics.
scaler = StandardScaler().fit(X[:warmup])
model = SGDClassifier(
loss="log_loss",
learning_rate="constant",
eta0=0.01,
alpha=0.0001,
random_state=42,
)
# First incremental update.
X0 = scaler.transform(X[:warmup])
model.partial_fit(X0, y[:warmup], classes=classes)
correct = 0
seen = 0
batch_scores = []
for start in range(warmup, len(X), batch_size):
stop = min(start + batch_size, len(X))
X_batch = scaler.transform(X[start:stop])
y_batch = y[start:stop]
# 1. TEST FIRST: predict before learning this batch.
y_pred = model.predict(X_batch)
batch_scores.append(accuracy_score(y_batch, y_pred))
correct += (y_pred == y_batch).sum()
seen += len(y_batch)
# 2. THEN TRAIN on the newly labelled batch.
model.partial_fit(X_batch, y_batch)
print("Warm-up samples:", warmup)
print("Streamed samples:", seen)
print("First 3 batch accuracies:", [round(s, 3) for s in batch_scores[:3]])
print("Prequential accuracy:", round(correct / seen, 3))Expected output
Warm-up samples: 200
Streamed samples: 1000
First 3 batch accuracies: [0.8, 0.8, 0.82]
Prequential accuracy: 0.811Spot the Bug: Which Stream Evaluation Is Honest?
Pipeline A — misleading
The model has already seen the answers before you measure it.
Pipeline B — preferred
This tests the model state that actually existed before learning from the new batch.
Why predict before learning?
If we train on a batch and then score that same batch, the result is partly a training score. In a stream, a more realistic approach is often test-then-train (also called prequential evaluation): predict using the model state available at that moment, observe the true label when it arrives, measure the error, and only then update the model.
Prequential pattern
Preprocessing Must Be Stream-Safe Too
Incremental learning is not only about the final classifier. Any preprocessing step that learns statistics from data must also respect time/order.
Risky
scaler.fit(X_all)
# scaler has seen future stream dataSafer idea
scaler.fit(X_initial_history)
# future batches remain unseenScikit-learn's StandardScaler itself supports partial_fit() for online mean/variance updates. However, if both the feature transformation and the model are changing over time, you must design the update order carefully so that evaluation does not use future information and the model sees a consistent feature representation. Dedicated streaming libraries can make this workflow easier.
Concept Drift: When the World Changes
Online learning is especially interesting when the data-generating process changes. But the word drift covers several different situations.
| Change | Simple meaning | Example |
|---|---|---|
| Data / covariate drift | The distribution of inputs changes | Customers now use newer devices |
| Class-prior shift | How common each class is changes | Fraud becomes more frequent |
| Concept drift | The relationship between inputs and target changes | Patterns that indicated fraud last year no longer mean the same thing |
Online learning does not automatically solve drift. If labels arrive slowly, updates are noisy, the chosen model cannot represent the new pattern, or harmful data enters the stream, the model can still perform poorly. Drift should be monitored rather than assumed to be fixed by continuous updates.
A simple drift story
Algorithms That Can Learn Incrementally
Not every Scikit-learn estimator supports incremental training. A practical clue is whether the estimator exposes a partial_fit() method.
| Task | Examples | Why useful |
|---|---|---|
| Classification | SGDClassifier, Perceptron, MultinomialNB | Can update from successive mini-batches |
| Regression | SGDRegressor | Incremental linear regression-style learning |
| Clustering | MiniBatchKMeans | Processes small batches instead of all rows at once |
| Neural networks | MLPClassifier, MLPRegressor | Scikit-learn also exposes incremental updates for these estimators |
What about trees and drift detectors?
Streaming-focused libraries such as River include methods designed specifically for evolving streams, including Hoeffding-style trees and drift detectors such as ADWIN. These are useful advanced topics, but they are not required to understand the core idea of incremental learning.
Learning Rate Matters
For SGD-style online models, the learning rate controls how strongly new data changes the model.
Too large
Updates may jump around and react too strongly to individual batches.
Reasonable
The model can learn while remaining reasonably stable.
Too small
Adaptation may be so slow that the model cannot keep up with meaningful change.
Scikit-learn's SGDClassifier supports several learning-rate schedules such as constant, optimal, invscaling, and adaptive. These schedules are not the same thing as algorithms such as AdaGrad or Adam.
Where Online Learning Can Be Useful
Fraud / abuse detection
New labelled transactions or moderation decisions can be incorporated incrementally when feedback is available.
Spam and text filtering
Vocabulary and message patterns can evolve, making incremental updates useful.
Recommendation / ranking signals
Recent clicks, views, and preferences may carry useful information about current behaviour.
IoT and operational monitoring
Continuous sensor streams may be processed without keeping the entire history in memory.
These are possible applications, not claims that every production system in these domains uses online learning. Many real systems deliberately use scheduled batch retraining, hybrid architectures, or human approval before model updates.
Benefits and Risks
Potential benefits
- Can process data in small chunks.
- May adapt faster than infrequent full retraining.
- Useful when the dataset is larger than available memory.
- Can incorporate recent labelled feedback continuously.
Important risks
- Bad labels or corrupted data can change the model immediately.
- Recent observations can be over-emphasized.
- Evaluation and rollback are more operationally difficult.
- Adversarial or poisoned inputs can be especially dangerous.
When Should You Choose Online Learning?
Online learning is worth considering when...
- data arrives continuously or is too large for memory,
- recent labelled feedback should affect the model quickly,
- full retraining is expensive,
- the environment may change over time, and
- you can monitor, validate, and roll back updates safely.
Batch learning may be simpler when...
- the environment changes slowly,
- labels arrive only after long delays,
- periodic retraining is inexpensive enough,
- strict reproducibility/governance is important, or
- the chosen model does not support safe incremental updates.
Common Mistakes
“A model making real-time predictions is automatically online learning.”
No. The model may make streaming predictions while remaining completely fixed.
“Online learning means one record at a time.”
Not necessarily. Mini-batch incremental updates are also common.
“Online learning automatically fixes concept drift.”
No. Adaptation depends on feedback, model capacity, learning rate, data quality, and monitoring.
“I can preprocess the whole stream first.”
That may leak future information. Preprocessing must respect the same chronology as model training.
“More recent data should always dominate old data.”
That is a modeling choice, not a universal law. Some old patterns remain important.
“partial_fit() is equivalent to calling fit() repeatedly.”
No. partial_fit continues incremental training, while fit() generally represents a new fitting procedure and may reset learned state.
Check Your Understanding
1. A recommendation system makes predictions every second, but its model is retrained only once a week. Is this online learning?
No. It is streaming or real-time inference, but the model itself is not being updated incrementally.
2. Why should a new labelled batch usually be scored before partial_fit()?
Because otherwise the model has already learned from that batch, making the score partly a training score rather than an honest test of the previous model state.
3. Does online learning have to update after every individual row?
No. Updates can happen one sample at a time, in mini-batches, or periodically while still continuing from the current model state.
Quick Recap
What is the central idea of online learning?
The model can continue updating incrementally as new labelled data arrives instead of always retraining from scratch.
Why is “predict first, then learn” useful?
It evaluates the model using the state that actually existed before the new labelled example or batch was incorporated.
Does online learning guarantee adaptation to concept drift?
No. Online updates create the possibility of adaptation, but successful adaptation still depends on feedback speed, algorithm choice, data quality, hyperparameters, and monitoring.
Final Takeaway
Online learning is incremental model updating. It is valuable when data or environments evolve, when datasets are too large for memory, or when waiting for full retraining is undesirable. But it adds operational risk: the model can learn from bad data just as quickly as it can learn from useful data. Good online systems therefore combine incremental learning with careful evaluation, drift monitoring, data-quality checks, and rollback strategies.