Javatpoint Logo
Javatpoint Logo

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 ID Item ID Product Name Brand Category Price Rating Color Size
19 1 Dresses Adidas Men's Fashion 40 1.043159 Red XLL
97 2 Shoes H&M Women's Fashion 54 4.044414 Red L
25 3 Dresses Adidas Women's Fashion 44 3.337935 Yellow XLL
57 4 Shoes Zara Men's Fashion 43 1.049543 Black S
79 5 T-shirts Adidas Men's Fashion 79 4.304773 Red M
98 6 Dress Adidas Men's Fashion 47 1.379544 Yellow L
16 7 Jean Gucci Men's Fashion 37 1.35475 Black XLL
63 8 Sweat Zara Kids' Fashion 44 4.340303 Blue XLL
96 9 Sweat H&M Men's Fashion 53 4.444154 Green XLL
36 10 T-shirts Zara Kids' Fashion 55 4.093434 Black XLL
69 11 T-shirts Adidas Men's Fashion 51 1.140955 Red S
87 12 Sweate Gucci Kids' Fashion 91 4.499734 Yellow M
9 13 Jeans Nike Kids' Fashion 35 1.401194 Red M
50 14 Dress Zara Women's Fashion 34 4.941004 Black L
31 15 Shoes Zara Men's Fashion 54 3.470414 Yellow M
37 16 Dress Adidas Women's Fashion 47 1.444714 Blue S
41 17 Dress Gucci Women's Fashion 75 1.450434 Blue XLL
15 18 Jeans Gucci Women's Fashion 73 4.495445 Red XLL
84 19 Shoes Gucci Women's Fashion 54 3.475341 Black M
56 20 Dres Gucci Men's Fashion 53 3.755445 Green L
87 21 Jean H&M Men's Fashion 54 4.409557 Yellow S
56 22 Jean Gucci Women's Fashion 59 4.545434 Green L
60 23 Jean Zara Kids' Fashion 91 4.074745 Yellow M
64 24 Jean Zara Women's Fashion 15 4.499754 Yellow XLL
65 25 Sweate H&Ms Kids' Fashion 35 1.040493 Green S
26 26 Dress Gucci's Women's Fashion 41 4.745149 Green S
82 27 T-shirts Adidas Kids' Fashion 74 3.301743 Red L
87 28 Shoes Zara Women's Fashion 34 4.544503 Yellow L
98 29 Shoes Gucci Women's Fashion 55 4.940955 Yellow M

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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA