Flipkart Review Sentiment Analysis using DistilBERT
1. Model Overview
This model is a fine-tuned DistilBERT-based text classification model developed to classify Flipkart product reviews into three sentiment categories:
- Negative (0)
- Neutral (1)
- Positive (2)
The model uses the pretrained distilbert/distilbert-base-uncased architecture and was fine-tuned on a Flipkart product review dataset.
Model type: Multi-class Text Classification
Base model: DistilBERT Base Uncased
Framework: PyTorch and Hugging Face Transformers
Number of classes: 3
2. Project Objective
The objective of this project is to build an NLP-based sentiment classification system that can automatically understand the sentiment expressed in customer product reviews.
The model can help analyze customer opinions and identify positive, negative and neutral feedback.
3. Dataset Description
The model was trained using a Flipkart product review dataset (Dataset-SA.csv).
The original dataset contains 205,052 records and six columns:
- Product Name
- Product Price
- Rating
- Review
- Summary
- Sentiment
The Review and Summary columns were combined to create the input text.
After preprocessing, removal of conflicting labels and duplicate texts, the final dataset contained 109,653 unique review texts.
Train-Test Split
| Dataset | Records |
|---|---|
| Training set | 87,722 |
| Testing set | 21,931 |
| Total | 109,653 |
The dataset was split using an 80:20 stratified train-test split with random_state=42.
Class Distribution
| Sentiment | Percentage |
|---|---|
| Negative | 19.0% |
| Neutral | 5.5% |
| Positive | 75.5% |
The dataset is imbalanced, with positive reviews representing the majority class.
4. Data Preprocessing
The following preprocessing steps were performed:
- Combined Review and Summary text.
- Handled missing review and summary values.
- Removed records with empty combined text.
- Normalized sentiment labels.
- Converted sentiment labels into numerical classes.
- Removed conflicting identical review texts that had different sentiment labels.
- Removed duplicate review texts.
- Applied stratified train-test splitting.
For DistilBERT, the original combined review text was retained rather than applying aggressive text cleaning.
5. Model Architecture and Training
The pretrained DistilBERT model was fine-tuned for three-class sentiment classification.
Training Configuration
| Parameter | Value |
|---|---|
| Base model | distilbert-base-uncased |
| Epochs | 2 |
| Batch size | 16 |
| Learning rate | 2e-5 |
| Optimizer | AdamW |
| Weight decay | 0.01 |
| Maximum sequence length | 128 |
| Scheduler | Linear learning-rate scheduler |
| Warmup ratio | 10% |
| Loss function | Cross-Entropy Loss |
| Hardware | NVIDIA Tesla T4 GPU |
Training Loss
| Epoch | Average Training Loss |
|---|---|
| Epoch 1 | 0.2555 |
| Epoch 2 | 0.1651 |
Training loss decreased by approximately 35.4% between the first and second epochs.
6. Model Evaluation
The model was evaluated on the held-out test dataset containing 21,931 reviews.
Overall Performance
| Metric | Score |
|---|---|
| Accuracy | 93.10% |
| Macro Precision | 80.74% |
| Macro Recall | 75.77% |
| Macro F1-score | 77.60% |
Classification Report
| Sentiment | Precision | Recall | F1-score |
|---|---|---|---|
| Negative | 0.87 | 0.90 | 0.89 |
| Neutral | 0.59 | 0.39 | 0.47 |
| Positive | 0.96 | 0.98 | 0.97 |
The model performs strongly on positive and negative reviews. Neutral sentiment remains the most challenging class.
Confusion Matrix
Rows represent actual sentiment, and columns represent predicted sentiment.
| Actual / Predicted | Negative | Neutral | Positive |
|---|---|---|---|
| Negative | 3,760 | 191 | 213 |
| Neutral | 315 | 473 | 415 |
| Positive | 240 | 140 | 16,184 |
7. Limitations
- The dataset contains significantly more positive reviews than neutral reviews.
- Neutral sentiment classification remains relatively weak, with an F1-score of 0.47.
- Some reviews contain mixed sentiments, spelling mistakes, informal language and ambiguous expressions.
- The original dataset may contain noisy or inconsistent sentiment labels.
- Model predictions may not always reflect the actual intention of a reviewer.
- The model was trained on Flipkart product reviews and may not generalize equally well to other domains, languages or types of text.
The model should be treated as a sentiment prediction tool, not as a perfect indicator of customer opinion.
8. How to Use the Model
Install the required libraries:
pip install transformers torch
Load the model using Hugging Face Transformers:
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
MODEL_NAME = "SharathLal/flipkart-distilbert-sentiment-analysis"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
model.eval()
def predict_sentiment(text):
inputs = tokenizer(
text,
return_tensors="pt",
truncation=True,
padding=True,
max_length=128
)
inputs = {key: value.to(device) for key, value in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
probabilities = torch.softmax(outputs.logits, dim=1)
predicted_class = torch.argmax(probabilities, dim=1).item()
sentiment = model.config.id2label[predicted_class]
confidence = probabilities[0][predicted_class].item()
return {
"sentiment": sentiment,
"confidence": round(confidence, 4)
}
# Example prediction
result = predict_sentiment(
"The product quality is excellent and I really love it!"
)
print(result)
9. Model Repository
Hugging Face Model Repository:
https://huggingface.co/SharathLal/flipkart-distilbert-sentiment-analysis
10. Acknowledgements
- Hugging Face Transformers
- DistilBERT pretrained model
- PyTorch
- Flipkart product review dataset
Developed as an end-to-end NLP and Deep Learning portfolio project.