Created
April 5, 2019 12:26
-
-
Save gabraganca/9c78a6610a6a53919b094e08f2fb0a75 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_rec_movies(user_id:str, algoritmo, n_top: int =10) -> pd.DataFrame: | |
""" | |
Obtém uma lista de recomendação para N filmes para um | |
usuário. | |
Parameters: | |
user_id: o ID do usuário | |
n_top: O número de filmes desejados | |
Returns: | |
Um DataFrame com o ID dos filmes, nome e gênero. | |
""" | |
all_movie_id = set(df_ratings['movieId'].unique()) | |
rated_movies = set(df_ratings.query(f"userId == '{user_id}'")['movieId'].values) | |
unrated_movies = list(all_movie_id.difference(rated_movies)) | |
# top N movies | |
top_movies = pd.Series(unrated_movies, index=unrated_movies)\ | |
.map(lambda movie_id: algoritmo.predict(user_id, movie_id).est)\ | |
.sort_values(ascending=False)[:n_top]\ | |
.rename('pred_rating')\ | |
.reset_index()\ | |
.rename(columns=dict(index='movieId'))\ | |
.merge(df_movies, on='movieId')\ | |
.set_index('movieId') | |
return top_movies[['title', 'genres', 'pred_rating']] | |
get_rec_movies('Esposa', algo_base) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment