Implementation:Online ml River Linear Model PA
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Classification, Regression |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Passive-Aggressive algorithms are online learning methods that remain passive for correct predictions and aggressively update weights for errors with margin-based constraints.
Description
This module implements both PAClassifier and PARegressor, which use a passive-aggressive update strategy. The algorithms update weights only when predictions violate a margin constraint (loss > 0). Three modes control the aggressiveness: mode 0 (unlimited step), mode 1 (bounded by C), and mode 2 (scaled by C). The update magnitude (tau) is computed based on the loss and feature norm, ensuring weights change just enough to correct the mistake while respecting constraints. PARegressor uses epsilon-insensitive hinge loss for robust regression.
Usage
Use PA algorithms when you need fast online learning with strong theoretical guarantees. PAClassifier works well for binary classification with challenging, noisy data. PARegressor is suitable for regression tasks where you want robustness to outliers through the epsilon parameter. Mode 1 is generally recommended as it provides a good balance between aggressive updates and regularization.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/linear_model/pa.py
Signature
class PARegressor(BasePA, base.Regressor):
def __init__(self, C=1.0, mode=1, eps=0.1, learn_intercept=True):
super().__init__(C=C, mode=mode, learn_intercept=learn_intercept)
self.eps = eps
class PAClassifier(BasePA, base.Classifier):
def __init__(self, C=1.0, mode=1, learn_intercept=True):
super().__init__(C=C, mode=mode, learn_intercept=learn_intercept)
Import
from river import linear_model
I/O Contract
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| C | float | 1.0 | Aggressiveness parameter (higher = more aggressive) |
| mode | int | 1 | Update mode: 0 (unbounded), 1 (bounded), 2 (scaled) |
| eps | float | 0.1 | Epsilon parameter for insensitive loss (regressor only) |
| learn_intercept | bool | True | Whether to learn intercept term |
Attributes
| Attribute | Type | Description |
|---|---|---|
| weights | collections.defaultdict | Weight vector |
| intercept | float | Intercept term |
Input/Output
| Method | Input | Output |
|---|---|---|
| PARegressor.learn_one | x: dict, y: float | None |
| PARegressor.predict_one | x: dict | float |
| PAClassifier.learn_one | x: dict, y: bool | None |
| PAClassifier.predict_proba_one | x: dict | dict |
Usage Examples
# PARegressor Example
from river import linear_model
from river import metrics
from river import stream
import numpy as np
from sklearn import datasets
np.random.seed(1000)
X, y = datasets.make_regression(n_samples=500, n_features=4)
model = linear_model.PARegressor(
C=0.01,
mode=2,
eps=0.1,
learn_intercept=False
)
metric = metrics.MAE() + metrics.MSE()
for xi, yi in stream.iter_array(X, y):
y_pred = model.predict_one(xi)
model.learn_one(xi, yi)
metric.update(yi, y_pred)
print(metric)
# MAE: 9.809402
# MSE: 472.393532
# PAClassifier Example
from sklearn import model_selection
np.random.seed(1000)
X, y = datasets.make_classification(
n_samples=5000,
n_features=4,
n_informative=2,
n_redundant=0,
n_repeated=0,
n_classes=2,
n_clusters_per_class=2
)
X_train, X_test, y_train, y_test = model_selection.train_test_split(
X, y, test_size=0.35, random_state=1000
)
model = linear_model.PAClassifier(C=0.01, mode=1)
for xi, yi in stream.iter_array(X_train, y_train):
model.learn_one(xi, yi)
metric = metrics.Accuracy() + metrics.LogLoss()
for xi, yi in stream.iter_array(X_test, y_test):
metric.update(yi, model.predict_proba_one(xi))
print(metric)
# Accuracy: 88.46%
# LogLoss: 0.325727...