E-Commerce Product Reviews using an NLP Model

Another exciting example of Natural Language Processing

In partnership with

Analytics on Live Data Without Leaving Postgres

When analytics on Postgres slows down, most teams add a second database. TimescaleDB by Tiger Data takes a different approach: extend Postgres with columnar storage and time-series primitives to run analytics on live data, no split architecture, no pipeline lag, no new query language to learn. Start building for free. No credit card required.

Project Overview

The E-Commerce Product Intelligence Dataset is a synthetically generated, multi-table relational dataset simulating 3.5 years of customer activity for a mid-size online retailer. It is designed to support the full spectrum of modern ML and data science workloads — from classic recommendation algorithms through graph neural networks to agentic AI evaluation.

Step 1: Importing the required libraries

import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

Step 2: Load and understand the data as per the raw format

ecommerce_data_users = pd.read_csv('/Users/Kaggle/eCommerce_Product_Dataset/users.csv')
print(ecommerce_data_users.head())

Step 3: Understand the size and shape of the data

ecommerce_data_users.size
90000
ecommerce_data_users.shape
(10000, 9)
ecommerce_data_users.columns
Index(['user_id', 'age', 'gender', 'country', 'city', 'signup_date',
       'income_level', 'preferred_category', 'loyalty_tier'],
      dtype='object')
ecommerce_data_users.ndim
2

Step 4: Importing Additional libraries to preprocess the data

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.pipeline import Pipeline

Step 5: Prepare the features (X) and (Y)

X = (ecommerce_data_interactions['product_id'].astype(str) + ' ' +
     ecommerce_data_interactions['user_id'].astype(str))
y = ecommerce_data_interactions['interaction_type'].astype(str)

Step 6: Split the values of X Train, Y Train, X Test and Y Test

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.4, random_state=42, stratify=y
)

Step 7: Creating the NLP pipeline and use fit function to train X_Train, Y_Train

nlp_pipeline = Pipeline([
    ('tfidf', TfidfVectorizer(max_features=10000, ngram_range=(1,2), stop_words='english')),
    ('clf', LogisticRegression(max_iter=1000, random_state=42))
])
nlp_pipeline.fit(X_train, y_train)
y_pred = nlp_pipeline.predict(X_test)
print("Confusion matrix:\n", confusion_matrix(y_test, y_pred))
print("Classification report:\n", classification_report(y_test, y_pred))

Step 8: Print the Accuracy, Confusion Matrix and Classification Report

print("Test accuracy:", accuracy_score(y_test, y_pred))
Test accuracy: 0.501325
Confusion matrix:
 [[    4     0    52     0     0  4688]
 [    5     1    30     0     0  4031]
 [    3     3    73     0     0  7914]
 [    2     0     9     0     0  1082]
 [    4     2    13     0     0  1899]
 [   15     4   191     0     0 19975]]

Classification report:
                       precision    recall  f1-score   support

         add_to_cart       0.12      0.00      0.00      4744
     add_to_wishlist       0.10      0.00      0.00      4067
               click       0.20      0.01      0.02      7993
    remove_from_cart       0.00      0.00      0.00      1093
remove_from_wishlist       0.00      0.00      0.00      1918
                view       0.50      0.99      0.67     20185

            accuracy                           0.50     40000
           macro avg       0.15      0.17      0.11     40000
        weighted avg       0.32      0.50      0.34     40000