diff --git a/Code/prediction_scripts/item_based.py b/Code/prediction_scripts/item_based.py index a1bb9907a..2f5f385a6 100644 --- a/Code/prediction_scripts/item_based.py +++ b/Code/prediction_scripts/item_based.py @@ -1,40 +1,42 @@ import pandas as pd -import warnings import os app_dir = os.path.dirname(os.path.abspath(__file__)) code_dir = os.path.dirname(app_dir) project_dir = os.path.dirname(code_dir) -#warnings.filterwarnings("ignore") - - -def recommendForNewUser(user_rating): - ratings = pd.read_csv(project_dir + "/data/ratings.csv") - movies = pd.read_csv(project_dir + "/data/movies.csv") +def recommend_for_new_user(user_rating): + ratings = pd.read_csv(os.path.join(project_dir, "data", "ratings.csv")) + movies = pd.read_csv(os.path.join(project_dir, "data", "movies.csv")) user = pd.DataFrame(user_rating) - userMovieID = movies[movies["title"].isin(user["title"])] - userRatings = pd.merge(userMovieID, user) + user_movie_id = movies[movies["title"].isin(user["title"])] + user_ratings = pd.merge(user_movie_id, user) - moviesGenreFilled = movies.copy(deep=True) - copyOfMovies = movies.copy(deep=True) - for index, row in copyOfMovies.iterrows(): - copyOfMovies.at[index, "genres"] = row["genres"].split("|") - for index, row in copyOfMovies.iterrows(): + movies_genre_filled = movies.copy(deep=True) + copy_of_movies = movies.copy(deep=True) + + for index, row in copy_of_movies.iterrows(): + copy_of_movies.at[index, "genres"] = row["genres"].split("|") + + for index, row in copy_of_movies.iterrows(): for genre in row["genres"]: - moviesGenreFilled.at[index, genre] = 1 - moviesGenreFilled = moviesGenreFilled.fillna(0) + movies_genre_filled.at[index, genre] = 1 + + movies_genre_filled = movies_genre_filled.fillna(0) - userGenre = moviesGenreFilled[moviesGenreFilled.movieId.isin(userRatings.movieId)] - userGenre.drop(["movieId", "title", "genres"], axis=1, inplace=True) - userProfile = userGenre.T.dot(userRatings.rating.to_numpy()) - moviesGenreFilled.set_index(moviesGenreFilled.movieId) - moviesGenreFilled.drop(["movieId", "title", "genres"], axis=1, inplace=True) + user_genre = movies_genre_filled[movies_genre_filled.movieId.isin(user_ratings.movieId)] + user_genre.drop(["movieId", "title", "genres"], axis=1, inplace=True) + user_profile = user_genre.T.dot(user_ratings.rating.to_numpy()) + + movies_genre_filled.set_index(movies_genre_filled.movieId) + movies_genre_filled.drop(["movieId", "title", "genres"], axis=1, inplace=True) - recommendations = (moviesGenreFilled.dot(userProfile)) / userProfile.sum() - joinMoviesAndRecommendations = movies.copy(deep=True) - joinMoviesAndRecommendations["recommended"] = recommendations - joinMoviesAndRecommendations.sort_values( + recommendations = (movies_genre_filled.dot(user_profile)) / user_profile.sum() + + join_movies_and_recommendations = movies.copy(deep=True) + join_movies_and_recommendations["recommended"] = recommendations + join_movies_and_recommendations.sort_values( by="recommended", ascending=False, inplace=True ) - return [x for x in joinMoviesAndRecommendations["title"]][:201] + + return list(join_movies_and_recommendations["title"][:201]) diff --git a/Code/recommenderapp/app.py b/Code/recommenderapp/app.py index b10b5f6a6..d9276ab6e 100644 --- a/Code/recommenderapp/app.py +++ b/Code/recommenderapp/app.py @@ -4,7 +4,7 @@ import sys from utils import * sys.path.append("../../") -from Code.prediction_scripts.item_based import recommendForNewUser +from Code.prediction_scripts.item_based import recommend_for_new_user from search import Search app = Flask(__name__) @@ -28,7 +28,7 @@ def predict(): movie_with_rating = {"title": movie, "rating": 5.0} if (movie_with_rating not in training_data): training_data.append(movie_with_rating) - recommendations = recommendForNewUser(training_data) + recommendations = recommend_for_new_user(training_data) recommendations = recommendations[:10] resp = {"recommendations": recommendations} return resp