def make_user_user_recommendations(self, user_id, num_recommendations=10): ''' INPUT: user_id - (int) a user id num_recommendations - (int) the number of recommendations you want for the user OUTPUT: recs - (list) a list of recommendations for the user by article id rec_names - (list) a list of recommendations for the user by article title Description: Loops through the users based on closeness to the input user_id For each user - finds articles the user hasn't seen before and provides them as recs Does this until m recommendations are found Notes: * Choose the users that have the most total article interactions before choosing those with fewer article interactions. * Choose articles with the articles with the most total interactions before choosing those with fewer total interactions. ''' recs = [] neighbors_df = self.get_top_sorted_users(user_id) user_articles_id, user_articles_names = self.get_user_articles(user_id) for neighbor in neighbors_df.index: neighbor_articles_id, neighbor_articles_names = self.get_user_articles(neighbor) sorted_neighbor_article_ids = Utils.get_top_articles_df(neighbor_articles_id, self.interactions_df) sorted_neighbor_article_ids = sorted_neighbor_article_ids.index.values article_not_read = np.setdiff1d(sorted_neighbor_article_ids, user_articles_id, assume_unique=True) article_not_read = [str(i) for i in article_not_read] recs = np.unique(np.concatenate([article_not_read, recs], axis=0)) if len(recs) >= num_recommendations: break if len(recs) >= num_recommendations: recs = recs[:num_recommendations] recommended_articles = Utils.get_article_names(recs, self.interactions_df, 'title') return recommended_articles
def __init__(self, interactions_df): self.interactions_df = interactions_df self.article_ids = self.interactions_df['article_id'].unique() self.top_articles_df = Utils.get_top_articles_df(self.article_ids, self.interactions_df)