def load_top_recommendations(self, n_recommendations, predictions, test_data, fold): """ This method loads the top n recommendations into a local variable. :param int n_recommendations: number of recommendations to be generated. :param int[][] predictions: predictions matrix (only 0s or 1s) :returns: A matrix of top recommendations for each user. :rtype: int[][] """ for user in range(self.ratings.shape[0]): # Bug discovered by Mostafa #nonzeros = self.test_indices[(user * (1 + fold))] #Fix: nonzeros = self.test_indices[user * self.k_folds + fold] top_recommendations = TopRecommendations(n_recommendations) for index in nonzeros: index = int(index) top_recommendations.insert(index, predictions[user][index]) self.recommendation_indices[user] = list( reversed(top_recommendations.get_indices())) top_recommendations = None self.recs_loaded = True return self.recommendation_indices
def load_top_recommendations(self, n_recommendations, predictions, test_data): """ This method loads the top n recommendations into a local variable. :param int n_recommendations: number of recommendations to be generated. :param int[][] predictions: predictions matrix (only 0s or 1s) """ for user in range(self.ratings.shape[0]): nonzeros = test_data[user].nonzero()[0] top_recommendations = TopRecommendations(n_recommendations) for index in nonzeros: top_recommendations.insert(index, predictions[user][index]) self.recommendation_indices[user] = list(reversed(top_recommendations.get_indices())) top_recommendations = None self.recs_loaded = True
def setUp(self): """ Setup method that is called at the beginning of each test. """ self.sizes = [10, 100, 1000] self.top_recommendations_array = [] for size in self.sizes: self.top_recommendations_array.append(TopRecommendations(size))
def recommend_items(self, user_id, num_recommendations=10): """ Get recommendations for a user. Based on the predictions returned by get_predictions :param int user_id: The id of the user. :param int num_recommendations: The number of recommended items. :returns: A zipped object containing list of tuples; first index is the id of the document and the second is the value of the calculated recommendation. :rtype: zip """ top_recommendations = TopRecommendations(num_recommendations) user_ratings = self.get_predictions()[user_id] for i in range(len(user_ratings)): top_recommendations.insert(i, user_ratings[i]) return zip(top_recommendations.get_indices(), top_recommendations.get_values())