Skip to content

Latest commit

ย 

History

History
139 lines (100 loc) ยท 4.18 KB

File metadata and controls

139 lines (100 loc) ยท 4.18 KB

๐Ÿง  Sentiment Analysis ML Pipeline

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.


๐Ÿ“ฆ Features

  • 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 pickle for deployment

  • Included helper function to make live predictions

  • Visualized sentiment distribution and training progress


๐Ÿ“Š Dataset Overview

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.


๐Ÿ—๏ธ Pipeline Architecture

[text, time_of_tweet, age_group]
            โ”‚
     โ”Œโ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”
     โ”‚ ColumnTransformer โ”‚
     โ””โ”€โ”€โ”€โ”€โ–ฒโ”€โ”€โ”€โ”€โ”˜
TF-IDF on 'clean_text' + OHE on categorical columns
            โ”‚
     โ”Œโ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”
     โ”‚ ML Model (RF / DNN) โ”‚
     โ””โ”€โ”€โ”€โ”€โ–ฒโ”€โ”€โ”€โ”€โ”˜
    Predict Sentiment

๐Ÿง  Model Performance

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

๐Ÿšช Classification Report (Random Forest)

              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

๐Ÿ”ฎ Predict Sentiment Example

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: negative

๐Ÿš€ Deployment

Models and transformers are saved using pickle:

  • website/RandomForest.pkl
  • website/data_preprocessor.pkl

These can be loaded in a web API (e.g., FastAPI or Flask) for live predictions.


๐Ÿงฐ Tech Stack

  • 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

๐Ÿ“‚ Project Structure

.
โ”œโ”€โ”€ 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

๐Ÿ“Œ Future Work

  • [โœ…] Deploy the model via FastAPI
  • [โœ…] Build a frontend
  • Add support for multilingual sentiment analysis
  • Integrate with live Twitter API

๐Ÿ™Œ Acknowledgments

This project was developed as part of my AI/ML portfolio. Built by Haris Ahmed.