Last active
March 26, 2018 03:23
-
-
Save lppier/3481fcee39efed7a5d9357d8f6326c07 to your computer and use it in GitHub Desktop.
item-item collaborative filtering
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
from __future__ import (absolute_import, division, print_function, | |
unicode_literals) | |
from surprise import KNNWithMeans | |
from surprise import Dataset | |
from surprise import accuracy | |
from surprise.model_selection import train_test_split | |
# Load the movielens-100k dataset UserID::MovieID::Rating::Timestamp | |
data = Dataset.load_builtin('ml-100k') | |
trainset, testset = train_test_split(data, test_size=.15) | |
# Use user_based true/false to switch between user-based or item-based collaborative filtering | |
algo = KNNWithMeans(k=50, sim_options={'name': 'pearson_baseline', 'user_based': False}) | |
algo.fit(trainset) | |
# run the trained model against the testset | |
test_pred = algo.test(testset) | |
# get RMSE | |
print("Item-based Model : Test Set") | |
accuracy.rmse(test_pred, verbose=True) | |
# if you wanted to evaluate on the trainset | |
print("Item-based Model : Training Set") | |
train_pred = algo.test(trainset.build_testset()) | |
accuracy.rmse(train_pred) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment