-
Notifications
You must be signed in to change notification settings - Fork 1
/
recommendation.py
55 lines (39 loc) · 2.15 KB
/
recommendation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from scipy.sparse.linalg import svds
import pandas as pd
import numpy as np
import warnings
import sys
warnings.filterwarnings("ignore")
def recommend_recipes(df_svd_preds, user_id, ori_recipes_df, ori_ratings_df, num_recommendations = 5):
user_row_number = user_id - 1
sorted_user_predictions = df_svd_preds.iloc[user_row_number].sort_values(ascending=False)
user_data = ori_ratings_df[ori_ratings_df.uid == user_id]
user_history = user_data.merge(ori_recipes_df, on = 'rid').sort_values(['rating'], ascending=False)
recommendations = ori_recipes_df[~ori_recipes_df['rid'].isin(user_history['rid'])]
recommendations = recommendations.merge(pd.DataFrame(sorted_user_predictions).reset_index(), on = 'rid')
recommendations = recommendations.rename(columns = {user_row_number: 'Predictions'}).sort_values('Predictions', ascending = False).iloc[:num_recommendations, :]
return user_history, recommendations
def main(argv):
user_id = int(sys.argv[1])
df_recipe = pd.read_csv('/home/ubuntu/myapp/FoodNotifierBE_NoSQL/csv/recipe.csv') # Recipe Data Load
df_ratings = pd.read_csv('/home/ubuntu/myapp/FoodNotifierBE_NoSQL/csv/ratings.csv', sep='\t') # Ratings Data Load
df_user_recipe_ratings = df_ratings.pivot(
index='uid',
columns='rid',
values='rating'
).fillna(0) # Make User - Recipe Pivot Table
matrix = df_user_recipe_ratings.to_numpy() # Convert Pivot Table to Numpy Matrix
user_ratings_mean = np.mean(matrix, axis = 1) # User's Average Rating
matrix_user_mean = matrix - user_ratings_mean.reshape(-1, 1) # (User-Recipe Matrix) - (Average Rating)
U, sigma, Vt = svds(matrix_user_mean, k = 1)
sigma = np.diag(sigma)
svd_user_predicted_ratings = np.dot(np.dot(U, sigma), Vt) + user_ratings_mean.reshape(-1, 1)
df_svd_preds = pd.DataFrame(svd_user_predicted_ratings, columns = df_user_recipe_ratings.columns)
already_rated, predictions = recommend_recipes(df_svd_preds, user_id, df_recipe, df_ratings)
print(predictions.iloc[0]['rid'])
if __name__ == "__main__":
argv = sys.argv
if len(argv) < 2:
print('Error: User ID Argument Need')
else:
main(argv)