Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The easy guide for building python collaborative filtering recommendation system in 2017
import zipfile
from surprise import Reader, Dataset, SVD, evaluate
# Unzip ml-100k.zip
zipfile = zipfile.ZipFile('ml-100k.zip', 'r')
zipfile.extractall()
zipfile.close()
# Read data into an array of strings
with open('./ml-100k/u.data') as f:
all_lines = f.readlines()
# Prepare the data to be used in Surprise
reader = Reader(line_format='user item rating timestamp', sep='\t')
data = Dataset.load_from_file('./ml-100k/u.data', reader=reader)
# Split the dataset into 5 folds and choose the algorithm
data.split(n_folds=5)
algo = SVD()
# Train and test reporting the RMSE and MAE scores
evaluate(algo, data, measures=['RMSE', 'MAE'])
# Retrieve the trainset.
trainset = data.build_full_trainset()
algo.train(trainset)
# Predict a certain item
userid = str(196)
itemid = str(302)
actual_rating = 4
print(algo.predict(userid, itemid, actual_rating))
@akashadhikari

This comment has been minimized.

Copy link

akashadhikari commented Aug 17, 2017

You might as well like to wrap the last print() function in line no. 32 for Python 3

print (algo.predict(userid, itemid, actual_rating))

@mahermalaeb

This comment has been minimized.

Copy link
Owner Author

mahermalaeb commented Aug 26, 2017

Wrapping print is done. Thanks!

@aman7895

This comment has been minimized.

Copy link

aman7895 commented Sep 26, 2017

How can I use this package to predict items for a user? So the only input would be the user ID and the output should be a list of products based on the previous ratings! It would be great to get some help!

@mahermalaeb

This comment has been minimized.

Copy link
Owner Author

mahermalaeb commented Oct 18, 2017

There is no native function to predict top items for a specific user. However you can check this FAQ part of the documentation of Surprise How to get the top-N recommendations for each user. This example shows how to predict top-n items for all users but you can modify the code to predict items for a specific user.

@irlaroche

This comment has been minimized.

Copy link

irlaroche commented Jul 29, 2018

Hello,
I'm a beginner and I use Python 3.7. I receive the following feedback when using your code:
ModuleNotFoundError: No module named 'surprise'

Could someone help me please?

@phillylovesdata

This comment has been minimized.

Copy link

phillylovesdata commented Aug 5, 2018

Hi,
that just means you haven't installed surprise yet. In your python shell run "pip install scikit-surprise" or in your conda environment "conda install -c conda-forge scikit-surprise".

If you don't know what any of that means, I'd suggest starting at the beginning (with a python course or something) and not with recommender systems ;)

@TamTSE2301

This comment has been minimized.

Copy link

TamTSE2301 commented Oct 15, 2018

Hi. Now i want to change back the data type back to DataFrame (pandas) after applying SVD. Could you suggest the way to do it. Thanks

@gourxb

This comment has been minimized.

Copy link

gourxb commented Dec 3, 2018

How to get the latent feature (or embedding) for each user and item from the trained model?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.