Overview
The Base class is the foundational superclass inherited by the majority of classes in River, providing uniform parameter handling, object representation, cloning, and mutation capabilities.
Description
The Base class serves as the root of River's object hierarchy, implementing core functionality that all River estimators and components need. It provides methods for getting and setting parameters through introspection, creating clones of objects with optional parameter overrides, mutating attributes safely, checking for stochastic behavior, computing memory usage, and generating consistent string representations. The class uses Python's inspect module to examine class signatures and automatically extract parameters, enabling seamless serialization and cloning operations throughout the library.
Usage
Use Base as the parent class when creating new River components that need standard parameter management, cloning, mutation, and representation capabilities. It is typically inherited alongside more specific base classes like Estimator, Transformer, or Classifier to provide both foundational and domain-specific functionality.
Code Reference
Source Location
Signature
class Base:
"""Base class that is inherited by the majority of classes in River."""
def __str__(self) -> str
def __repr__(self) -> str
@classmethod
def _unit_test_params(cls) -> collections.abc.Iterator[dict[str, typing.Any]]
def _get_params(self) -> dict[str, typing.Any]
def clone(
self,
new_params: dict[str, typing.Any] | None = None,
include_attributes: bool = False
) -> typing.Self
@property
def _mutable_attributes(self) -> set[str]
def mutate(self, new_attrs: dict[str, typing.Any]) -> None
@property
def _is_stochastic(self) -> bool
@property
def _raw_memory_usage(self) -> int
@property
def _memory_usage(self) -> str
Import
from river.base import Base
I/O Contract
clone Method
| Parameter |
Type |
Description
|
| new_params |
None |
Optional dictionary of parameters to override in the clone
|
| include_attributes |
bool |
Whether to clone attributes not in the class signature (default: False)
|
| Returns |
Type |
Description
|
| clone |
typing.Self |
A fresh instance with the same parameters but no learned state
|
mutate Method
| Parameter |
Type |
Description
|
| new_attrs |
dict[str, Any] |
Dictionary of attribute names and new values to set
|
_get_params Method
| Returns |
Type |
Description
|
| params |
dict[str, Any] |
Dictionary mapping parameter names to their current values
|
Usage Examples
from river import linear_model
from river import optim
# Create a model
model = linear_model.LinearRegression(
optimizer=optim.SGD(lr=0.042),
)
# Clone with new parameters
new_params = {
'optimizer': optim.SGD(0.001)
}
cloned_model = model.clone(new_params)
# Mutate existing model
model.mutate({
'optimizer': {'lr': optim.schedulers.Constant(0.001)}
})
# Check if model is stochastic
print(model._is_stochastic) # False if all seeds are set
# Get memory usage
print(model._memory_usage) # Human-readable string like "2.4 KB"
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.