This project is a full machine learning pipeline that predicts the sentiment of social media text (positive, negative, or neutral). It includes classical ML (Random Forest, Logistic Regression), deep learning (TensorFlow DNN), and deployment-ready preprocessing using ColumnTransformer.
-
Cleaned and preprocessed a real-world Twitter dataset
-
Applied TF-IDF vectorization to textual data
-
Encoded categorical features using OneHotEncoder
-
Trained and compared:
- โ Random Forest Classifier
- โ Logistic Regression
- โ TensorFlow Deep Neural Network (DNN)
-
Exported model and pipeline using
picklefor deployment -
Included helper function to make live predictions
-
Visualized sentiment distribution and training progress
| Feature | Description |
|---|---|
text |
Raw tweet text |
sentiment |
Target class: positive / neutral / negative |
age_group |
Age group of the tweet's author |
time_of_tweet |
When the tweet was posted |
Country, population, etc. |
Dropped from final model |
๐ Total rows after cleaning: 3534
๐งผ Cleaning included: removing URLs, punctuation, single characters, and stopwords.
[text, time_of_tweet, age_group]
โ
โโโโโโผโโโโโ
โ ColumnTransformer โ
โโโโโโฒโโโโโ
TF-IDF on 'clean_text' + OHE on categorical columns
โ
โโโโโโผโโโโโ
โ ML Model (RF / DNN) โ
โโโโโโฒโโโโโ
Predict Sentiment
| Model | Accuracy | Notes |
|---|---|---|
| Logistic Regression | 64.21% | Balanced but basic |
| Random Forest | 64.36% | Stronger on positive class |
| TensorFlow DNN | 98.64% train / 59.01% val | Overfitting observed |
precision recall f1-score
negative 0.70 0.44 0.54
neutral 0.57 0.77 0.65
positive 0.76 0.67 0.71
accuracy 0.64
from predict import predict_sentiment
print(predict_sentiment("I love this product! It's amazing."))
# Output: positive
print(predict_sentiment("This is the worst experience I've ever had."))
# Output: negativeModels and transformers are saved using pickle:
website/RandomForest.pklwebsite/data_preprocessor.pkl
These can be loaded in a web API (e.g., FastAPI or Flask) for live predictions.
- Pandas, NumPy โ Data cleaning and manipulation
- NLTK โ Stopword removal and lemmatization
- scikit-learn โ TF-IDF, OneHotEncoder, Random Forest, Logistic Regression
- TensorFlow / Keras โ Deep learning model
- Matplotlib / Seaborn โ Visualization
- Pickle โ Saving models for reuse
.
โโโ data/ # Dataset (test.csv, cleaned CSVs)
โโโ website/
โ โโโ RandomForest.pkl # Final model
โ โโโ data_preprocessor.pkl # ColumnTransformer
โโโ predict.py # predict_sentiment() function
โโโ SentimentAnalysis.ipynb # Full ML pipeline notebook
โโโ README.md # This file
- [โ ] Deploy the model via FastAPI
- [โ ] Build a frontend
- Add support for multilingual sentiment analysis
- Integrate with live Twitter API
This project was developed as part of my AI/ML portfolio. Built by Haris Ahmed.