Implementation:Online ml River Stats Mean
| Knowledge Sources | |
|---|---|
| Domains | Online_Learning, Statistics |
| Last Updated | 2026-02-08 16:00 GMT |
Overview
Mean computes the running arithmetic mean of a data stream using Welford's algorithm.
Description
This statistic calculates the average value incrementally as data arrives, maintaining numerical stability through Welford's method. It supports weighted observations and includes a revert method for use with rolling windows. The implementation also provides batch update capabilities via update_many for efficient processing of numpy arrays. It can be combined with other statistics and supports parallel updates.
Usage
Use Mean when you need to track the average value in streaming data. This is one of the most fundamental statistics used in machine learning and data analysis. It's essential for normalization, baseline calculations, monitoring system metrics, and as a building block for more complex statistics like variance and covariance.
Code Reference
Source Location
- Repository: Online_ml_River
- File: river/stats/mean.py
Signature
class Mean(stats.base.Univariate):
def __init__(self):
self.n = 0
self._mean = 0.0
class BayesianMean(stats.base.Univariate):
def __init__(self, prior: float, prior_weight: float):
self.prior = prior
self.prior_weight = prior_weight
self._mean = Mean()
Import
from river import stats
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| x | numbers.Number | Yes | Value to update the statistic with |
| w | float | No | Weight for the observation (default: 1.0) |
Outputs
| Name | Type | Description |
|---|---|---|
| get() | float | Current mean value |
Usage Examples
from river import stats
# Basic running mean
X = [-5, -3, -1, 1, 3, 5]
mean = stats.Mean()
for x in X:
mean.update(x)
print(f"Value: {x}, Mean: {mean.get():.1f}")
# Output:
# Value: -5, Mean: -5.0
# Value: -3, Mean: -4.0
# Value: -1, Mean: -3.0
# Value: 1, Mean: -2.0
# Value: 3, Mean: -1.0
# Value: 5, Mean: 0.0
# Rolling mean using utils.Rolling
from river import utils
X = [1, 2, 3, 4, 5, 6]
rmean = utils.Rolling(stats.Mean(), window_size=2)
for x in X:
rmean.update(x)
print(f"Value: {x}, Rolling Mean: {rmean.get():.1f}")
# Output:
# Value: 1, Rolling Mean: 1.0
# Value: 2, Rolling Mean: 1.5
# Value: 3, Rolling Mean: 2.5
# Value: 4, Rolling Mean: 3.5
# Value: 5, Rolling Mean: 4.5
# Value: 6, Rolling Mean: 5.5
# Weighted mean
weighted_mean = stats.Mean()
weighted_mean.update(10, w=1.0)
weighted_mean.update(20, w=2.0)
weighted_mean.update(30, w=3.0)
print(f"Weighted mean: {weighted_mean.get():.2f}")
# Output: 23.33 (weighted towards higher values)
# Bayesian mean with prior
bayes_mean = stats.BayesianMean(prior=5.0, prior_weight=10)
for x in [1, 2, 3]:
bayes_mean.update(x)
print(f"Bayesian mean: {bayes_mean.get():.2f}")
# Output incorporates prior belief