def make_recs(self, _id, _id_type='movie', rec_num=5): ''' given a user id or a movie that an individual likes to make recommendations. Input: _id - the user/movie id you want to predect for _id_type - the id _id_type rec_num - how many recommendation you want to provide Output: rec_names - (array) a list or numpy array of recommended movies by name ''' rec_ids = create_ranked_df(self.movies, self.ratings_mat) if _id_type == 'user': if _id in self.ratings_mat.index: ind = np.where(self.ratings_mat.index == _id)[0][0] preds = np.dot(self.user_mat[ind, :], self.movie_mat) rec_inds = np.argsort(preds)[0 - rec_num:] rec_ids = self.ratings_mat.columns[rec_inds] rec_names = rf.get_movie_names(rec_ids) else: rec_names = rf.popular_recommendations(_id, rec_num, rec_ids) else: rec_ids = rf.find_similar_movies(_id) rec_names = rf.get_movie_names(rec_ids) return rec_names
def make_recs(self, _id, _id_type='movie', rec_num=5): """ given a user id or a movie that an individual likes make recommendations INPUT: _id - either a user or movie id (int) _id_type - "movie" or "user" (str) rec_num - number of recommendations to return (int) OUTPUT: rec_ids - (array) a list or numpy array of recommended movies by id rec_names - (array) a list or numpy array of recommended movies by name """ # if the user is available from the matrix factorization data, # I will use this and rank movies based on the predicted values # For use with user indexing val_users = self.train_data_df.index rec_ids, rec_names = None, None if _id_type == 'user': if _id in self.train_data_df.index: # Get the index of which row the user is in for use in U matrix idx = np.where(val_users == _id)[0][0] # take the dot product of that row and the V matrix preds = np.dot(self.user_mat[idx, :], self.movie_mat) # pull the top movies according to the prediction indices = preds.argsort()[-rec_num:][::-1] # indices rec_ids = self.train_data_df.columns[indices] rec_names = rf.get_movie_names(rec_ids, self.movies) else: # if we don't have this user, give just top ratings back rec_names = rf.popular_recommendations(_id, rec_num, self.ranked_movies) print( "Because this user wasn't in our database, we are giving back the top movie " "recommendations for all users.") # Find similar movies if it is a movie that is passed else: if _id in self.train_data_df.columns: rec_names = list(rf.find_similar_movies(_id, self.movies))[:rec_num] else: print( "That movie doesn't exist in our database. Sorry, we don't have any recommendations for you." ) return rec_ids, rec_names
def make_recs(self, _id, _id_type='movie', rec_num=5): ''' given a user id or a movie that an individual likes make recommendations :param _id: id of the user or movie (int) :param _id_type: "movie" or "user" (str) :param rec_num: number of desired recommendations (int) :return rec_ids: list of recommended movie ids for @_id_type :return rec_names: list of recommended movie names for @_id_type ''' if _id_type == 'user': if _id in self.user_item_df.index: # Get the index of which row the user is in for use in U matrix user_row = np.where(self.user_ids == _id)[0][0] # take the dot product of that U row and the V matrix predictions = np.dot(self.user_mat[user_row], self.movie_mat) pred_df = pd.DataFrame(predictions, index=self.movie_ids, columns=['Predictions']) # Sorting the pred df to have the best rated movies first pred_df.sort_values(by='Predictions', ascending=False, inplace=True) rec_ids = pred_df.index[:rec_num] else: rec_ids = self.ranked_movies['movie_id'][:rec_num] if _id_type == 'movie': similar_movies = rf.find_similar_movies(_id, self.movies) best_movies = pd.DataFrame(self.ranked_movies['movie'].values) recs = best_movies[best_movies['movie'].isin( similar_movies)][:rec_num] rec_ids = np.array(recs.index) rec_names = rf.get_movie_names(rec_ids, self.movies) return rec_ids, rec_names
def make_recs(self, _id, _id_type='movie', rec_num=5): ''' given a user id or a movie that an individual likes make recommendations INPUT: _id - either a user or movie id (int) _id_type - "movie" or "user" (str) (defult 'movie') rec_num - number of recommendations to return (int) (defult 5) OUTPUT: rec_ids - (array) a list or numpy array of recommended movies by id rec_names - (array) a list or numpy array of recommended movies by name ''' if _id_type == 'movie': try: rec_names = rf.find_similar_movies(_id, self.movies)[:rec_num] rec_ids = self.movies[self.movies['movie'].isin( rec_names)]['movie_id'].values[:rec_num] except: print('movie not in dataset') rec_ids, rec_names = None, None else: if _id in self.train_data_df.index: # find row in user_mat user = np.where(self.train_data_df.index == _id)[0][0] # preidct rateing on user with all movies pre = np.dot(self.user_mat[user, :], self.movie_mat) # get movies indices of top rec_num records indices = np.argsort(pre)[::-1][:rec_num] # get movie ids with index rec_ids = self.train_data_df.columns[indices].values # get movie names rec_names = rf.get_movie_names(rec_ids, self.movies) else: rec_names = rf.popular_recommendations(_id, rec_num, self.ranked_movies) rec_ids = self.movies[self.movies['movie'].isin( rec_names)]['movie_id'].values[:rec_num] print( "Because this user wasn't in our database, we are giving back the top movie recommendations for all users." ) return rec_ids, rec_names
def make_recommendations(self, _id, _id_type='movie', rec_num=5): """ given a user id or a movie that an individual likes make recommendations """ rec_ids, rec_names = None, None if _id_type == 'user': if _id in self.user_ids_series: # Get the index of which row the user is in for use in U matrix idx = np.where(self.user_ids_series == _id)[0][0] # take the dot product of that row and the V matrix preds = np.dot(self.user_matrix[idx, :], self.movie_matrix) # pull the top movies according to the prediction indices = preds.argsort()[-rec_num:][::-1] #indices rec_ids = self.movie_ids_series[indices] rec_names = rf.get_movie_names(rec_ids, self.movies) else: # if we don't have this user, give just top ratings back rec_names = rf.popular_recommendations(_id, rec_num, self.ranked_movies) print( "Because this user wasn't in our database, we are giving back the top movie recommendations for all users. (Cold Start Problem)" ) # Find similar movies if it is a movie that is passed else: if _id in self.movie_ids_series: rec_names = list(rf.find_similar_movies(_id, self.movies))[:rec_num] else: print( "That movie doesn't exist in our database. Sorry, we don't have any recommendations for you." ) return rec_ids, rec_names
def make_recs(self,_id, _id_type='user', rec_num=5): ''' INPUT: _id - either a user or movie id (int) _id_type - "movie" or "user" (str) rec_num - number of recommendations to return (int) OUTPUT: recs - (array) a list or numpy array of recommended movies like the given movie, or recs for a user_id given ''' rec_ids, rec_names = None, None if _id_type == 'user': if _id in self.user_ids_series: # Get the index of which row the user is in for use in U matrix idx = np.where(self.user_ids_series == _id)[0][0] # take the dot product of that row and the V matrix preds = np.dot(self.user_mat[idx,:],self.movie_mat) # pull the top movies according to the prediction indices = preds.argsort()[-rec_num:][::-1] #indices rec_ids = self.user_item_df.columns[indices] rec_names = rf.get_movie_names(rec_ids, self.movies) else: # if we don't have this user, give just top ratings back rec_names = rf.popular_recommendations(_id, rec_num, self.ranked_movies) # Find similar movies if it is a movie that is passed else: if _id in self.movie_ids_series: rec_names = list(rf.find_similar_movies(_id, self.movies))[:rec_num] else: print("That movie doesn't exist in our database. Sorry, we don't have any recommendations for you.") return rec_ids, rec_names