Skip to content

Commit

Permalink
refactoring; pylint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ananya173147 committed Oct 8, 2023
1 parent 0f5de8d commit 92a34e9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
54 changes: 28 additions & 26 deletions Code/prediction_scripts/item_based.py
Original file line number Diff line number Diff line change
@@ -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])
4 changes: 2 additions & 2 deletions Code/recommenderapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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
Expand Down

0 comments on commit 92a34e9

Please sign in to comment.