Home » Python, Recommender System2 May 2016

Recommender System using Python & python-recsys

python-recsys is a Python Library for implementing a Recommender System.

Currently, python-recsys supports two Recommender Algorithms: Singular Value Decomposition (SVD) and Neighborhood SVD.

Here is a QuickStart tutorial on using python-recsys for Recommender Systems. It takes movielens’s movie ratings dataset and shows examples about computing similarity between movie items and recommending movies to users.

Here, I will be showing code on how to use a custom CSV dataset and evaluate a recommender system using SVD algorithm.

I will be fetching data from a CSV file. The CSV file consists of 3 fields (user_id, item_id, and star_rating). item_id can be ID of anything like hotels, movies, books, etc. star_rating is the rating provided by users to items. The rating ranges from 1 to 5. 5 is considered as best rating and 1 is considered as worst rating.

For this article, I have created a dummy CSV file named dataset-recsys.csv containing three columns (user_id, item_id, and star_rating). You can download the CSV file from here.

Here is the code to create the CSV file:

Creating a Data Model

python-recsys library uses matrix factorization algorithms like SVD and Neighborhood SVD that take input data (in a form of a matrix) and then decompose it (reduce it into lower dimensional space).

Computing Similarity

Similarity between two items:

Similar items to a particular item:

The similar and similarity function take row value of the matrix M.

As you can see below, while loading data, we have specified 2nd column of our CSV dataset file as row.

In our CSV file 1st column is user_id and 2nd column is item_id. Hence, we had to pass ITEMID as a parameter to similar and similarity functions.

So, if we want to compute similarity between users then we first need to load data by specifying 1st column (user_id) as row like this:

Now we can compute similarity between users.

Predicting rating for a particular user and item

Recommend items to a particular user

Recommend items to a user that he/she hasn’t rated before.


The following code contains evaluation using two prediction-based metrics (Root Mean Square Error (RMSE) & Mean Absolute Error (MAE)) and two rank-based metrics (Spearman’s rho & Kendall-tau).

Here’s the full source code:

Hope this helps.

Recommender System

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed