예제 #1
0
 def get_r_hat(self, weights_array):
     hybrid_r_hat = data.get_empty_urm()
     count = 0
     for m in self.normalized_r_hat_array:
         hybrid_r_hat += m * weights_array[count]
         count += 1
     return hybrid_r_hat
예제 #2
0
 def get_r_hat(self, load_from_file=False, path=''):
     """
     :param load_from_file: if the matrix has been saved can be set to true for load it from it
     :param path: path in which the matrix has been saved
     -------
     :return the extimated urm from the recommender
     """
     r_hat = data.get_empty_urm()
     r_hat[data.get_target_playlists()] = self.U[data.get_target_playlists()].dot(self.s_Vt)
     return r_hat
예제 #3
0
 def get_r_hat(self):
     """
     compute the r_hat for the model
     :return  r_hat only for the target playlists
     """
     if self.W_sparse == None:
         log.error(
             'the recommender has not been trained, call the fit() method for compute W'
         )
     r_hat = data.get_empty_urm()
     r_hat[data.get_target_playlists()] = self.URM_train[
         data.get_target_playlists()].dot(self.W_sparse)
     return r_hat
예제 #4
0
    def get_r_hat(self):
        """
        compute the r_hat for the model filled with zeros in playlists not target
        :return  r_hat
        """

        if self.user_vecs is None:
            log.error('the recommender has not been trained, call the fit() method')

        r_hat = data.get_empty_urm()
        r_hat = r_hat.todense()
        r_estimated = np.dot(self.user_vecs[data.get_target_playlists()], self.item_vecs.T)
        r_hat[data.get_target_playlists()] = r_estimated
        r_hat = sps.csr_matrix(r_hat)
        print('saving matrix')
        return r_hat
예제 #5
0
    def get_r_hat(self, weights_array):
        hybrid_matrix = sps.csr_matrix(self.normalized_matrices_array[0].shape)
        count = 0
        for m in self.normalized_matrices_array:
            hybrid_matrix += m*weights_array[count]
            count += 1

        if self.name == 'HybridSimilarity':
            #compute the r_hat if we have the similarity
            if self.INVERSE == False:
                hybrid_matrix = self.urm_filter_tracks[data.get_target_playlists()].dot(hybrid_matrix)
            else:
                hybrid_matrix = hybrid_matrix[data.get_target_playlists()].dot(self.urm_filter_tracks)
            r_hat = data.get_empty_urm()
            r_hat[data.get_target_playlists()] = hybrid_matrix
            hybrid_matrix = r_hat

        return hybrid_matrix
예제 #6
0
 def get_r_hat(self):
     r_hat = data.get_empty_urm()
     r_hat[data.get_target_playlists()] = self.URM_train[
         d.get_target_playlists()].dot(self.W_sparse)
     return r_hat
예제 #7
0
파일: CFW.py 프로젝트: keyblade95/RecSys
 def get_r_hat(self):
     r_hat = d.get_empty_urm()
     user_profile_batch = self.URM_train[d.get_target_playlists()]
     r_hat[d.get_target_playlists()] = user_profile_batch.dot(self.W_sparse)
     return r_hat
예제 #8
0
    def recommend_batch(self,
                        weights_array,
                        target_userids,
                        N=10,
                        filter_already_liked=True,
                        items_to_exclude=[],
                        verbose=False):
        """
        method used for get the hybrid prediction from the r_hat_matrices passed as parameter during the creation of the recommender
        step1: normalize the matrices, there are two type of normalization
        step2: sum the normalized matrices
        step3: get the prediction from the matrix obtained as sum

        :param weights_array: array of the weights of each r_hat matrix
        :param userids: user for which compute the predictions
        :param normalization_mode: 'MAX_ROW'(default) normalize each row for the max of the row
                                    'MAX_MATRIX' normalize each row for the max of the matrix
        :param N: how many items to predict for each user
        :param filter_already_liked: filter the element with which the user has interacted
        :param items_to_exclude: list of item to exclude from the predictions
        :param verbose:
        :return:
        """

        start = time.time()

        hybrid_r_hat = data.get_empty_urm()

        count = 0
        for m in self.normalized_r_hat_array:
            hybrid_r_hat += m * weights_array[count]
            count += 1

        #filter seen elements
        if filter_already_liked:
            user_profile = self.urm_filter_tracks
            hybrid_r_hat[user_profile.nonzero()] = -np.inf
        """
        # r_hat has only the row for the target playlists, so recreate a matrix with a shape = to the shape of the original_urm
        reconstructed_r_hat = sps.csr_matrix(self.urm.shape)
        reconstructed_r_hat[data.get_target_playlists()] = hybrid_r_hat
        """

        # STEP3
        ranking = np.zeros((len(target_userids), N), dtype=np.int)
        hybrid_r_hat = hybrid_r_hat.todense()

        count = 0
        for row_index in target_userids:
            scores_row = hybrid_r_hat[row_index]

            relevant_items_partition = (-scores_row).argpartition(N)[0, 0:N]
            relevant_items_partition_sorting = np.argsort(
                -scores_row[0, relevant_items_partition])
            ranking[count] = relevant_items_partition[
                0, relevant_items_partition_sorting[0, 0:N]]
            count += 1

        print('recommendations created')

        print('{:.2f}'.format(time.time() - start))
        return self._insert_userids_as_first_col(target_userids, ranking)