This project aims to automatically classify the rating (1–5 stars) of text reviews for data science books. The model used is DistilBERT, a pre-trained Transformer optimized for efficiency and performance.
- Dataset analysis
- Preprocessing and balancing
- Tokenization
- Tensor creation
- Model fine-tuning
- Evaluation and error analysis
- Inference using the Hugging Face Pipeline
Name: Amazon Reviews Science Size: 20,647 reviews Format: text + rating (1–5) Source: local CSV file
-
Main columns: comment → review text stars → numerical rating book_url → link to the book
-
Exploratory Analysis: Strong imbalance toward 5-star reviews Average review length: 87 words Outliers: from 1 word to 4,868 words
- Rating distribution
- Review length distribution
- Class balance
- Convert ratings from 1–5 to 0–4
- Remove empty reviews or reviews with only one word
- Analyze text length
- Undersample the training set to balance classes
- 80% train
- 10% validation
- 10% test
Tokenization:
- Model: distilbert-base-uncased
- Parameters: max_length=256, padding="max_length",truncation=True
Tensor creation: Data converted into Hugging Face Dataset objects with columns:
- input_ids
- attention_mask
- label
- Model used: DistilBertForSequenceClassification
- num_labels=5
- Optimized for text classification
- Trained with Hugging Face Trainer
Training parameters:
- Epochs: 3
- Batch size: 8
- Metrics: accuracy
- Checkpoints saved and evaluated at each epoch
- Validation accuracy
- Test accuracy
- Best performance
- Difficulty
- Classify new reviews
- Visualize probabilities using softmax
- Interpret the final prediction
- Python
- PyTorch
- Hugging Face Transformers
- Hugging Face Datasets
- Pandas
- Matplotlib / Seaborn