Author: Neha Narkhede PRN: 202301040146
This project performs binary sentiment analysis on the Sentiment140 dataset, which contains 1.6 million tweets labeled as positive or negative. The goal is to preprocess raw tweet text using NLP techniques and train machine learning classifiers to predict tweet sentiment.
| File | Description |
|---|---|
training.1600000.processed.noemoticon.csv |
Sentiment140 dataset with 1.6M tweets |
Columns used:
target– Sentiment label (0= Negative,4→ remapped to1= Positive)text– Raw tweet content
NLP_Assignment/
├── dataset/
│ └── archive (6)/
│ └── training.1600000.processed.noemoticon.csv
├── Code_nlp.ipynb # Main Jupyter Notebook
├── requirements.txt # Python dependencies
└── README.md
- Loaded the CSV with
latin-1encoding - Retained only
targetandtextcolumns - Dropped rows with missing values (
dropna) - Remapped label
4→1for binary classification
- Sampled 50,000 tweets (random state = 42) before running the NLP pipeline
- This avoids preprocessing all 1.6M rows, significantly reducing runtime
| Step | Description |
|---|---|
| Tokenization | Split text into tokens using NLTK word_tokenize (lowercased) |
| Stopword Removal | Removed English stopwords using NLTK |
| Stemming | Applied Porter Stemmer to reduce words to their root form |
| Lemmatization | Applied WordNet Lemmatizer on non-stemmed tokens (independent step) |
| Clean Text | Combined pipeline: lowercasing → URL removal → mention removal → non-alpha removal → stopword removal → short token filter (len > 2) → stemming + lemmatization |
- 80% train / 20% test (random state = 42)
| Vectorizer | Config |
|---|---|
| CountVectorizer | max_features=5000, ngram_range=(1,2) |
| TF-IDF Vectorizer | max_features=5000, ngram_range=(1,2) |
| Model | Feature Input |
|---|---|
Logistic Regression (max_iter=200) |
TF-IDF |
| Multinomial Naive Bayes | CountVectorizer |
| Model | Accuracy | Precision (avg) | Recall (avg) | F1 (avg) |
|---|---|---|---|---|
| Logistic Regression | 75.17% | 0.75 | 0.75 | 0.75 |
| Naive Bayes | 74.70% | 0.75 | 0.75 | 0.75 |
- Logistic Regression slightly outperforms Naive Bayes (~0.5% higher accuracy).
- Logistic Regression captures feature relationships, while Naive Bayes assumes word independence.
- Both models show balanced precision/recall across classes — no class bias.
- ~75% accuracy is reasonable given the noisy nature of Twitter data (slang, abbreviations, informal language).
- NLP preprocessing (stopword removal, stemming, lemmatization) significantly improved model performance.
- Class Distribution – Bar chart of positive vs. negative tweet counts (with colors)
- Confusion Matrix – Heatmap with true/predicted axis labels for Logistic Regression
- Model Accuracy Comparison – Bar chart with value labels and y-axis limits
- Model Performance Comparison – Line chart of Precision, Recall, F1 for both models
- Top 20 Words – Bar chart of the most frequent words in cleaned tweets (with
tight_layout)
| Area | Change |
|---|---|
| Data loading | Added dropna() to remove rows with missing text or labels |
| Sampling order | Moved sampling to before preprocessing (saves ~30x processing time) |
| NLTK downloads | Consolidated all downloads into a single cell at the start |
| Lemmatization | Fixed standalone lemmatized column to use no_stopwords (not stemmed tokens) |
| Clean text | Added len(w) > 2 filter to remove very short noise tokens |
| Confusion matrix | Added xticklabels, yticklabels, axis titles, and cmap='Blues' |
| Accuracy bar chart | Added y-axis limits (0.5–1.0) and numeric value labels on bars |
| All charts | Added tight_layout() to prevent label cutoff |
Install all dependencies at once:
pip install -r requirements.txtOr manually:
pip install pandas numpy matplotlib seaborn scikit-learn nltkDownload NLTK resources (run once inside the notebook):
import nltk
nltk.download('punkt')
nltk.download('punkt_tab')
nltk.download('stopwords')
nltk.download('wordnet')- Open
Code_nlp.ipynbin Google Colab or Jupyter Notebook. - Upload the dataset CSV when prompted (Colab) or place it in the working directory.
- Run all cells sequentially.