-
Notifications
You must be signed in to change notification settings - Fork 0
/
mrc.py
56 lines (38 loc) · 1.43 KB
/
mrc.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
# -*- coding: utf-8 -*-
"""
Created on Sat Sept 11 20:15:15 2017
@author: Sachin Bommale
"""
import numpy as np
from lightfm.datasets import fetch_movielens
from lightfm import LightFM
#fetch data and format it
data = fetch_movielens(min_rating=4.0)
#print training and testing data
print(repr(data['train']))
print(repr(data['test']))
#create model
model = LightFM(loss='bpr')
#train model
model.fit(data['train'], epochs=30, num_threads=2)
#recomendation function
def sample_recommendation(model, data, user_ids):
#number of users and movies in training data
n_users, n_items = data['train'].shape
#generate recommendations for each user we input
for user_id in user_ids:
#movies they already like
known_positives = data['item_labels'][data['train'].tocsr()[user_id].indices]
#movies our model predicts they will like
scores = model.predict(user_id, np.arange(n_items))
#rank them in order of most liked to least
top_items = data['item_labels'][np.argsort(-scores)]
#print out the results
print("User %s" % user_id)
print(" Already Watched Movies :")
for x in known_positives[:3]:
print(" %s" % x)
print(" New Recommended Movies:")
for x in top_items[:3]:
print(" %s" % x)
sample_recommendation(model, data, [2, 20, 40])