Last active
May 18, 2017 14:28
-
-
Save ethen8181/08badf77cc417c24244f4b160991ace0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import numpy as np | |
from lightfm import LightFM | |
from sklearn.metrics import roc_auc_score | |
from lightfm.datasets import fetch_movielens | |
def auc_score(model, ratings): | |
""" | |
computes area under the ROC curve (AUC). | |
The full name should probably be mean | |
auc score as it is computing the auc | |
for every user's prediction and actual | |
interaction and taking the average for | |
all users | |
Parameters | |
---------- | |
model : BPR instance | |
the trained BPR model | |
ratings : scipy sparse matrix [n_users, n_items] | |
sparse matrix of user-item interactions | |
Returns | |
------- | |
auc : float 0 ~ 1 | |
auc score of the model | |
""" | |
ratings = ratings.tocsr() | |
auc = 0.0 | |
n_users, n_items = ratings.shape | |
for user in range(n_users): | |
y_pred = model.user_embeddings[user].dot(model.item_embeddings.T) + model.item_biases | |
y_true = np.zeros(n_items, dtype = np.int32) | |
y_true[ratings[user].indices] = 1 | |
auc += roc_auc_score(y_true, y_pred) | |
auc /= n_users | |
return auc | |
movielens = fetch_movielens() | |
train = movielens['train'] | |
test = movielens['test'] | |
model = LightFM(learning_rate=0.05, loss='bpr') | |
model.fit(train, epochs=10) | |
auc_score(model, test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment