Skip to content

ruthuraraj-ml/Twitter-Sentiment-Analysis-Deep-Learning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐦 Twitter Sentiment Analysis using Deep Learning and Transformer Architectures

πŸ“Œ Project Overview

This project presents a comprehensive comparative study of deep learning and transformer-based architectures for Twitter sentiment classification.

Four different models were implemented and evaluated on a large-scale Twitter sentiment dataset containing over 1 million tweets:

  • πŸ”Ή Simple Recurrent Neural Network (RNN)
  • πŸ”Ή Long Short-Term Memory Network (LSTM)
  • πŸ”Ή Gated Recurrent Unit (GRU)
  • πŸ”Ή Bidirectional Encoder Representations from Transformers (BERT)

The objective is to analyze how modern neural architectures improve sentiment classification performance and identify the most suitable model for large-scale sentiment analysis.


πŸ“Š Dataset Information

Attribute Value
Total Tweets 1,048,572
Features 6
Negative Tweets 799,996
Positive Tweets 248,576
Sentiment Classes Binary Classification

Dataset Class Distribution

Distribution of positive and negative sentiment classes.


πŸ—οΈ Project Workflow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚          Twitter Sentiment Dataset          β”‚
β”‚            1,048,572 Tweets                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚
                      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         Exploratory Data Analysis           β”‚
β”‚ β€’ Class Distribution                        β”‚
β”‚ β€’ Tweet Length Analysis                     β”‚
β”‚ β€’ Word Frequency Analysis                   β”‚
β”‚ β€’ Positive/Negative Word Clouds             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚
                      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚            Text Preprocessing               β”‚
β”‚ β€’ Lowercasing                               β”‚
β”‚ β€’ URL & Username Removal                    β”‚
β”‚ β€’ Punctuation Removal                       β”‚
β”‚ β€’ Whitespace Normalization                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚
                      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚          Train / Validation / Test          β”‚
β”‚        175k / 37.5k / 37.5k Samples         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚           β”‚           β”‚
          β–Ό           β–Ό           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Vocabulary  β”‚ β”‚ Vocabulary  β”‚ β”‚ Vocabulary  β”‚
β”‚ 20,000      β”‚ β”‚ 20,000      β”‚ β”‚ 20,000      β”‚
β”‚ Max Len=32  β”‚ β”‚ Max Len=32  β”‚ β”‚ Max Len=32  β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚               β”‚               β”‚
       β–Ό               β–Ό               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     RNN     β”‚ β”‚    LSTM     β”‚ β”‚     GRU     β”‚
β”‚ Train from  β”‚ β”‚ Train from  β”‚ β”‚ Train from  β”‚
β”‚ Scratch     β”‚ β”‚ Scratch     β”‚ β”‚ Scratch     β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚               β”‚               β”‚
       β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚               β”‚
               β–Ό               β”‚
      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”‚
      β”‚ Performance     β”‚      β”‚
      β”‚ Evaluation      β”‚      β”‚
      β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β”‚
               β”‚               β”‚
               β–Ό               β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”‚
        β”‚ Accuracy    β”‚        β”‚
        β”‚ Precision   β”‚        β”‚
        β”‚ Recall      β”‚        β”‚
        β”‚ F1 Score    β”‚        β”‚
        β”‚ ROC-AUC     β”‚        β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β”‚
                               β”‚
                               β–Ό

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  BERT                       β”‚
β”‚ bert-base-uncased                           β”‚
β”‚ Pretrained Transformer                      β”‚
β”‚ Full Fine-Tuning                            β”‚
β”‚ Max Length = 48                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚
                      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           Comparative Analysis              β”‚
β”‚ RNN vs LSTM vs GRU vs BERT                  β”‚
β”‚ Accuracy β€’ Precision β€’ Recall               β”‚
β”‚ F1 Score β€’ ROC-AUC                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚
                      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              Best Model: BERT               β”‚
β”‚ Accuracy : 87.72%                           β”‚
β”‚ F1 Score : 72.48%                           β”‚
β”‚ ROC-AUC  : 92.21%                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

The project follows a complete NLP pipeline:

  1. Exploratory Data Analysis
  2. Text Preprocessing
  3. Dataset Splitting
  4. Sequence Preparation
  5. Model Development
  6. Performance Evaluation
  7. Comparative Analysis
  8. Best Model Selection

πŸ” Exploratory Data Analysis

Key findings from the dataset:

  • No missing values detected
  • Strong class imbalance (76% Negative, 24% Positive)
  • Average tweet length β‰ˆ 74 characters
  • Median tweet length β‰ˆ 70 characters
  • Significant vocabulary overlap between sentiment classes

Word Cloud Analysis

Most frequently occurring words in positive and negative tweets.


🧹 Text Preprocessing

The following preprocessing pipeline was applied:

  • Lowercasing
  • URL Removal
  • Username Removal
  • Punctuation Removal
  • Numeric Filtering
  • Whitespace Normalization

Sequence Preparation

RNN / LSTM / GRU

  • Vocabulary Size: 20,000
  • Maximum Sequence Length: 32

BERT

  • Pretrained BERT Tokenizer
  • Maximum Sequence Length: 48
  • Attention Masks
  • Special Tokens

🧠 Models Implemented

1️⃣ Simple RNN

Baseline recurrent neural network trained from scratch.

Architecture

  • Embedding Layer (128)
  • Simple RNN Layer (128 Hidden Units)
  • Dropout (0.3)
  • Dense Output Layer

RNN Training Dynamics

Training and validation loss during RNN training.


2️⃣ LSTM

Long Short-Term Memory architecture designed to capture long-range contextual dependencies.

Architecture

  • Embedding Layer (128)
  • LSTM Layer (128 Hidden Units)
  • Dropout (0.3)
  • Dense Output Layer

LSTM Training Dynamics

Training and validation loss during LSTM training.


3️⃣ GRU

Gated Recurrent Unit architecture providing efficient sequence modeling with fewer parameters.

Architecture

  • Embedding Layer (128)
  • GRU Layer (128 Hidden Units)
  • Dropout (0.3)
  • Dense Output Layer

GRU Training Dynamics

Training and validation loss during GRU training.


4️⃣ BERT

Pretrained transformer model fine-tuned end-to-end for sentiment classification.

Architecture

  • BERT Base (Uncased)
  • Transformer Encoder
  • Classification Head

Training Strategy

  • Full Fine-Tuning
  • AdamW Optimizer
  • Learning Rate = 2e-5
  • Best Model Checkpointing

BERT Training Dynamics

Training and validation loss during BERT fine-tuning.


πŸ“ˆ Model Performance Comparison

Model Accuracy Precision Recall F1 Score ROC-AUC
RNN 0.7530 0.4820 0.5637 0.5197 0.7084
LSTM 0.8117 0.5789 0.7541 0.6550 0.8723
GRU 0.8030 0.5613 0.7730 0.6504 0.8697
BERT 0.8772 0.7730 0.6822 0.7248 0.9221

πŸ“Š Comparative Analysis

Overall Model Comparison

Comparison of Accuracy, Precision, Recall, F1 Score, and ROC-AUC across all architectures.


F1 Score Comparison


ROC-AUC Comparison


πŸ† Key Findings

RNN

  • Established baseline performance
  • Limited contextual understanding
  • Lowest overall performance

LSTM

  • Significant improvement over vanilla RNN
  • Strong sequence modeling capability
  • Approximately 26% improvement in F1 Score

GRU

  • Comparable performance to LSTM
  • Faster and computationally efficient
  • Highest Recall score

BERT

  • Best overall performance
  • Highest Accuracy, Precision, F1 Score, and ROC-AUC
  • Benefited from pretrained contextual language representations

🎯 Best Model

πŸ₯‡ BERT

Metric Score
Accuracy 87.72%
Precision 77.30%
Recall 68.22%
F1 Score 72.48%
ROC-AUC 92.21%

πŸ“‚ Project Structure

sentiment-analysis-comparison/

β”œβ”€β”€ data/
β”œβ”€β”€ notebooks/
β”‚   β”œβ”€β”€ 01_EDA.ipynb
β”‚   β”œβ”€β”€ 02_Preprocessing.ipynb
β”‚   β”œβ”€β”€ 03_RNN.ipynb
β”‚   β”œβ”€β”€ 04_LSTM.ipynb
β”‚   β”œβ”€β”€ 05_GRU.ipynb
β”‚   β”œβ”€β”€ 06_BERT.ipynb
β”‚   └── 07_Comparative_Analysis.ipynb
β”‚
β”œβ”€β”€ models/
β”œβ”€β”€ images/
β”œβ”€β”€ reports/
β”œβ”€β”€ results/
β”‚
β”œβ”€β”€ README.md
└── requirements.txt

πŸ› οΈ Technologies Used

Programming

  • Python

Data Processing

  • Pandas
  • NumPy

Visualization

  • Matplotlib
  • Seaborn
  • WordCloud

Deep Learning & NLP

  • PyTorch
  • Hugging Face Transformers
  • Scikit-Learn

πŸš€ Future Improvements

  • Hyperparameter Optimization
  • Threshold Tuning
  • RoBERTa Comparison
  • DeBERTa Comparison
  • Multi-Class Sentiment Classification
  • Real-Time Sentiment Monitoring Dashboard
  • FastAPI Deployment
  • Streamlit Application

πŸ“œ Conclusion

This project demonstrates the progression of sentiment classification performance from traditional recurrent neural networks to transformer-based language models.

While LSTM and GRU significantly outperformed the vanilla RNN baseline, BERT achieved the strongest overall performance with an Accuracy of 87.72%, an F1 Score of 72.48%, and a ROC-AUC of 92.21%.

The results highlight the effectiveness of transfer learning and contextual language understanding in modern Natural Language Processing applications.

About

End-to-end sentiment analysis project comparing RNN, LSTM, GRU, and BERT on over 1 million Twitter posts using PyTorch and Hugging Face Transformers.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages