Fine-tuning
bert-base-uncasedon airline tweets to classify whether a tweet is a complaint or non-complaint — achieving strong performance over a TF-IDF + Naive Bayes baseline.
This project implements a full NLP pipeline for binary sentiment classification on airline tweets, comparing:
| Approach | Description |
|---|---|
| Baseline | TF-IDF Vectorizer + Multinomial Naive Bayes |
| Fine-tuned | bert-base-uncased via Hugging Face Transformers + PyTorch |
The dataset contains 3400 tweets (1700 complaints, 1700 non-complaints) from airline handles, split into train/validation/test sets.
Bert-for-sentiment-analysis/
├── BERT-for-sentiment-analysis.ipynb # Main notebook (end-to-end pipeline)
├── requirements.txt # Python dependencies
├── .gitignore # Git ignore rules
└── README.md # This file
Input Tweet
↓
[BERT Tokenizer] — bert-base-uncased, max_len=64
↓
[BertModel] — 768-dim CLS token embedding
↓
[Linear(768 → 50)] → ReLU
↓
[Linear(50 → 2)] → Logits
↓
Complaint / Non-Complaint
- Load
complaint1700.csvandnoncomplaint1700.csv - Assign binary labels: complaint=0, non-complaint=1
- 80/20 train-validation split with
random_state=2020 - Text cleaning: lowercasing, removing mentions, punctuation normalization, stopword removal
TfidfVectorizerwithngram_range=(1,3), binary=TrueMultinomialNBwith hyperparameter tuning via 5-fold stratified cross-validation- Evaluated using AUC-ROC curve and accuracy
- Tokenization via
BertTokenizer(bert-base-uncased,max_length=64) - PyTorch
DataLoaderwith batch size 32 - Custom
BertClassifierwith a 2-layer classification head - Optimizer:
AdamW(lr=5e-5, eps=1e-8) - Scheduler: Linear warmup with Hugging Face
get_linear_schedule_with_warmup - Trained for 2 epochs on train set, evaluated on validation set
- Final model trained on the full dataset (train + val) for test predictions
- ROC-AUC curve plotted for both baseline and BERT
- Accuracy and AUC reported on validation set
- Test predictions generated with confidence threshold
0.9
- Python 3.8+
- CUDA-capable GPU (optional but recommended)
git clone https://github.com/DevyaniD19/Bert-for-sentiment-analysis.git
cd Bert-for-sentiment-analysis
pip install -r requirements.txtjupyter notebook BERT-for-sentiment-analysis.ipynbGoogle Colab: This notebook was developed on Colab. Simply upload it and run — all dependencies and data are downloaded automatically.
See requirements.txt for the full list. Key packages:
torch— deep learning frameworktransformers— Hugging Face BERT model & tokenizerscikit-learn— TF-IDF, Naive Bayes, evaluation metricspandas,numpy— data manipulationmatplotlib— ROC curve visualizationnltk— stopwords for text preprocessing
| Model | Validation AUC | Validation Accuracy |
|---|---|---|
| TF-IDF + Naive Bayes | ~0.87 | ~81% |
| Fine-tuned BERT | ~0.94+ | ~88%+ |
Results may vary slightly due to random seeds and hardware.
Devyani Deore 📧 devyanid@umich.edu 🔗 github.com/DevyaniD19
This project is licensed under the MIT License — see the LICENSE file for details.