Abdulrahman Elsayed Badawy

Data AnalysisMachine learning

Twitter Sentiment Analysis (DEPI Data Science Track, Grade: A)

1.6 million tweets explored, a DistilBERT classifier fine-tuned and tracked with MLflow, then served through a Flask API and a web page in a Docker image.

  • TypeMachine learning
  • WhenMay 2025
  • 1.6Mtweets explored
  • 86.9%accuracy on the test set
  • 11.5%of the model’s weights trained
  • 3phases: explore, train, serve
Twitter Sentiment Analysis (DEPI Data Science Track, Grade: A)

Highlights

  • Explored 1.6M tweets from 659,775 users: n-grams, hashtags, sentiment by hour, day and month
  • Compared Naive Bayes, SVM, LSTM and DistilBERT; the fine-tuned DistilBERT reached 86.9% test accuracy and was served through a Dockerized Flask API

The problem

Short, informal text is hard to read at scale: slang, mentions, hashtags and sarcasm get in the way. The capstone of the DEPI Data Science track asked for the whole path, from raw data to something a person can try.

Approach

The work ran in three phases.

Explore: the Sentiment140 corpus (1.6 million tweets from 659,775 accounts, half positive and half negative) was profiled for words, phrases, hashtags, mentions, posting times and user behaviour.

Train: DistilBERT was fine-tuned with most of its layers frozen, so only 11.5% of the weights learn. Every run was tracked in MLflow and judged with a confusion matrix, precision, recall and F1.

Serve: a Flask API (POST /predict) and a small web page, packaged in a Docker image.

Results

  • 1,600,000 tweets explored: 208,988 distinct words, 12,091 hashtags and 343,646 mentions
  • Positive tweets use more varied language: 10% more distinct words, 20% more hashtags and 29% more mentions than negative ones
  • By the hour on the timestamp, positive tweets lead overnight (11 pm to 5 am) and negative ones through the day (7 am to 9 pm)
  • The fine-tuned DistilBERT reached 86.9% accuracy on the test set while training only 11.5% of its weights
  • A working API and web page in one Docker image

Tools

  • Python
  • pandas
  • NLTK
  • scikit-learn
  • Hugging Face Transformers
  • MLflow
  • Flask
  • Docker

Figures

Select a figure to enlarge it.

The project at a glance

The project at a glance

The three phases from left to right: exploring 1.6 million tweets, fine-tuning DistilBERT with most of its layers frozen, and serving the model through a Flask API, a web page and a Docker image.

What the exploration found

What the exploration found

Positive and negative tweets are equal in number but not in style: positive tweets use more distinct words, hashtags and mentions. Each small chart has its own scale.

Training the classifier

Training the classifier

Only the last transformer layer and the classifier learn: 7.7 of 67 million weights. The fine-tuned model reached 86.9% accuracy on the test set.

Sentiment by hour of day (the project’s own plot)

Sentiment by hour of day (the project’s own plot)

A chart produced by the exploration code: by the hour on the timestamp, positive tweets lead overnight and negative ones through the day.