Пример #1
0
class UserCBCosineKNNRecommender(BaseUserColdStartRecommender):
    def __init__(
        self,
        X_interaction: InteractionMatrix,
        X_profile: ProfileMatrix,
        top_k: int = 100,
        n_threads: Optional[int] = None,
        shrink: float = 1e-1,
    ):

        self.top_k = top_k
        self.shrink = shrink
        self.n_threads = get_n_threads(n_threads)
        super().__init__(X_interaction, X_profile)
        self.X_interaction_csc = X_interaction.astype(np.float64).tocsc()
        self.sim_computer: Optional[CosineSimilarityComputer] = None

    def _learn(self) -> None:
        self.sim_computer = CosineSimilarityComputer(self.X_profile,
                                                     self.shrink,
                                                     normalize=True,
                                                     n_threads=self.n_threads)

    def get_score(self, profile: ProfileMatrix) -> DenseScoreArray:
        if self.sim_computer is None:
            raise RuntimeError("'get_score' called before learn.")
        if not sps.issparse(profile):
            profile = sps.csr_matrix(profile)
        similarity = self.sim_computer.compute_similarity(profile, self.top_k)
        score = sparse_mm_threaded(similarity, self.X_interaction_csc,
                                   self.n_threads)
        return score.astype(np.float64)
Пример #2
0
 def _create_computer(self, X: InteractionMatrix) -> CosineSimilarityComputer:
     return CosineSimilarityComputer(
         X, self.shrinkage, self.normalize, self.n_threads
     )
Пример #3
0
 def _learn(self) -> None:
     self.sim_computer = CosineSimilarityComputer(self.X_profile,
                                                  self.shrink,
                                                  normalize=True,
                                                  n_threads=self.n_threads)