Natural Language Processing

Twitter Sentiment Analysis with TF-IDF and Multinomial Naïve Bayes

A classical NLP pipeline on 200k balanced tweets — TF-IDF (10k unigrams + bigrams) into Multinomial Naïve Bayes, 74.84% accuracy.

Course research on the Sentiment140 corpus. 1.6M tweets are balanced down to 200k (100k positive + 100k negative), run through a deep preprocessing chain — lowercasing, URL/@/# removal, punctuation stripping, stopword removal, WordNet synonym substitution, Porter stemming, and lemmatization — then vectorized with TF-IDF and classified by Multinomial Naïve Bayes on a stratified 80/20 split.

74.84%
accuracy
200,000
balanced tweets
10,000
TF-IDF features
40,000
test tweets

[ 01 ]

Research Overview

Sentiment classification on short, noisy social-media text is a classic NLP task — casual language, hashtags, mentions, and slang make it harder than formal text.

This study builds an end-to-end classical pipeline: an aggressive text-cleaning stage, a rich TF-IDF representation with unigrams and bigrams, and a fast, interpretable Multinomial Naïve Bayes classifier.

[ 02 ]

Problem Statement

  • Raw tweets are filled with URLs, mentions, hashtags, punctuation, and case differences that add noise rather than signal.
  • Class imbalance and neutral labels can bias a sentiment model toward the majority class.
  • Classical (non-transformer) pipelines need disciplined normalization to stay competitive.

[ 03 ]

Objective

  • Build a balanced binary sentiment classifier from the Sentiment140 corpus.
  • Design a preprocessing chain that reduces Twitter noise while preserving sentiment-bearing words.
  • Evaluate TF-IDF + Multinomial Naïve Bayes with a stratified split and a full classification report.

[ 04 ]

Methodology

  • Balanced sampling: 100,000 positive and 100,000 negative tweets drawn with random_state=42 from the 1.6M-row Sentiment140 corpus, with the neutral class excluded.
  • Preprocessing chain: lowercase → strip URLs, @mentions, #hashtags, and non-alphabetic characters → remove punctuation → tokenize → remove English stopwords → WordNet synonym substitution → Porter stemming → WordNet lemmatization (noun / verb / adjective).
  • Vectorization: TF-IDF with max_features=10,000 and ngram_range=(1,2).
  • Classification: Multinomial Naïve Bayes (alpha=1.0, fit_prior=True) on a stratified 80/20 split (160k train / 40k test).

[ 05 ]

Models Used

Multinomial Naïve Bayes

Probabilistic classifier over TF-IDF counts; fast, lightweight, and interpretable for text.

TF-IDF (unigrams + bigrams)

Term frequency–inverse document frequency vectorization capped at 10,000 features.

[ 06 ]

Dataset

  • Sentiment140 (Kaggle / kazanova): 1.6 million tweets labeled positive (4) and negative (0); neutral (2) excluded.
  • Balanced to 200,000 rows — 100k per class — with a 50/50 class distribution confirmed by a pie-chart check before training.

[ 07 ]

Implementation

  • Pipeline in Python with pandas, NLTK (stopwords, punkt, wordnet), and scikit-learn (TfidfVectorizer, MultinomialNB, train_test_split, metrics).
  • Per-class sentiment distribution visualized, null values dropped, and the balanced 200k rows saved to a CSV.
  • Evaluation outputs a confusion-matrix heatmap, a classification report, and accuracy.
  • Sanity-checked on unseen sentences — 'good' classifies positive and 'I hate this' classifies negative.

[ 08 ]

Key Features

  • Balanced two-class dataset with reproducible sampling (random_state=42).
  • Deep normalization: synonym substitution + Porter stemming + multi-POS lemmatization.
  • TF-IDF with unigrams and bigrams capped at 10,000 features.
  • Stratified evaluation with per-class precision, recall, and F1 reporting.

[ 09 ]

Results

  • The model reaches 74.84% accuracy on a balanced 40k-tweet held-out set, with near-symmetric precision and recall across both classes.
  • Bigrams and synonym substitution help the model handle conversational phrasing, while aggressive cleaning keeps the feature space tractable.

[ 10 ]

Outcome

  • A compact, transparent sentiment classifier that is cheap to train and easy to inspect — a strong baseline for more expensive transformer approaches.
  • Demonstrated that disciplined preprocessing materially improves classical NLP performance on noisy social text.

[ 11 ]

Tools & Technologies

PythonpandasNLTKscikit-learnmatplotlibseaborn

[ 12 ]

Challenges

  • Twitter slang, abbreviations, and emojis resist rule-based cleaning.
  • Sentiment140 is noisy by construction (distant-supervision labels), capping the achievable accuracy.
  • Classical models cannot capture the long-range contextual meaning that transformers handle.

[ 13 ]

Future Improvements

  • Compare against contextual models such as BERT or BanglaBERT-style transformers using this Naïve Bayes result as the baseline.
  • Add a neutral class and multi-class sentiment labels.
  • Tune TF-IDF parameters (ngram range, sublinear TF, min_df) and try SVMs or logistic regression.

[ 14 ]

Related work

Back to research