Skip to content

top_k

logger = logging.getLogger(__name__) module-attribute

MetricTopK

Bases: Metric

Base class for all metrics computed based on the Top-K recommendations for every user.

A MetricTopK object is stateful, i.e. after calculate the results can be retrieved in one of two ways: - Detailed results are stored in :attr:results, - Aggregated result value can be retrieved using :attr:value

:param K: Size of the recommendation list consisting of the Top-K item predictions. :type K: int

Source code in src/recnexteval/metrics/core/top_k.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class MetricTopK(Metric):
    """Base class for all metrics computed based on the Top-K recommendations for every user.

    A MetricTopK object is stateful, i.e. after `calculate`
    the results can be retrieved in one of two ways:
      - Detailed results are stored in :attr:`results`,
      - Aggregated result value can be retrieved using :attr:`value`

    :param K: Size of the recommendation list consisting of the Top-K item predictions.
    :type K: int
    """

    def __init__(
        self,
        user_id_sequence_array: np.ndarray,
        user_item_shape: tuple[int, int],
        timestamp_limit: None | int = None,
        K: int = 10,
    ) -> None:
        super().__init__(
            user_id_sequence_array=user_id_sequence_array,
            user_item_shape=user_item_shape,
            timestamp_limit=timestamp_limit,
        )
        if K is None:
            warn(f"K not specified, using default value {K}.")
        self.K = K

    @property
    def name(self) -> str:
        """Name of the metric."""
        return f"{super().name}_{self.K}"

    @property
    def params(self) -> dict[str, int | None]:
        """Parameters of the metric."""
        return super().params | {"K": self.K}

    @property
    def col_names(self) -> list[str]:
        """The names of the columns in the results DataFrame."""
        return ["user_id", "score"]

    def prepare_matrix(self, y_true: csr_matrix, y_pred: csr_matrix) -> tuple[csr_matrix, csr_matrix]:
        y_true, y_pred = super()._prepare_matrix(y_true, y_pred)
        y_pred = get_top_K_ranks(y_pred, self.K)
        return y_true, y_pred

K = K instance-attribute

name property

Name of the metric.

params property

Parameters of the metric.

col_names property

The names of the columns in the results DataFrame.

identifier property

Identifier of the object.

Identifier is made by combining the class name with the parameters passed at construction time.

Constructed by recreating the initialisation call. Example: Algorithm(param_1=value)

:return: Identifier of the object

IS_BASE = True class-attribute instance-attribute

micro_result property

Micro results for the metric.

:return: Detailed results for the metric. :rtype: dict[str, np.ndarray]

macro_result property

The global metric value.

is_time_aware property

Whether the metric is time-aware.

timestamp_limit property

The timestamp limit for the metric.

num_items property

Dimension of the item-space in both y_true and y_pred

num_users property

Dimension of the user-space in both y_true and y_pred after elimination of users without interactions in y_true.

prepare_matrix(y_true, y_pred)

Source code in src/recnexteval/metrics/core/top_k.py
57
58
59
60
def prepare_matrix(self, y_true: csr_matrix, y_pred: csr_matrix) -> tuple[csr_matrix, csr_matrix]:
    y_true, y_pred = super()._prepare_matrix(y_true, y_pred)
    y_pred = get_top_K_ranks(y_pred, self.K)
    return y_true, y_pred

get_params()

Get the parameters of the metric.

Source code in src/recnexteval/metrics/core/base.py
53
54
55
56
57
def get_params(self) -> dict[str, int | None]:
    """Get the parameters of the metric."""
    if not self.is_time_aware:
        return {}
    return {"timestamp_limit": self._timestamp_limit}

calculate(y_true, y_pred)

Calculates this metric for all nonzero users in y_true, given true labels and predicted scores.

Source code in src/recnexteval/metrics/core/base.py
116
117
118
119
120
121
def calculate(self, y_true: csr_matrix, y_pred: csr_matrix) -> None:
    """Calculates this metric for all nonzero users in `y_true`,
    given true labels and predicted scores.
    """
    y_true, y_pred = self._prepare_matrix(y_true, y_pred)
    self._calculate(y_true, y_pred)