Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

A Product Recommendation System in Python

  • Isha BansalIsha Bansal
  • July 6, 2022
  • Python Programming Examples
A Product Recommendation System in Python

There are tons of smart product recommendation tools out there. But as a developer, it makes more sense to understand exactly what goes on behind the scenes. Luckily, Python has a solution for everything, and that’s what we’re going to learn in this article.

Also Read: Theoretical Introduction to Recommendation Systems in Python

In this tutorial, we will be building a product recommendation system in Python programming language. Let us start off by understanding the dataset that we will be using for this tutorial.

For this tutorial, we will be using the Amazon Beauty Products Ratings Dataset which contains over 2 million customer reviews and ratings of Beauty related products sold on the website.

The dataset contains the following information about each product: UserID, which is unique to each customer and helps to identify a user; ProductID, which helps to identify a product uniquely, ratings, which range from 1 to 5, and a timestamp, which provides the time of the ratings.


Code Implementation

We will be importing all the necessary libraries and loading the dataset into the program. Make sure your .csv file is in the same directory as the code file to avoid any errors. Look at the code below.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("ratings_Beauty.csv")
print("Number of Products in the dataset : ",df.shape[0])

In the dataset, we can see that there are total 2023070 beauty product reviews. We will try to plot a bar graph that will display the value of the ratings against the count of the ratings.

This will help us understand how the user’s reviews are distributed among the 5 rating values, i.e., 1,2,3,4, and 5. Look at the code snippet below.

count_ratings = [0 for i in range(len(np.unique(df['Rating'])))]
print("Number of Unique Ratings available : ",len(count_ratings))

for i in range(df.shape[0]):
  count_ratings[int(df['Rating'][i]-1)]+=1

print("Count of each ratings is : ",count_ratings)

plt.style.use('seaborn')

labels = ["1 star" , "2 star", "3 star", 
          "4 star", "5 star"]

plt.figure(figsize=(15,8),facecolor="w")
ax = plt.barh(labels,count_ratings,
        color=["yellow","cyan","pink",
               "skyblue","lightgreen"],
        edgecolor="black")

for i in ax.patches:
    plt.text(i.get_width()+0.6, i.get_y()+0.3,
             str(round((i.get_width()), 4)),
             fontsize=15, fontweight='bold',
             color='grey')
  
plt.title("Horizontal Bar Graph - Ratings vs Count",fontsize=15)
plt.show()

After the code execution, the program will display the plot shown below. This plot will help us understand what users think about beauty products on Amazon.

Ratings Vs Count Beauty Products
Ratings Vs Count Beauty Products

Next, we will look at the recommendations in two ways: The first way would be to recommend the products which have the maximum 4- or 5-star ratings to the user.

Another way is to let the users know which users are making the 4- or 5-star ratings for the products which will help recognize that these are users whose ratings can be helpful as they are the most users.

Let’s filter out the data where the ratings are either 4- or 5-star ratings using the code below.

df_4 = df[df['Rating']==4.0]
df_5 = df[df['Rating']==5.0]
df_45 = pd.concat([df_4, df_5])

Next, we will be looking at the top products having the 4 star and 5-star rating and displaying it in form of a bar graph to know the most recommended products by the website according to the ratings

popular_products = pd.DataFrame(df_45.groupby('ProductId')['Rating'].count())
most_popular = popular_products.sort_values('Rating', ascending=False)[:10]

plt.figure(figsize=(15,8),facecolor="w")
most_popular.head(30).plot(kind = "barh")

plt.title("Products vs Count of Purchase",fontsize=15)
plt.show()

The code when executed results in the plot shown below. The plot shows that the product with the product_id: B001MA0QY2 is the most recommended product and we can see the top 10 popular products listed in the plot below.

Product Vs Count Reviews
Product Vs Count Reviews

Now let’s move on to the other approach to recommend the new users the products on the website according to the users who have rated the most on the website as they are the more frequent users of the beauty products. Look at the code and output below.

popular_users = pd.DataFrame(df_45.groupby('UserId')['Rating'].count())
most_popular_users = popular_users.sort_values('Rating', ascending=False)[:10]

plt.figure(figsize=(15,8),facecolor="w")
most_popular_users.head(30).plot(kind = "barh")

plt.title("UserIDs vs Count of Purchase",fontsize=15)
plt.show()

Have a look at the plot displayed by the code above which displays the most frequent users of the product.

Users Vs Count Reviews
Users Vs Count Reviews

Conclusion

In this tutorial, we learned about a product recommendation system using Python programming language.

Recommendation systems help understand what the current users of the products are liking and what interests them the most in order to help the new users understand what products they must try on.

Thank you for reading!


Isha Bansal
Isha Bansal

Hey there stranger!
Do check out my blogs if you are a keen learner!

Hope you like them!

Articles: 185
Support Vector Machines in Python
Previous Post Support Vector Machines in Python
Next Post Customizing the Pyvis Interactive Network Graphs
Customizing the Pyvis Interactive Network Graphs

Related Posts

How to Calculate Distance Between Two Points in Python

How to Calculate Distance Between Two Points in Python

  • November 26, 2026
CIFAR 10 Dataset: Load and Use in Python

CIFAR 10 Dataset: Load and Use in Python

  • November 24, 2026
Maximum A Posteriori Estimation in Python

Maximum A Posteriori Estimation in Python

  • November 24, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.