Hybrid Recommendation System using Python

By combining two or more different recommendation systems, a hybrid recommendation system provides a thorough and well-rounded approach. It strives to give customers more precise, varied, and personalized suggestions by utilizing the advantages of various methodologies and delivering a beneficial user experience. This tutorial is for you if you want to learn how to create a hybrid recommendation system. I'll walk you through creating a Python-based hybrid recommendation system in this tutorial.

What is a Hybrid Recommendation System?

To give customers recommendations that are more accurate and varied, a hybrid recommendation system incorporates different recommendation algorithms. It improves the recommendation process by utilizing the advantages of many techniques, such as group filtering and filtering based on content, to get around their drawbacks.

Group filtering and filtering by content must be terms you are familiar with. In order to generate suggestions, group filtering examines user-item interactions and finds commonalities between people or objects. It suggests products that consumers or users with comparable preferences have enjoyed or used. However, it could have trouble with specialized or new products with little user interaction.

Contrarily, content-based filtering emphasizes an item's features and qualities to suggest comparable things to consumers based on their preferences. It looks for characteristics such as brand names, product descriptions, category names, and user profiles.

It could, however, fail to adequately capture a variety of user preferences and produce less varied recommendations.

A hybrid recommendation system is useful in this situation. We can overcome the drawbacks of group filtering and filtering by content by integrating them into a hybrid approach. While the content-driven filtering component takes into consideration the unique characteristics and properties of each item, the group-based filtering component taps into the collective knowledge of the public. When user-item interactions are uncommon or personalized suggestions are needed, this combination enables the system to provide more accurate recommendations.

By this point, you know what a hybrid recommendation system is. I'll walk you through creating a Python hybrid recommendation system in the following part.

Data Exploration and Analysis

The dataset that we can use for this task is available below.

Dataset Overview of Hybrid Recommendations

E-commerce systems must recognize individual tastes and provide customized suggestions to boost consumer satisfaction and sales in the fashion business. By using a hybrid recommendation system, we can give clients recommendations that are more specific and tailored.

Information on user-product interactions at an online clothing business is contained in the dataset. The dataset features the following traits:

  • User ID: A unique identifier for each user.
  • Item ID: Each product has a unique identification number.
  • Product name: The name or a summary of the thing.
  • Brand: The manufacturer or name of the goods.
  • Category: The proper category for the product (for instance, Men's Fashion or Women's Fashion).
  • Price: The item's price.
  • Consumer rating of the product (on a scale of 1 to 5).
  • Color: The hue of the item.
  • Size: The item's measurements.

You aim to develop a hybrid recommender system combining group and filtering based on content approaches. Finding people similar to one another may be done through group filtering, which can then be used to recommend products that various users who are similar to them have liked. Contrarily, you may utilize filtering based on content to contrast the merits of other products by examining details like item title, brand, and category.

Main table (for reference purpose only):

User IDItem IDProduct NameBrandCategoryPriceRatingColorSize
191DressesAdidasMen's Fashion401.043159RedXLL
972ShoesH&MWomen's Fashion544.044414RedL
253DressesAdidasWomen's Fashion443.337935YellowXLL
574ShoesZaraMen's Fashion431.049543BlackS
795T-shirtsAdidasMen's Fashion794.304773RedM
986DressAdidasMen's Fashion471.379544YellowL
167JeanGucciMen's Fashion371.35475BlackXLL
638SweatZaraKids' Fashion444.340303BlueXLL
969SweatH&MMen's Fashion534.444154GreenXLL
3610T-shirtsZaraKids' Fashion554.093434BlackXLL
6911T-shirtsAdidasMen's Fashion511.140955RedS
8712SweateGucciKids' Fashion914.499734YellowM
913JeansNikeKids' Fashion351.401194RedM
5014DressZaraWomen's Fashion344.941004BlackL
3115ShoesZaraMen's Fashion543.470414YellowM
3716DressAdidasWomen's Fashion471.444714BlueS
4117DressGucciWomen's Fashion751.450434BlueXLL
1518JeansGucciWomen's Fashion734.495445RedXLL
8419ShoesGucciWomen's Fashion543.475341BlackM
5620DresGucciMen's Fashion533.755445GreenL
8721JeanH&MMen's Fashion544.409557YellowS
5622JeanGucciWomen's Fashion594.545434GreenL
6023JeanZaraKids' Fashion914.074745YellowM
6424JeanZaraWomen's Fashion154.499754YellowXLL
6525SweateH&MsKids' Fashion351.040493GreenS
2626DressGucci'sWomen's Fashion414.745149GreenS
8227T-shirtsAdidasKids' Fashion743.301743RedL
8728ShoesZaraWomen's Fashion344.544503YellowL
9829ShoesGucciWomen's Fashion554.940955YellowM

Python: Hybrid Recommendation System

Let's begin the process of creating a hybrid recommendation system by loading the dataset and the required Python libraries:

Source Code Snippet:

Output:

   User ID  Item ID Product Name   Brand         Category  Price    Rating  \
0       19           1        Dress  Adidas    Men Fashion     40  1.043159   
1       97           2        Shoes     H&M  Women Fashion     82  4.026416   
2       25           3        Dress  Adidas  Women Fashion     44  3.337938   
3       57           4        Shoes    Zara    Men Fashion     23  1.049523   
4       79           5      T-shirt  Adidas    Men Fashion     79  4.302773   
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - -
    Color Size  
0   Red   XLL  
1   Red    L  
2  Yellow   XLL  
3   Black    S  
4   Red    M  

Explanation: Thus, the fashion goods for women, men, and children are the basis for this statistic. We aim to design two recommendation systems employing group and content-based filtering, then merge the two systems to produce a hybrid recommendation system.

Let's start by importing the Python libraries that we will need for the remaining steps of the task:

Source Code Snippet:

Explanation: we imported the Surprise library, which you might need to become more familiar with, into the code above. To utilize the SVD algorithm, the surprise library is loaded. Singular Value Decomposition is referred to as SVD. It is a matrix factorization method frequently applied in group filtering algorithms. You may install it on your computers by using the following command:

  • To use the command line or terminal, type pip and install sci-kit-surprise.
  • To use sci-kit-surprise in Colab Notebook, type! pip install.

First Approach: Content-Based Filtering

Let's proceed by developing a recommendation system that makes use of content-based filtering:

Source Code Snippet:

Explanation: The content-based filtering element of the hybrid recommender system is implemented in the code above. From the dataset, we first chose the Item ID, name, company, category, colors, size, and other pertinent attributes. Then, for each product, we aggregated all of these attributes into a single "Content" column.

The material was then transformed into a TF'S-IDF1 feature matrix using the Term Rate-Inverse Document Frequency (TF'S-IDF1) vectorizer. This matrix shows how significant each word is in the content when compared to the entire corpus.

The cosine similarity metric was then used to determine the degree of similarity between items based on their contents. Based on their content, the products in this similarity matrix are comparable to one another.

We initially located the index of the desired product in a matrix of similarities to obtain content-based suggestions. The top N comparable goods were then chosen after we sorted the similarity ratings in descending order. Last but not least, we gave the suggested Item IDs back.

Second Approach: Group Filtering

Using group filtering, let's create a recommendation system now.

Source Code Snippet:

Explanation: We used the Singular Value Decomposition method to create the group filtering portion of the hybrid recommender system in the code above.

The SVD method was first initialized and trained using the dataset. This stage entails breaking down the user-item rating matrix to identify the deeper trends and latent elements that influence user preferences.

The next step was to develop a test set of user-item pairings that needed to be in a training set to provide group filtering suggestions. This test set has been filtered only to contain items that belong to the target user identified by user__id.

The test set object ratings were then predicted using the trained SVD model. These forecasts are an estimate of the ratings the user would give the goods.

The forecasts are then arranged in descending order by their projected ratings. We choose the top N products with the best-predicted ratings to serve as the user's group filtering suggestions.

Lastly, the hybrid strategy

Let's now employ the Hybrid approach to mix content-driven and group filtering techniques to create a recommendation system:

Source Code Snippet:

Explanation: To construct a hybrid recommender system in the code above, we merged content-based and group filtering methodologies.

The user ID, Item ID, and the required number of top n recommendations are all inputs to the get__hybrid__recommendations method.

To receive a list of content-based suggestions for the given product__id, it first uses the get__content__based__recommendations method. These suggestions are based on how closely the traits of the provided product match those of other items in the dataset.

To obtain a list of group filtering recommendations for the given user__id, it then uses the get__group__filtering__recommendations method. These suggestions are made by drawing on previous user-item interactions and making assumptions about user preferences from prior user behavior.

The suggestions from the group filtering and content-based filtering are then combined by taking a combination of the two lists. It guarantees that content-based and group filtering suggestions based on customer preferences are included in hybrid recommendations.

To provide product recommendations based on the item a user is now browsing, follow these steps:

Source Code Snippet:

Output:

Hybrid Recommendations for User 6 based on Product 11:
1- - - > Item ID: 9281
1. - - - > Item ID: 9218
2. - - - >Item ID: 1311
2- - - >. Item ID: 1311
3. - - - >Item ID: 4511
3. - - - > Item ID: 4511
4. - - - > Item ID: 8371
4. - - - >Item ID: 8371
5. - - - >Item ID: 8751
5. - - - > Item ID: 8751
6. - - - >Item ID: 5941
6. - - - > Item ID: 5941
7. - - - >Item ID: 14631
7. - - - > Item ID: 14631
8. - - - >Item ID: 16881
8. - - - >Item ID: 16881
9. - - - >Item ID: 6011
9. - - - >Item ID: 6011
10. - - - >Item ID: 15616
10. - - - >Item ID: 15661

Summary

So, here is how you use Python to build a hybrid recommendation system. To give customers recommendations that are more accurate and varied, a hybrid recommendation system incorporates different recommendation algorithms. It improves the recommendation process by utilizing the advantages of many techniques, such as group filtering and filtering by content, to get around their drawbacks. I hope you enjoyed reading this tutorial on using Python to build a hybrid recommendation system.