lenskit.metrics.Metric#
- class lenskit.metrics.Metric[L, S: MetricResult]#
Bases:
abc.ABC,lenskit.data.accum.AccumulatorFactory[L,S]Base class for LensKit metrics. Simple metrics that compute a single value for a list should extend
ListMetric.This class defines the interface for metrics. Subclasses should implement the measure_list method to compute metric values.
The summarize() method has a default implementation that computes the mean of the per-list metric values, but subclasses can override it to provide more appropriate summary statistics.
Changed in version 2026.1: Removed the
summarizemethod in favor of requiring metrics to implementAccumulatorFactoryto allow metric-controlled accumulation.- Stability:
- Full (see Stability Levels).
Note
For simplicity in the analysis code, you cannot simply implement the properties of this class on an arbitrary class in order to implement a metric with all available behavior such as labeling and defaults; you must actually extend this class. This requirement may be relaxed in the future.
The default value to impute when computing statistics over missing values. If
None, no imputation is done (necessary for metrics like RMSE, where the missing value is theoretically infinite).- property label: str#
The metric’s default label in output. The base implementation returns the class name by default.
- Return type:
- __str__()#
- abstractmethod measure_list(output, test, /)#
Compute measurements for a single list.
- Returns:
A float for simple metrics
Intermediate data for decomposed metrics
A dict mapping metric names to values for multi-metric classes
- Parameters:
output (lenskit.data.ItemList)
test (lenskit.data.ItemList)
- Return type:
L
- extract_list_metrics(data, /)#
Extract per-list metric(s) from intermediate measurement data.
- Returns:
A float for simple metrics
A dict mapping metric names to values for multi-metric classes
None if no per-list metrics are available
- Parameters:
data (L)
- Return type:
MetricResult | None
- abstractmethod create_accumulator()#
Creaet an accumulator to aggregate per-list measurements into summary metrics.
Each result from
measure_list()is passed toAccumulator.add().- Return type: