Source code for irspack.recommenders.toppop

from typing import List, Optional

import numpy as np

from ..definitions import DenseScoreArray, InteractionMatrix, UserIndexArray
from ..optimization.parameter_range import ParameterRange
from .base import BaseRecommender, RecommenderConfig


class TopPopConfig(RecommenderConfig):
    pass


[docs]class TopPopRecommender(BaseRecommender): """A simple recommender system based on the popularity of the items in the training set (without any personalization). Args: X_train Union[scipy.sparse.csr_matrix, scipy.sparse.csc_matrix]): Input interaction matrix. """ default_tune_range: List[ParameterRange] = [] config_class = TopPopConfig score_: Optional[np.ndarray]
[docs] def __init__(self, X_train: InteractionMatrix): super().__init__(X_train) self.score_ = None
def _learn(self) -> None: self.score_ = self.X_train_all.sum(axis=0).astype(np.float64).A @property def score(self) -> np.ndarray: if self.score_ is None: raise RuntimeError("The method called before ``learn``.") return self.score_
[docs] def get_score(self, user_indices: UserIndexArray) -> DenseScoreArray: n_users: int = user_indices.shape[0] res: DenseScoreArray = np.repeat(self.score, n_users, axis=0) return res
[docs] def get_score_cold_user(self, X: InteractionMatrix) -> DenseScoreArray: n_users: int = X.shape[0] res: DenseScoreArray = np.repeat(self.score, n_users, axis=0) return res