-
Notifications
You must be signed in to change notification settings - Fork 0
/
recommender_script.py
59 lines (44 loc) · 1.95 KB
/
recommender_script.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
56
57
58
59
import turicreate as tc
import pickle
import json
import numpy as np
import pandas as pd
import pymongo
from dotenv import load_dotenv
import os
from bson.objectid import ObjectId
# get authentication credentials
USERNAME = os.environ["USERNAME_DB"]
PASSWORD = os.environ["PASSWORD_DB"]
# connect to mongo
auth_string = 'mongodb+srv://'+USERNAME+':'+PASSWORD+'@cluster0.zwmnc.mongodb.net/myFirstDatabase'
client = pymongo.MongoClient(auth_string)
# recommender script, updates the recommendation model stored in an amazon s3 bucket
db = client.myFirstDatabase
dishratings_col = db.dishratings
cursor = dishratings_col.find()
list_cur = list(cursor)
df = pd.DataFrame(list_cur)
df = df.drop(['_id','rating_id_num','review'], axis=1)
rec_data = tc.SFrame(df)
item_sim_model = tc.item_similarity_recommender.create(rec_data, user_id='user_id', item_id='dish_id', target='rating', similarity_type='cosine')
filename = 's3://paletterecommendermodel/finalized_recommender_model/'
item_sim_model.save('s3://paletterecommendermodel/finalized_recommender_model/')
# average rating script, updates the average ratings for each dish using the ratings that were added during the day
dishes = db.dishes
dish_cursor = dishes.find()
list_cur_dish = list(dish_cursor)
dish_df = pd.DataFrame(list_cur_dish)
dish_df = dish_df.drop(['dish_id_num', 'dish_name','restaurant_id', 'features', 'description', 'tags', 'rating_ids', 'createdAt', 'price', 'img'], axis=1)
for row in dish_df.iterrows():
dish_id = row[1]['_id']
ratings_cursor = dishratings_col.find({'dish_id': str(dish_id)})
ratings_cursor_list = list(ratings_cursor)
total = 0
rating_sum = 0
if(len(ratings_cursor_list) > 0):
for rating in ratings_cursor_list:
total+=1
rating_sum+=rating['rating']
avg = rating_sum/total
res = dishes.update_one({'_id': ObjectId(str(dish_id))}, {"$set": {'average_rating': round(avg, 1)}})