Machine Learning project 17
Machine Learning Twitter Sentiment Analysis Project
Build and evaluate a complete twitter sentiment analysis workflow with reproducible Python code, explicit metrics, and honest limitations.
Explore Machine Learning training in VizagView all project ideas
Dataset and objective
A small embedded set of positive and negative tweet-like posts; the program does not connect to Twitter/X or collect user data.
Skills practised
- Social-text cleaning
- Privacy-aware examples
- TF-IDF pipelines
- Language limitations
Requirements
- Python 3.10 or later
- A terminal or command prompt
python -m pip install scikit-learn- About 45-60 minutes to build and review
Machine Learning workflow
- Data: A small embedded set of positive and negative tweet-like posts; the program does not connect to Twitter/X or collect user data.
- Preprocessing: A cleaning function replaces URLs and @mentions with neutral tokens, then TF-IDF creates unigram and bigram features.
- Model: LogisticRegression predicts one of two teaching labels.
- Evaluation: A stratified holdout classification report with precision, recall, F1, and accuracy.
Complete Python code
Save the code as ml_twitter_sentiment_analysis.py. The random state and data handling are included so the result can be reproduced and reviewed.
"""Analyse tweet-like text sentiment without connecting to the X/Twitter API."""
from __future__ import annotations
import re
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
POSTS = [
("Loved today's workshop, the examples finally made sense!", "positive"),
("Great release—the new search is quick and accurate", "positive"),
("Thanks for the helpful reply and clear solution", "positive"),
("This project was fun to build and easy to understand", "positive"),
("Really impressed by the improved performance", "positive"),
("The documentation update is excellent", "positive"),
("Happy with the friendly support today", "positive"),
("The lesson helped me finish my first app", "positive"),
("The service is broken again and I lost my work", "negative"),
("Very disappointed by the slow response", "negative"),
("This update is confusing and full of bugs", "negative"),
("Support ignored the issue for another week", "negative"),
("The instructions do not work at all", "negative"),
("I regret installing this unstable version", "negative"),
("The new layout makes everything harder", "negative"),
("Terrible experience, the page keeps crashing", "negative"),
]
def clean_text(text: str) -> str:
text = re.sub(r"https?://\S+", " URL ", text)
text = re.sub(r"@[A-Za-z0-9_]+", " USER ", text)
return re.sub(r"\s+", " ", text).strip()
def train_model(random_state: int = 42):
texts, labels = zip(*[(clean_text(text), label) for text, label in POSTS])
x_train, x_test, y_train, y_test = train_test_split(
texts, labels, test_size=0.375, random_state=random_state, stratify=labels
)
model = make_pipeline(
TfidfVectorizer(ngram_range=(1, 2), lowercase=True),
LogisticRegression(max_iter=1000, random_state=random_state),
)
model.fit(x_train, y_train)
predictions = model.predict(x_test)
return model, y_test, predictions
def main() -> None:
model, actual, predictions = train_model()
print("Tweet-like Sentiment Analysis")
print(classification_report(actual, predictions, zero_division=0))
text = input("Post to classify (or press Enter to stop): ").strip()
if text:
print("Prediction:", model.predict([clean_text(text)])[0])
print("No platform data is collected. Tiny binary labels cannot capture sarcasm, dialect, context, or mixed sentiment.")
if __name__ == "__main__":
main()
How the pipeline works
A cleaning function replaces URLs and @mentions with neutral tokens, then TF-IDF creates unigram and bigram features.
LogisticRegression predicts one of two teaching labels. A stratified holdout classification report with precision, recall, F1, and accuracy.
Run the project
- Create and activate a virtual environment.
- Install the dependencies with
python -m pip install scikit-learn. - Run
python ml_twitter_sentiment_analysis.py. - Review every printed metric together with the limitation below; a single score never proves deployment readiness.
How to interpret the evaluation
Classification projects report class-aware metrics so majority classes do not hide weak performance. Regression projects report errors in target units and include R-squared or a simple baseline where appropriate. Always verify the split strategy matches how new data will arrive.
Accuracy, ethics, and safety limits
Tiny binary labels cannot capture sarcasm, dialect, harassment context, mixed opinions, or evolving language. Do not profile people or moderate accounts with this model.
Ways to extend the project
Use consented or properly licensed data, add neutral/mixed labels, perform subgroup and dialect error review, test drift, and preserve platform terms and user privacy.
Continue learning Machine Learning
Try the next project, return to the Softenant project library, or explore the Machine Learning course in Vizag for guided data preparation, model evaluation, and portfolio feedback.