Advanced Recommender Systems With Python
Advanced Recommender Systems With Python
Advanced Recommender Systems With Python
Recommendation Systems usually rely on larger data sets and specifically need to be organized in a
particular fashion. Because of this, we won't have a project to go along with this topic, instead we will have a
more intensive walkthrough process on creating a recommendation system with Python with the same Movie
Lens Data Set.
Note: The actual mathematics behind recommender systems is pretty heavy in Linear Algebra.
Methods Used
Two most common types of recommender systems are Content-Based and Collaborative Filtering (CF).
Collaborative Filtering
In general, Collaborative filtering (CF) is more commonly used than content-based systems because it
usually gives better results and is relatively easy to understand (from an overall implementation perspective).
The algorithm has the ability to do feature learning on its own, which means that it can start to learn for itself
what features to use.
CF can be divided into Memory-Based Collaborative Filtering and Model-Based Collaborative filtering.
In this tutorial, we will implement Model-Based CF by using singular value decomposition (SVD) and
Memory-Based CF by computing cosine similarity.
The Data
We will use famous MovieLens dataset, which is one of the most common datasets used when implementing
and testing recommender engines. It contains 100k movie ratings from 943 users and a selection of 1682
movies.
You can download the dataset here (http://files.grouplens.org/datasets/movielens/ml-100k.zip) or just use the
u.data file that is already included in this folder.
Getting Started
Let's import some libraries we will need:
http://localhost:8888/nbconvert/html/Downloads/Python-Data-Science-and-Machine-Learning-Bootcamp/Machine%20Learning%20Sections/Rec… 1/13
22.1.2018 Advanced Recommender Systems with Python
In [2]:
import numpy as np
import pandas as pd
We can then read in the u.data file, which contains the full dataset. You can read a brief description of the
dataset here (http://files.grouplens.org/datasets/movielens/ml-100k-README.txt).
Note how we specify the separator argument for a Tab separated file.
In [3]:
In [4]:
df.head()
Out[4]:
0 0 50 5 881250949
1 0 172 5 881250949
2 0 133 1 881250949
Note how we only have the item_id, not the movie name. We can use the Movie_ID_Titles csv file to grab the
movie names and merge it with this dataframe:
In [15]:
movie_titles = pd.read_csv("Movie_Id_Titles")
movie_titles.head()
Out[15]:
item_id title
1 2 GoldenEye (1995)
4 5 Copycat (1995)
http://localhost:8888/nbconvert/html/Downloads/Python-Data-Science-and-Machine-Learning-Bootcamp/Machine%20Learning%20Sections/Rec… 2/13
22.1.2018 Advanced Recommender Systems with Python
In [16]:
df = pd.merge(df,movie_titles,on='item_id')
df.head()
Out[16]:
Now let's take a quick look at the number of unique users and movies.
In [26]:
n_users = df.user_id.nunique()
n_items = df.item_id.nunique()
In [27]:
http://localhost:8888/nbconvert/html/Downloads/Python-Data-Science-and-Machine-Learning-Bootcamp/Machine%20Learning%20Sections/Rec… 3/13
22.1.2018 Advanced Recommender Systems with Python
A user-item filtering will take a particular user, find users that are similar to that user based on similarity of
ratings, and recommend items that those similar users liked.
In contrast, item-item filtering will take an item, find users who liked that item, and find other items that those
users or similar users also liked. It takes items and outputs other items as recommendations.
Item-Item Collaborative Filtering: “Users who liked this item also liked …”
User-Item Collaborative Filtering: “Users who are similar to you also liked …”
In both cases, you create a user-item matrix which built from the entire dataset.
Since we have split the data into testing and training we will need to create two [943 x 1682] matrices (all
users by all movies).
The training matrix contains 75% of the ratings and the testing matrix contains 25% of the ratings.
After you have built the user-item matrix you calculate the similarity and create a similarity matrix.
The similarity values between items in Item-Item Collaborative Filtering are measured by observing all the
users who have rated both items.
http://localhost:8888/nbconvert/html/Downloads/Python-Data-Science-and-Machine-Learning-Bootcamp/Machine%20Learning%20Sections/Rec… 4/13
22.1.2018 Advanced Recommender Systems with Python
For User-Item Collaborative Filtering the similarity values between users are measured by observing all the
items that are rated by both users.
A distance metric commonly used in recommender systems is cosine similarity, where the ratings are seen
as vectors in n-dimensional space and the similarity is calculated based on the angle between these vectors.
Cosine similiarity for users a and m can be calculated using the formula below, where you take dot product of
the user vector u k and the user vector u a and divide it by multiplication of the Euclidean lengths of the
vectors.
Your first step will be to create the user-item matrix. Since you have both testing and training data you need
to create two matrices.
In [28]:
#Create two user-item matrices, one for training and another for testing
train_data_matrix = np.zeros((n_users, n_items))
for line in train_data.itertuples():
train_data_matrix[line[1]-1, line[2]-1] = line[3]
In [29]:
http://localhost:8888/nbconvert/html/Downloads/Python-Data-Science-and-Machine-Learning-Bootcamp/Machine%20Learning%20Sections/Rec… 5/13
22.1.2018 Advanced Recommender Systems with Python
Next step is to make predictions. You have already created similarity matrices: user_similarity and
item_similarity and therefore you can make a prediction by applying following formula for user-based CF:
You can look at the similarity between users k and a as weights that are multiplied by the ratings of a similar
user a (corrected for the average rating of that user). You will need to normalize it so that the ratings stay
between 1 and 5 and, as a final step, sum the average ratings for the user that you are trying to predict.
The idea here is that some users may tend always to give high or low ratings to all movies. The relative
difference in the ratings that these users give is more important than the absolute values. To give an
example: suppose, user k gives 4 stars to his favourite movies and 3 stars to all other good movies. Suppose
now that another user t rates movies that he/she likes with 5 stars, and the movies he/she fell asleep over
with 3 stars. These two users could have a very similar taste but treat the rating system differently.
When making a prediction for item-based CF you don't need to correct for users average rating since query
user itself is used to do predictions.
In [30]:
return pred
In [31]:
http://localhost:8888/nbconvert/html/Downloads/Python-Data-Science-and-Machine-Learning-Bootcamp/Machine%20Learning%20Sections/Rec… 6/13
22.1.2018 Advanced Recommender Systems with Python
Evaluation
There are many evaluation metrics but one of the most popular metric used to evaluate accuracy of predicted
ratings is Root Mean Squared Error (RMSE).
Since you only want to consider predicted ratings that are in the test dataset, you filter out all other elements
in the prediction matrix with prediction[ground_truth.nonzero()].
In [32]:
In [33]:
Memory-based algorithms are easy to implement and produce reasonable prediction quality. The drawback
of memory-based CF is that it doesn't scale to real-world scenarios and doesn't address the well-known cold-
start problem, that is when new user or new item enters the system. Model-based CF methods are scalable
and can deal with higher sparsity level than memory-based models, but also suffer when new users or items
that don't have any ratings enter the system. I would like to thank Ethan Rosenthal for his post
(http://blog.ethanrosenthal.com/2015/11/02/intro-to-collaborative-filtering/) about Memory-Based
Collaborative Filtering.
http://localhost:8888/nbconvert/html/Downloads/Python-Data-Science-and-Machine-Learning-Bootcamp/Machine%20Learning%20Sections/Rec… 7/13
22.1.2018 Advanced Recommender Systems with Python
In [34]:
sparsity=round(1.0-len(df)/float(n_users*n_items),3)
print('The sparsity level of MovieLens100K is ' + str(sparsity*100) + '%')
To give an example of the learned latent preferences of the users and items: let's say for the MovieLens
dataset you have the following information: (user id, age, location, gender, movie id, director, actor, language,
year, rating). By applying matrix factorization the model learns that important user features are age group
(under 10, 10-18, 18-30, 30-90), location and gender, and for movie features it learns that decade, director
and actor are most important. Now if you look into the information you have stored, there is no such feature
as the decade, but the model can learn on its own. The important aspect is that the CF model only uses data
(user_id, movie_id, rating) to learn the latent features. If there is little data available model-based CF model
will predict poorly, since it will be more difficult to learn the latent features.
Models that use both ratings and content features are called Hybrid Recommender Systems where both
Collaborative Filtering and Content-based Models are combined. Hybrid recommender systems usually show
higher accuracy than Collaborative Filtering or Content-based Models on their own: they are capable to
address the cold-start problem better since if you don't have any ratings for a user or an item you could use
the metadata from the user or item to make a prediction.
http://localhost:8888/nbconvert/html/Downloads/Python-Data-Science-and-Machine-Learning-Bootcamp/Machine%20Learning%20Sections/Rec… 8/13
22.1.2018 Advanced Recommender Systems with Python
SVD
A well-known matrix factorization method is Singular value decomposition (SVD). Collaborative Filtering
can be formulated by approximating a matrix X by using singular value decomposition. The winning team at
the Netflix Prize competition used SVD matrix factorization models to produce product recommendations, for
more information I recommend to read articles: Netflix Recommendations: Beyond the 5 stars
(http://techblog.netflix.com/2012/04/netflix-recommendations-beyond-5-stars.html) and Netflix Prize and SVD
(http://buzzard.ups.edu/courses/2014spring/420projects/math420-UPS-spring-2014-gower-netflix-SVD.pdf).
The general equation can be expressed as follows:
Given m x n matrix X:
U is an (m x r) orthogonal matrix
S is an (r x r) diagonal matrix with non-negative real numbers on the diagonal
V^T is an (r x n) orthogonal matrix
Matrix X can be factorized to U, S and V. The U matrix represents the feature vectors corresponding to the
users in the hidden feature space and the V matrix represents the feature vectors corresponding to the items
in the hidden feature space.
Now you can make a prediction by taking dot product of U, S and V^T.
In [35]:
import scipy.sparse as sp
from scipy.sparse.linalg import svds
http://localhost:8888/nbconvert/html/Downloads/Python-Data-Science-and-Machine-Learning-Bootcamp/Machine%20Learning%20Sections/Rec… 9/13
22.1.2018 Advanced Recommender Systems with Python
Carelessly addressing only the relatively few known entries is highly prone to overfitting. SVD can be very
slow and computationally expensive. More recent work minimizes the squared error by applying alternating
least square or stochastic gradient descent and uses regularization terms to prevent overfitting. Alternating
least square and stochastic gradient descent methods for CF will be covered in the next tutorials.
Review:
We have covered how to implement simple Collaborative Filtering methods, both memory-based
CF and model-based CF.
Memory-based models are based on similarity between items or users, where we use cosine-
similarity.
Model-based CF is based on matrix factorization where we use SVD to factorize the matrix.
Building recommender systems that perform well in cold-start scenarios (where little data is
available on new users and items) remains a challenge. The standard collaborative filtering method
performs poorly is such settings.
http://localhost:8888/nbconvert/html/Downloads/Python-Data-Science-and-Machine-Learning-Bootcamp/Machine%20Learning%20Sections/Re… 10/13
22.1.2018 Advanced Recommender Systems with Python
Movies Recommendation:
Music Recommendation:
Books Recommendation:
Institut für Informatik, Universität Freiburg - Book Ratings Data Sets http://www.informatik.uni-
freiburg.de/~cziegler/BX/ (http://www.informatik.uni-freiburg.de/~cziegler/BX/) Food Recommendation:
Healthcare Recommendation:
http://localhost:8888/nbconvert/html/Downloads/Python-Data-Science-and-Machine-Learning-Bootcamp/Machine%20Learning%20Sections/Re… 11/13
22.1.2018 Advanced Recommender Systems with Python
Dating Recommendation:
Great Job!
http://localhost:8888/nbconvert/html/Downloads/Python-Data-Science-and-Machine-Learning-Bootcamp/Machine%20Learning%20Sections/Re… 12/13
22.1.2018 Advanced Recommender Systems with Python
http://localhost:8888/nbconvert/html/Downloads/Python-Data-Science-and-Machine-Learning-Bootcamp/Machine%20Learning%20Sections/Re… 13/13