lenskit.metrics.Metric#

class lenskit.metrics.Metric#

Bases: ABC

Base class for LensKit metrics. Individual metrics need to implement a sub-interface, such as ListMetric and/or GlobalMetric.

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.

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).

__init__()#

Methods

__init__()

extract_list_metrics(data, /)

Extract per-list metric(s) from intermediate measurement data.

measure_list(output, test, /)

Compute measurements for a single list.

summarize(values, /)

Aggregate intermediate values into summary statistics.

Attributes

label

The metric's default label in output.

property label: str#

The metric’s default label in output. The base implementation returns the class name by default.

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:
Return type:

Any

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 (Any)

Return type:

float | dict[str, float] | None

abstractmethod summarize(values, /)#

Aggregate intermediate values into summary statistics.

Returns:

A dictionary of summary statistics.

Parameters:

values (list[Any] | Array | ChunkedArray)

Return type:

dict[str, float]