3
0 Comments

Building a Product Recommendation System with Collaborative Filtering

Hello IndieHackers! I would like to share this tutorial with you, which I wrote in order to explain how recommendation algorithms work, in an easy to understand way for beginners.

I will go deeper into building a product recommendation system that we can better target customers with, using product recommendations that are tailored to individual customers. Studies have shown that customized product recommendations improve conversion rates and customer retention rates.

A product recommendation system is a system whose objective is to predict and compile a list of items that a customer is likely to buy. Referral systems have gained much popularity in recent years and have been developed and implemented for various commercial use cases

For example,

The media service provider, Netflix, uses referral systems to recommend movies or television programs for individual users who are likely to watch

The e-commerce company Amazon uses recommendation systems to predict and display a list of products that the customer is likely to buy

The music streaming service, Pandora, uses music recommendation systems for its listeners.

The use of a referral system does not stop here. It can also be used to recommend users related articles, news or books

With the potential to be used in a variety of areas, referral systems play a critical role in many businesses, especially e-commerce and media businesses, as they directly impact sales revenue and user engagement

Generally, there are two ways to develop a list of recommendations:

  1. Collaborative filtering
    The collaborative filtering method is based on users’ previous behaviors, such as the pages they saw, the products they bought, or the ratings they gave to different items. The collaborative filtering method uses this data to find similarities between users or articles, and recommends the most similar articles or content to users.

The basic assumption behind the collaborative filtering method is that those who have seen or bought similar content or products in the past are likely to see or buy similar types of content or products in the future.

Thus, based on this assumption, if one person bought items A, B, and C and another person bought items A, B, and D in the past, it is likely that the first person will buy item D and the other person will buy item C, since they share many similarities with each other

  1. Content-based filtering
    On the other hand, content-based filtering produces a list of recommendations based on the characteristics of an article or user. It usually examines the keywords that describe the characteristics of an article. The basic assumption of the content-based filtering method is that users are likely to see or buy items similar to those they have bought or seen in the past

For example, if a user has listened to some songs in the past, the content-based filtering method will recommend similar song types that share similar characteristics to those the user has already heard

Building a Product Recommendation System with Collaborative Filtering
As mentioned, a collaborative filtering algorithm is used to recommend products based on user behavior history and similarities between them. The first step in implementing a collaborative filtering algorithm for a product recommendation system is to build a user-to-item matrix

A user-to-item matrix comprises individual users in the rows and individual elements in the columns. It will be easier to explain with an example. Take a look at the following matrix

The rows of this matrix represent each user and the columns represent each element. The values in each cell represent whether the given user bought the given item or not. For example, user 1 has purchased items B and D and user 2 has purchased items A, B, C and E

To build a product recommendation system based on collaborative filtering, we need to first build this type of user-to-item matrix. With this user-to-item matrix, the next step in building a product recommendation system based on collaborative filtering is to calculate the similarities between users

To measure similarities, the similarity of cosines is often used. The equation for computing cosine similarity between two users looks like this

In this equation, U1 and U2 represent user 1 and user 2. P1i and P2i represent each product, i, that user 1 and user 2 have purchased. If you use this equation, you will get 0.353553 as the cosine similarity between users 1 and 2 in the example above and 0.866025 as the cosine similarity between users 2 and 4

As you can imagine, the greater the similarity of the cosine, the more similar the two users. Thus, in our example, users 2 and 4 are more similar to each other than users 1 and 2. Finally, when using a collaborative filtering algorithm for product recommendations, there are two approaches that can be taken: a user-based approach and an item-based approach

As the names suggest, the user-based approach to collaborative filtering uses the similarities between users. On the other hand, the article-based collaborative filtering approach uses the similarities between the items. This means that when we calculate the similarities between the two users in the collaborative filtering of the user-based approach, we need to build and use a user-to-article matrix, as we have discussed above

However, for the item-based approach, we need to calculate the similarities between the two elements, and this means that we need to build and use an item-to-user matrix, which we can obtain by simply transposing the user-to-item matrix.

Let’s discuss how to build a product recommendation system using Python. We will begin this section by analyzing some e-commerce business data and then discuss the two approaches to building a product recommendation system with collaborative filtering

We will use one of the publicly available data sets from the UCI’s Machine Learning Repository, which can be found at this link.

You can follow this link and download the data in Microsoft Excel format, in a file called Online Retail.xlsx

You can download the code and the whole explanation here: https://www.narrativetext.co/the-analyst/building-a-product-recommendation-system-with-collaborative-filtering

on February 9, 2021