Online Learning: Continuous Stream Learning

Online Learning : Guide Complet — Apprentissage en Flux Continu

Online Learning: Complete Guide — Continuous Stream Learning

Summary

Online Learning refers to a family of machine learning algorithms capable of updating their parameters with each new data point, never requiring a complete retraining on the full history. Unlike classical methods that require a static dataset and a centralized training phase, an Online Learning algorithm incorporates each observation progressively, making it ideal for real-time data streams, anomaly detection, and environments where data distribution evolves over time (concept drift). In this complete guide, we will explore the mathematical principle of the online Perceptron, the Passive-Aggressive algorithm, the concept of regret bound, and the FTRL approach (Follow-the-Regularized-Leader). We will also see how to implement these algorithms in Python with scikit-learn, study key hyperparameters, and examine four real-world use cases.

Mathematical Principles of Online Learning

The Online Perceptron

The starting point for online learning is the Online Perceptron. At each step t, the model receives an example (x_t, y_t) where x_t is the feature vector and y_t ∈ {-1, +1} is the true label. The model first makes a prediction:

ŷ_t = sign(w_t · x_t)

Then, it checks whether the prediction is correct. If y_t · (w_t · x_t) ≤ 0 (i.e., the example is misclassified), the model updates its weights:

w_{t+1} = w_t + y_t · x_t

Otherwise (y_t · (w_t · x_t) > 0), no update is performed: w_{t+1} = w_t. This is the Passive behavior: if everything is working correctly, the model changes nothing. It preserves acquired knowledge and does not waste computational resources.

The Passive-Aggressive Algorithm

The Passive-Aggressive (PA) algorithm, introduced by Crammer et al. in 2006, generalizes this principle. It introduces a tolerance margin and adapts the update intensity based on the error. The principle is: w_{t+1} = w_t + τ · y_t · x_t where:

  • τ = loss_t / ||x_t||²
  • loss_t = max(0, 1 – y_t · (w_t · x_t))

The loss loss_t measures how much the current prediction respects the desired margin. If the example is correctly classified with sufficient margin (y_t · (w_t · x_t) ≥ 1), the loss is zero and τ = 0: the model stays Passive. On the other hand, if the margin is violated, the model becomes Aggressive: it modifies its weights proportionally to the committed error, with a step size τ automatically adjusted according to the input vector norm.

There are three main variants of the Passive-Aggressive algorithm:

  • PA-0: the basic version described above, with no bound on the learning step.
  • PA-I: introduces a regularization parameter C that bounds the step τ ≤ C, preventing overly aggressive updates.
  • PA-II: modifies the optimization objective to include an L2 regularization term on the update.

The Regret Bound

In online learning theory, the central metric is the regret bound. The regret measures the difference between the total number of errors committed by the online algorithm over T examples, and the number of errors of the best fixed classifier possible on the same data:

Regret(T) = Σ_{t=1}^{T} ℓ_t(w_t) – min_w Σ_{t=1}^{T} ℓ_t(w)

where ℓ_t(w) is the loss function at time t. A good online learning algorithm guarantees that Regret(T) ≤ O(√T), meaning that the average regret per example converges toward zero as T increases. In other words, in the long run, the online algorithm performs as well as the best fixed classifier in hindsight.

For the online Perceptron, the classical bound is:

(Number of errors) ≤ (R² · ||w*||²) / γ²

where R is the maximum radius of the data, w is the optimal separator, and γ is the geometric margin. This bound does not depend on the total number of examples T*, which is remarkable.

FTRL (Follow-the-Regularized-Leader)

For problems at very very large scale (billions of features, as in online advertising or text filtering), the reference algorithm is FTRL (Follow-the-Regularized-Leader). Instead of updating the model at each step in isolation, FTL chooses at each iteration the weight that minimizes the sum of past losses plus a regularization term:

w_{t+1} = argmin_w [ Σ_{s=1}^{t} ℓ_s(w) + R(w) ]

where R(w) is a regularization term, often a combination of L1 and L2 norms. L1 regularization encourages sparsity (many weights exactly zero), which is crucial when the number of features is colossal. FTRL is used in production by Google for Click-Through Rate (CTR) prediction on models with hundreds of billions of parameters.

Intuition: The Doctor Who Refines Their Diagnosis

Online learning is like a doctor who refines their diagnosis with each new patient.

Imagine a young doctor fresh out of medical school. Their first instinct would be to review all their textbooks between each consultation to recalibrate all their knowledge — that would obviously be impossible in practice. Instead, the doctor adjusts their understanding progressively. They accumulate clinical experience with each encounter.

  • Passive mode: when a patient arrives with a common cold, the symptoms correspond exactly to what the doctor already knows. They don’t need to change anything in their mental model. Their diagnosis is good, their prescription is appropriate, they continue. This is the Passive-Aggressive behavior in its Passive phase: if the error is zero (the prediction is correct), the weight doesn’t move.
  • Aggressive mode: when a patient surprises them — an unusual symptom, an unexpected reaction to treatment — the doctor corrects their mental model. They think: “Ah, I hadn’t considered that possibility before.” The correction is proportional to the surprise: the more unexpected the case, the larger the update. This is exactly the mechanism of τ = loss / ||x||² in the Passive-Aggressive algorithm.

Where batch learning would consist of re-studying all medical textbooks after each new patient (extremely costly and unnecessary), Online Learning allows the doctor to grow continuously while remaining operational. This is the way learning naturally occurs in living beings: progressive, incremental, and adaptive.

Python Implementation with scikit-learn

SGDClassifier with partial_fit on simulated data streams

The scikit-learn library offers a practical implementation of online learning via SGDClassifier. The partial_fit() method allows updating the model in mini-batches, which simulates a real data stream.

Batch vs Online Comparison: Code Example

Let’s compare the performance of a batch classifier (trained on all data at once) with an online classifier (incremental update). The following code illustrates this comparison on a synthetic dataset.

import numpy as np
from sklearn.linear_model import SGDClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt

# 1. Generate a synthetic dataset
X, y = make_classification(
    n_samples=10000, n_features=20,
    n_informative=10, n_redundant=5,
    random_state=42, flip_y=0.05
)

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# 2. BATCH approach: classical training
batch_clf = SGDClassifier(
    loss='hinge', penalty='l2', alpha=0.0001,
    random_state=42, max_iter=1000, tol=1e-3
)
batch_clf.fit(X_train, y_train)
batch_accuracy = accuracy_score(y_test, batch_clf.predict(X_test))
print(f"Batch Accuracy: {batch_accuracy:.4f}")

# 3. ONLINE approach: partial_fit by mini-batches
online_clf = SGDClassifier(
    loss='hinge', penalty='l2', alpha=0.0001,
    random_state=42
)
classes = np.unique(y_train)
chunk_size = 100
online_accuracies = []

for start in range(0, len(X_train), chunk_size):
    end = start + chunk_size
    X_chunk = X_train[start:end]
    y_chunk = y_train[start:end]
    online_clf.partial_fit(X_chunk, y_chunk, classes=classes)
    acc = accuracy_score(y_test, online_clf.predict(X_test))
    online_accuracies.append(acc)

final_online_accuracy = online_accuracies[-1]
print(f"Online Accuracy: {final_online_accuracy:.4f}")

# 4. Convergence visualization
plt.figure(figsize=(10, 6))
plt.plot(range(len(online_accuracies)), online_accuracies,
         label='Online Learning (partial_fit)', color='blue', linewidth=2)
plt.axhline(y=batch_accuracy, color='red', linestyle='--',
            label=f'Batch Learning ({batch_accuracy:.3f})')
plt.xlabel('Number of mini-batches processed')
plt.ylabel('Accuracy on test set')
plt.title('Convergence: Online Learning vs Batch Learning')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()

The convergence graph typically shows that Online Learning progressively catches up to the performance of batch learning. After seeing enough examples, both approaches reach comparable accuracy levels. The decisive advantage of the online approach: it works without ever loading the entire dataset into memory.

Concept Drift Detection with ADWIN

Concept drift refers to a change in the underlying data distribution over time. The ADWIN algorithm (Adaptive Windowing) automatically detects these changes by monitoring the mean of a sliding window.

import numpy as np
from collections import deque

class ADWINDetector:
    """Simplified implementation of the ADWIN detector for drift
    detection in an Online Learning data stream."""

    def __init__(self, delta=0.002, min_window=10):
        self.delta = delta
        self.min_window = min_window
        self.window = deque()
        self.drift_detected = False
        self.change_point = None

    def _hoeffding_bound(self, n):
        """Hoeffding bound for change detection."""
        return np.sqrt(np.log(2.0 / self.delta) / (2.0 * n))

    def add_element(self, value):
        """Adds a new element and checks whether drift is detected."""
        self.window.append(value)

        if len(self.window) < 2 * self.min_window:
            return False

        # Test different sub-window sizes
        n = len(self.window)
        for cut in range(self.min_window, n - self.min_window):
            W0 = list(self.window)[:cut]
            W1 = list(self.window)[cut:]

            mu0 = np.mean(W0)
            mu1 = np.mean(W1)
            eps = self._hoeffding_bound(cut) + self._hoeffding_bound(n - cut)

            if abs(mu0 - mu1) > eps:
                # Drift detected: reduce the window
                self.window = deque(list(self.window)[cut:])
                self.drift_detected = True
                self.change_point = len(self.window)
                return True

        self.drift_detected = False
        return False

# Concept drift simulation
np.random.seed(0)
detector = ADWINDetector(delta=0.01)
stream = []
drifts_notified = []

for t in range(500):
    if t < 250:
        # Distribution A: mean 0.3
        value = np.random.normal(loc=0.3, scale=0.1)
    else:
        # Distribution B: mean 0.7 (drift at point 250)
        value = np.random.normal(loc=0.7, scale=0.1)

    stream.append(value)
    if detector.add_element(value):
        drifts_notified.append(t)

print(f"Drift detected at time: {drifts_notified}")
print(f"True drift point: 250")

When concept drift is detected, an Online Learning algorithm can partially reset its weights or temporarily increase its learning rate to adapt more quickly to the new distribution.

Key Hyperparameters

Proper hyperparameter tuning is critical for Online Learning algorithm performance. Here are the main ones:

learning_rate

The learning rate controls the size of each update. In SGDClassifier, three strategies are available:

  • constant: η(t) = eta0 (fixed rate, requires good manual choice)
  • optimal: η(t) = 1 / (alpha * (t + t_0)) (recommended by default)
  • invscaling: η(t) = eta0 / pow(t, power_t) (gentle decay)

In general, a rate that decreases over time is preferable: at the beginning, the model knows nothing and must learn quickly; progressively, it refines its knowledge with smaller and smaller steps.

loss

The loss function determines the optimization objective:

  • hinge: linear SVM loss (maximum margin). Closest to the Passive-Aggressive algorithm.
  • log_loss: logistic regression with calibrated probabilities.
  • modified_huber: smooth variant of hinge, tolerant of outliers.
  • squared_hinge: squared hinge loss, penalizes margin violations more heavily.

penalty

Regularization prevents overfitting and controls model complexity:

  • l2 (Ridge): penalizes large weights, favors small but non-zero weights.
  • l1 (Lasso): encourages sparsity, sets some weights exactly to zero.
  • elasticnet: weighted combination of L1 and L2.

alpha

The alpha parameter controls the strength of regularization. A value that is too low leads to overfitting (the model memorizes noise), while a value that is too high prevents learning (the model stays too simple). Typical values lie between 10⁻⁶ and 10⁻³.

Advantages and Limitations of Online Learning

Advantages

  1. Memory efficiency: the model never stores the complete history of data. Memory usage is independent of the total number of observations, allowing infinite data streams to be processed with constant memory footprint.
  2. Real-time adaptability: the algorithm can adapt immediately to changes in data distribution. If user preferences evolve, if fraud patterns change, the model follows without manual retraining.
  3. Quick startup: for applications where the first predictions are critical, Online Learning starts learning from the very first example. No need to wait for a complete dataset.
  4. Scalability: in distributed environments, each node can process its own data stream and models can be aggregated periodically. This is the architecture behind Netflix’s recommendation systems and Google’s advertising engines.
  5. Concept drift compatibility: with detectors like ADWIN, Online Learning can not only adapt to drift but also alert operators when a structural change occurs.

Limitations

  1. Sensitivity to noise: an aberrant outlier can significantly degrade the model if the learning step is not properly regularized. Robustness mechanisms (such as PA-I with bound C) are necessary.
  2. Order dependence: unlike batch learning, results may vary depending on the order in which examples arrive. Non-random ordering of the stream can bias learning.
  3. Difficult debugging: without access to the complete data history, diagnosing a performance regression is more complex. Traceability of decisions requires meticulous logging.
  4. No convergence guarantee on non-separable data: if the data is not linearly separable (even approximately), the online Perceptron may oscillate indefinitely without converging to a stable optimum.
  5. Continuous evaluation: unlike batch learning where a fixed validation set is available, evaluating online performance requires specific protocols such as prequential evaluation (predict then learn, in a loop).

Four Real-World Use Cases

1. Real-Time Banking Fraud Detection

Fraudulent transactions evolve constantly: fraudsters adapt their techniques as soon as a detection pattern is identified. An Online Learning classifier (typically Passive-Aggressive or FTRL) analyzes each transaction as it occurs. If an unusual transaction is identified, the model immediately incorporates this information. When a new fraud pattern appears, the model adapts within minutes rather than weeks (as with nightly batch retraining). Banks like Capital One and Stripe use these approaches to protect billions of daily transactions.

2. Spam Filtering and Text Classification

Spam evolves over time: spammers change their keywords, their obfuscation techniques, their sending domains. An Online Learning classifier processes each email on arrival. If an email initially classified as legitimate turns out to be spam (flagged by the user), the model updates its weights immediately. The partial_fit method from scikit-learn is perfectly suited to this scenario: each batch of 100 or 1,000 emails constitutes an incremental classifier update. Gmail uses similar techniques at very large scale.

3. Adaptive Recommendation Systems

User preferences change: a user interested in action movies in January might prefer romantic comedies in summer. A recommendation system based on Online Learning continuously updates each user’s profile with every interaction (click, viewing, purchase, abandonment). Google’s FTRL-PROX algorithm, used for CTR (Click-Through Rate) prediction, processes hundreds of thousands of queries per second with models of several billion parameters, only updating weights corresponding to activated features.

4. IoT and Predictive Maintenance

In a connected industrial environment, sensors continuously transmit measurements of temperature, vibration, pressure, etc. An Online Learning model can detect a progressive drift in signals in real time before a failure occurs. If a machine’s behavior changes (wear, change in raw material, ambient variation), the model detects the concept drift via ADWIN and adapts automatically. This avoids costly production shutdowns while maintaining high prediction accuracy, without human intervention or scheduled retraining.

Conclusion

Online Learning is a powerful and elegant machine learning approach that allows models to grow continuously from data streams, without costly retraining. Solid theoretical foundations — the online Perceptron, the Passive-Aggressive algorithm, regret bounds, and FTRL — guarantee that these methods are not just practical heuristics, but algorithms with proven convergence guarantees. Implementation with SGDClassifier and partial_fit in scikit-learn makes these techniques accessible to every practitioner. Whether for fraud detection, spam filtering, personalized recommendations, or predictive maintenance, Online Learning offers a robust framework for building intelligent systems that learn their entire lives, just like living beings.


See Also

About Salah YAHIAOUI

Salah YAHIAOUI publie sur The Pingouin des tutoriels Python et des décryptages sur l’intelligence artificielle. Les cas pratiques récents précisent leurs données, leurs tests et leurs limites. À propos, méthode éditoriale et contact.

View all posts by Salah YAHIAOUI →