Created
June 15, 2019 14:26
-
-
Save JustAyush/23a7e1077f9350a6a0951118cf12e715 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def similar_recommendation(model, interaction_matrix, user_id, user_dikt, | |
item_dikt,threshold = 0,number_rec_items = 15): | |
#Function to produce user recommendations | |
n_users, n_items = interaction_matrix.shape | |
user_x = user_dikt[user_id] | |
scores = pd.Series(model.predict(user_x,np.arange(n_items))) | |
scores.index = interaction_matrix.columns | |
scores = list(pd.Series(scores.sort_values(ascending=False).index)) | |
known_items = list(pd.Series(interaction_matrix.loc[user_id,:][interaction_matrix.loc[user_id,:] > threshold].index).sort_values(ascending=False)) | |
scores = [x for x in scores if x not in known_items] | |
print(len(scores)) | |
score_list = scores[0:number_rec_items] | |
known_items = list(pd.Series(known_items).apply(lambda x: item_dikt[x])) | |
scores = list(pd.Series(score_list).apply(lambda x: item_dikt[x])) | |
scores1 = list(pd.Series(score_list)) | |
# jsonScores = json.dumps(scores) | |
# print(jsonScores) | |
# return json.dumps(scores) | |
client=pymongo.MongoClient('mongodb://110.34.31.28:27017/') | |
mydb=client['majorProject'] | |
mycol=mydb['bookDataset'] | |
x=mycol.aggregate([{"$match":{"ISBN":{"$in":scores1}}}, | |
{"$project":{'_id':0,'ISBN':'$ISBN','bookTitle':'$Book-Title','bookAuthor':'$Book-Author','imageURL':'$Image-URL','averageRating':'$average_rating','publicationYear':'$publication_year','description':'$description'} }]) | |
y=list(x) | |
print(scores1) | |
print(scores) | |
# new dataframe for books | |
book_newdf = pd.DataFrame({'bookTitle':scores}) | |
print("Items that were liked by the User:") | |
counter = 1 | |
for i in known_items[:25]: | |
print(str(counter) + '- ' + i) | |
counter+=1 | |
print("\n Recommended Items:") | |
counter = 1 | |
for i in scores: | |
print(str(counter) + '- ' + i) | |
counter+=1 | |
return book_newdf,y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment