Surprise Recommender Library,Few Lines for Everything

mustafac
3 min readApr 15, 2021

In former post I played with SVD for Recommendation. Now I will play with heartbreaking “Surprise library”. They make everything a few lines, so soon our job could be a excel plugin.
The code if at github. ( Link )

In this post, I will be using Surpise and compare results with other methods. We just need to give 3 columns from our dataframe. At the reader we give scale of our ratings (1,5).

From this dataset we can generate :
trainset : which contains all the rated items
antitestset : items which are missing in dataframe

When you have trainset you can apply lots of algorithms with 2 lines. As you see below, I define algorithm and apply to trainset for various algorithms. And also famous svd_plusplus, but it takes longer than others so be careful if you use.

I want to evaluate results per user. With below method I return the items rated by user and items not rated by user. Below I traverse 2 sets anti_testset and test_set and select items with user_id.

We can also calculate predictions and metrics very easily as below. You just give the dataset to evalutate the result and you get predictions.

If we apply these methods to whole set we get results as below. You can svdplusplus is best. Also interestingly SVD with 100 features is better than KNNs. In the github code there is evaluation for “ME” dataset , you can check it.

Now I want to evaluate these models for my preferences. I want them to return their suggestions for me.

SVD 100
SVDPLUSPLUS

I think both are good but SVDPLUSPLUS really aligned me more(Shakespeare in Love). You can check other samples in github.

In this post, I showed how simple is to use Surprise library for recommendation. You can do everything in a few lines. At a later post, I will apply Neural Network to see if it performs better than these.

--

--