Recommender Systems

Original Source Here


If you have ever felt spied on by internet, then, you have experienced the recommender systems. From Amazon to Netflix, Spotify or even Asos, most e-commerce websites use it. Those websites suggest items (products) you might want to buy, something similar from what you’ve already checked or bought, or read, etc. in order for you to buy more and to make your life easier (but mostly to get your money…).

The different types of recommender systems:

There exist different types of recommender systems:

  • Non personalized recommenders
  • Content based filtering
  • Collaborative filtering (item based, user based)

Non personnalized recommender systems:

They give general recommendations, not according to your history. It can be popular, trending, new arrivals…

Mostly, they give you a selection of popular products. To know them, the weighted average ratings of each item is calculated.

You can also add some parameters, according to the age of the client for example, or its location etc…

Content-Based Recommender System

A content-based recommender works with data that the user provides.

According to the products you bought (the movies you saw, or the books you read… you get it!), it will recommend you similar products. To do so, it calculates the similiraty between the ones you bought, to the others in the list.

As the user provides more inputs or takes actions on the recommendations, the engine becomes more and more accurate.

Let’s take an example. We consider a user that has already rated movies : Movies A (rating = 2/10), B (rating = 10/10), C (rating = 8/10) and D (rating = 7/10). What other movie (E, F or G), the recommender system will recommend him?

We need to know the genres of the rated movies and make it a one hot encoded matrix and consider also their ratings.

Movies’ genres matrix (one hot encoded)

And for the non-rated movies:

None rated movies’ genres matrix
Ratings matrix

We multiply the one hot encoded matrix by the ratings and we get a weighted genre matrix:

Movies’ genres matrix (one hot encoded)*ratings

Now, to get the user’s profile, you need to sum the ratings by genres:

User-Genres ratings

We normalize it (by dividing by the sum of the ratings which is 17+10+25+18=70)

Normalized User-Genres ratings

Now, to predict the ratings of three new movies: E, F and G, we multiply the normalized user-genres ratings matrix by the non-rated one hot encoded matrix:

And summing it, we get the recommendation matrix:

The movie, with the highest weight will be recommended to the user. Here, movie G.

Collaborative filtering

The idea behind collaborative filtering is to consider users’ opinions on different items and recommend the best item to each user based on the user’s previous rankings and the opinion of other similar types of users.

User-Based Collaborative Filtering

The first step is to discover how similar the active user is to the other users.

To generate recommendation to user U:

1. Find users with similar rating pattern to U.

2. Infer top-k or missing ratings for user U based on aggregation of ratings of the similar users.

To do so, different similarity measurements can be used (ex: Cosine similarity, Euclidian distance…).

For example, we’ll calculate the cosine similiraty between users according to their movies’ ratings.

Similarity(User1,User4)= (8*9)/(8*9) = 1

Similarity(User2,User4)=[9*6+5*9+5*4]/[sqrt(9²+5²+5²)*sqrt(6²+9²+4²)]=119/135.9 = 0.87

Similarity(User3,User4)=[8*6+7*9]/[sqrt(8²+7²)*sqr(6²+9²)]=111/115 = 0.96

We want to recommend to User4 movie A or movie F.

Rating matrix:

Rating matrix (Movies A and F)

And the similarity matrix:

Similarity matrix (Users-User4)

Multiplication of both:

Rating matrix*Similatiry matrix

The normalized recommendation matrix is made by dividing each movie’s weighted sum by the sum of the similarity index.

Recommendation matrix (normalized by the sum of similarity):

Recommendation matrix

We obtained the predicted ratings of the user for movie A and movie F. The movie with the highest one will be recommended, which is Movie F.


A recommender system is a subclass of information filtering system that seeks to predict the “rating” or “preference” a user would give to an item.

Recommendation algorithms can be divided into two categories: collaborative approaches (user-user, item-item) based on user-item interaction matrix, and content based approaches (such as regression or classification models) that use prior information about users and/or items.

For the past years, the use of recommender systems has been thriving in many big industries due to the success of online stores.

You can check my repository using the different types of recommender systems (in Python) with real data (online bookstore):

If you have further questions, you can contact me on LinkedIn (click here).


Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: