<jupyter_start><jupyter_text>Pull data from mongo<jupyter_code>import turicreate as tc
import pickle
import json
import numpy as np
import pandas as pd
import sklearn.model_selection as sk_ms
import pymongo

client = pymongo.MongoClient('mongodb+srv://paletteadmin:<EMAIL>/myFirstDatabase')
db = client.myFirstDatabase
dishratings_col = db.dishratings
cursor = dishratings_col.find()
list_cur = list(cursor)
df = pd.DataFrame(list_cur)
df.shape
df = df.drop(['_id','rating_id_num','review'], axis=1)
pd.unique(df['user_id']).shape<jupyter_output><empty_output><jupyter_text>ML<jupyter_code>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 = 'finalized_recommender_model'
item_sim_model.save('finalized_recommender_model')<jupyter_output><empty_output><jupyter_text>When Client Hits Server<jupyter_code>item_sim_model_open = tc.load_model('finalized_recommender_model')
#Making recommendations
item_sim_recomm = item_sim_model_open.recommend(users=["45"],k=5)
item_sim_recomm.print_rows(num_rows=5)
recs = item_sim_recomm['dish_id']
rec_list = []
for rec in recs:
    rec_list.append(rec)
    
print(rec_list)<jupyter_output>['<KEY>', '<KEY>', '603ab53b88dbe9c89607dfad', '<KEY>', '603ab53b88dbe9c89607dfce']<jupyter_text>update average rating<jupyter_code># for each dish 

    # total sum of ratings
    # total number of ratings
    # avg_rating = sum/number
    # update in mongo
auth_string = 'mongodb+srv://paletteadmin:<EMAIL>/myFirstDatabase'
import pymongo
import pandas as pd
from bson.objectid import ObjectId
client = pymongo.MongoClient(auth_string)
db = client.myFirstDatabase

dishratings_col = db.dishratings


dishes = db.dishes
cursor = dishes.find()
list_cur = list(cursor)
df = pd.DataFrame(list_cur)
df = df.drop(['dish_id_num', 'dish_name','restaurant_id', 'features', 'description', 'tags', 'rating_ids', 'createdAt', 'price', 'img'], axis=1)

print(df)
    


for row in 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)}})
    

# boolean confirmation that the API call went through
print ("acknowledged:", res.acknowledged)

# integer of the number of docs modified
print ("number of docs updated:", res.modified_count)

# dict object with more info on API call
print ("raw_result:", res.raw_result)<jupyter_output>acknowledged: True
number of docs updated: 1
raw_result: {'n': 1, 'nModified': 1, 'opTime': {'ts': Timestamp(1621976326, 2), 't': 21}, 'electionId': ObjectId('7fffffff0000000000000015'), 'ok': 1.0, '$clusterTime': {'clusterTime': Timestamp(1621976326, 2), 'signature': {'hash': b'\x07\xd9Llx\xaaC\x15\x93\xd8\xdbv\xbc^\x892\xd9\xb2J\x9a', 'keyId': 6928219948565135364}}, 'operationTime': Timestamp(1621976326, 2), 'updatedExisting': True}
{'fraction_non_alphanumeric': 0.13524215656411817, 'fraction_numerical': 0.038988729820286325, 'mean_word_length': 5.634343434343434, 'pattern_counts': {'":': 1, '<': 20, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '147686', 'license': 'MIT', 'n_tokens_mistral': 1290, 'n_tokens_neox': 1191, 'n_words': 232, 'path': 'ipynbnotebooks/.ipynb_checkpoints/collaborative_filtering_script_notebook-checkpoint.ipynb', 'repo_name': 'Palette-ai/ml', 'size': '3408'}