Implementation:Online ml River Active Base
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Active_Learning, Classification |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Base class for active learning classifiers that wraps classifiers to selectively request labels based on uncertainty or other criteria.
Description
ActiveLearningClassifier is an abstract base class that wraps any classifier and adds label querying logic. It extends both Wrapper and Classifier interfaces, delegating classification to the wrapped model while controlling when labels are requested through the abstract _ask_for_label method. The predict_one and predict_proba_one methods return both predictions and a boolean indicating whether a label is needed. This enables active learning scenarios where labeling is expensive and should be done selectively.
Usage
Use this base class when implementing custom active learning strategies. Subclass it and implement _ask_for_label to define your label selection criteria. The framework handles prediction and learning, while you focus on the label selection logic.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/active/base.py
Signature
class ActiveLearningClassifier(base.Wrapper, base.Classifier):
def __init__(self, classifier: base.Classifier, seed: int | None = None):
...
@abc.abstractmethod
def _ask_for_label(self, x, y_pred) -> bool:
...
def predict_proba_one(self, x, **kwargs):
"""Returns (y_pred, ask_for_label)"""
...
def predict_one(self, x, **kwargs):
"""Returns (y_pred, ask_for_label)"""
...
Import
from river import active
I/O Contract
| Method | Input | Output |
|---|---|---|
| predict_proba_one(x) | Feature dictionary | (probability dict, bool) |
| predict_one(x) | Feature dictionary | (label, bool) |
| learn_one(x, y) | Feature dict, label | None |
Usage Examples
from river import active
from river import base
import random
# Implement custom active learning strategy
class RandomSampler(active.base.ActiveLearningClassifier):
def __init__(self, classifier, p=0.1, seed=None):
super().__init__(classifier, seed=seed)
self.p = p
def _ask_for_label(self, x, y_pred):
# Randomly request labels with probability p
return self._rng.random() < self.p
# Use with any classifier
from river import linear_model
from river import datasets
model = RandomSampler(
classifier=linear_model.LogisticRegression(),
p=0.2,
seed=42
)
n_labels_requested = 0
for x, y in datasets.Phishing():
y_pred, ask = model.predict_one(x)
if ask:
n_labels_requested += 1
model.learn_one(x, y)
if n_labels_requested >= 100:
break
print(f"Labels requested: {n_labels_requested}")