This project develops a natural language processing (NLP) pipeline for classifying Amazon product reviews as positive or negative. The workflow combines linguistic text preprocessing with TF-IDF feature extraction and logistic regression classification.
Raw review text is cleaned and normalized using spaCy before being transformed into TF-IDF features. A baseline logistic regression classifier is then compared with models optimized through five-fold cross-validation and grid search.
The final optimized model achieved 77% test accuracy and 84.21% precision, improving on the baseline model's 72% accuracy and 69.81% precision.
- Build an end-to-end NLP workflow for binary sentiment classification.
- Clean and normalize unstructured review text using linguistic preprocessing.
- Convert processed text into numerical features using TF-IDF.
- Establish baseline performance using logistic regression.
- Optimize both vectorization and classification hyperparameters using cross-validation.
- Evaluate the effect of optimization on accuracy, precision, recall, and F1 score.
- Analyze the tradeoff between precision and recall introduced by model optimization.
The optimized model improved overall accuracy from 72% to 77% and increased precision from 69.81% to 84.21%.
| Metric | Baseline | Optimized |
|---|---|---|
| Accuracy | 72.00% | 77.00% |
| Precision | 69.81% | 84.21% |
| Recall | 75.51% | 65.31% |
| F1 Score | 72.55% | 73.56% |
Optimization produced a substantial improvement in precision while reducing recall. The optimized classifier therefore became more reliable when predicting positive sentiment, but failed to identify a larger proportion of positive reviews.
The baseline confusion matrix contained:
- 35 true negatives
- 16 false positives
- 12 false negatives
- 37 true positives
After optimization, the confusion matrix contained:
- 45 true negatives
- 6 false positives
- 17 false negatives
- 32 true positives
This reduced false-positive predictions from 16 to 6 while increasing false negatives from 12 to 17.
The final model uses a scikit-learn pipeline combining TF-IDF vectorization and logistic regression.
Five-fold GridSearchCV optimization selected:
| Hyperparameter | Selected Value |
|---|---|
| Logistic Regression C | 0.1 |
| Solver | liblinear |
| Maximum TF-IDF Features | 3,000 |
| Minimum Document Frequency | 1 |
| N-gram Range | (1, 2) |
An expanded hyperparameter search additionally evaluated max_df and a wider range of regularization strengths. It produced identical cross-validation and test performance, providing no improvement over the initial optimized model.
The selected pipeline is serialized with joblib for reuse without retraining.
Review titles and review bodies are combined into a single text field before preprocessing.
The preprocessing pipeline uses spaCy to:
- Convert text to lowercase.
- Remove URLs and email addresses.
- Normalize whitespace.
- Tokenize review text.
- Remove stopwords and punctuation.
- Remove non-alphabetic tokens.
- Lemmatize the remaining tokens.
- Recombine the processed tokens for vectorization.
The resulting text is then transformed into numerical features using TF-IDF.
The modeling workflow consists of:
Processed Review Text → TF-IDF Vectorization → Logistic Regression → Sentiment Prediction
The TF-IDF vectorizer is fit only on training data to prevent information from the test set from influencing the learned vocabulary or term weights.
During hyperparameter optimization, TF-IDF and logistic regression are combined in a scikit-learn Pipeline. This ensures that vectorization is independently fit within each cross-validation fold and prevents validation data from leaking into feature extraction.
The data used in this project were obtained from the Amazon Review Polarity Dataset distributed through Kaggle under the CC0 Public Domain license. A reproducible random sample of 500 reviews was selected for this project using random_state=42.
Amazon Review Polarity Dataset on Kaggle
The modeling dataset contains:
- 500 total reviews
- 400 training observations
- 100 held-out test observations
- Approximately balanced positive and negative sentiment classes
- 4,184 TF-IDF features in the baseline representation
The relatively small sample size makes this project an exploration of the complete NLP modeling workflow rather than an attempt to maximize sentiment-classification performance on the full Amazon corpus.
- Python
- pandas
- spaCy
- Natural Language Processing
- Text preprocessing
- Lemmatization
- TF-IDF
- Logistic Regression
- scikit-learn Pipelines
- GridSearchCV
- Cross-validation
- Hyperparameter tuning
- Classification evaluation
- Model serialization with joblib
Amazon_Review_Sentiment_Classification/
├── .gitignore
├── README.md
├── Sample_extraction.py
├── Preprocessing.ipynb
├── Sentiment_Classification.ipynb
├── data/
│ ├── Amazon_Review_Samples_500.csv
│ └── Proc_Amazon_Samples.csv
└── model/
└── sentiment_classifier.joblib
-
Download the Amazon Review Polarity Dataset from Kaggle.
-
Extract the downloaded dataset and place
train.csvin:amazon_review_polarity_csv/train.csv -
Run
Sample_extraction.pyto generate a reproducible 500-review sample atdata/Amazon_Review_Samples_500.csv. -
Run
Preprocessing.ipynbto clean and preprocess the review text and generatedata/Proc_Amazon_Samples.csv. -
Run
Sentiment_Classification.ipynbto train, optimize, and evaluate the sentiment classifier. -
The selected pipeline is saved to
model/sentiment_classifier.joblib.
Because the serialized model contains the complete scikit-learn pipeline, it retains both the fitted TF-IDF vectorizer and logistic regression classifier.
The primary limitation of this project is the small 500-review sample. The full Amazon Review Polarity dataset is substantially larger, and training on additional data would likely provide a more representative evaluation of model performance.
The preprocessing pipeline also removes stopwords, punctuation, and non-alphabetic tokens. While these steps reduce noise, they may remove information relevant to sentiment, including negation, emphasis, and numerical expressions.
TF-IDF represents text primarily through term occurrence and does not model contextual meaning in the way modern language models do.
Potential extensions include:
- Training and evaluating the classifier on a substantially larger portion of the Amazon Review Polarity dataset.
- Comparing different preprocessing strategies, particularly preservation of negation and punctuation.
- Evaluating additional linear classifiers such as Support Vector Machines.
- Testing alternative n-gram and TF-IDF configurations.
- Adjusting the classification threshold to explore the precision-recall tradeoff.
- Comparing the classical NLP pipeline with transformer-based sentiment classifiers.
- Performing more detailed error analysis to identify linguistic patterns associated with incorrect predictions.
Core dependencies include:
pandas
spacy
scikit-learn
matplotlib
joblib
The spaCy English language model en_core_web_sm is also required.