Machine Learning Mini Project
Shambhavi Mishra (2301193)
CS32
Topic : Sorting/Filtering Of Tweets
From Twitter
1. Problem Statement
Develop a ML model for Twitter (X) to classify tweets as
positive, negative, or neutral, and apply a heuristic
function to sort tweets based on their length. The goal
is to extract public opinion using interpretable machine
learning models and rank tweets for further analysis.
2. Dataset
Source: Custom tweet dataset from kaggle
Format: CSV with tweet content and metadata
Labeling: Based on keyword matching:
o Positive keywords → label 1
o Negative keywords → label 0
o Others → label -1 (neutral)
3. Preprocessing Pipeline
Cleaning: Removed URLs, mentions, hashtags,
punctuation, numbers, and repeated characters
Tokenization: Split tweets into words
Vectorization: Bag-of-words using custom
vocabulary (max 3000 terms)
Train/Validation Split: 80/20 ratio on non-neutral
tweets
4. Models Implemented
Model Description
Custom implementation
Logistic Regression
using gradient descent
KNN Distance-based voting
Scratch CART with Gini
Decision Tree
impurity
Probabilistic model with
Naive Bayes
Laplace smoothing
Logistic Regression
A linear classifier that models the probability of a class
using the sigmoid function. It updates weights using
gradient descent to minimize cross-entropy loss.
Formula:
K-Nearest Neighbours (KNN)
A non-parametric method that classifies based on the
majority label among the k closest data points. It uses
Euclidean distance to measure similarity.
Formula:
Decision Tree
A tree-based model that recursively splits data to
maximize class purity using Gini impurity. It selects the
best feature and threshold at each node.
Formula (Gini impurity):
Naive Bayes
A probabilistic classifier assuming feature
independence given the class. It uses Bayes’ theorem
with Laplace smoothing to avoid zero probabilities.
Formula:
5. Model Performance
Model Accuracy Precision Recall F1 Score
Logistic Regression 0.9466 0.9466 1.0000 0.9725
KNN 0.9466 0.9466 1.0000 0.9725
Decision Tree 0.9847 0.9919 0.9919 0.9919
Naive Bayes 0.9504 0.9502 1.0000 0.9745
Best Model: Decision Tree
6. Confusion Matrix (Decision Tree)
X Predicted Negative Predicted Positive
True Negative 12 2
True Positive 2 246
7. Decision Tree Visualization
Root split: bad <= 0.5
Key features: sad, hate, love, off, key, thumbs, etc.
Color-coded nodes: Blue = Positive, Orange =
Negative
Depth: 5
Gini impurity: Low across most splits
8. CSV Outputs
File Name Description
Performance metrics for all
model_results.csv
models
positive_tweets.csv Tweets classified as positive
negative_tweets.csv Tweets classified as negative
neutral_tweets.csv Tweets classified as neutral
positive_sorted_tweets_on_leng Positive tweets sorted by
th_basis.csv heuristic length
negative_sorted_tweets_on_len Negative tweets sorted by
gth_basis.csv heuristic length
9. Heuristic Sorting Function Purpose:
Prioritize tweets with meaningful length — penalize
overly short or long tweets.
10. Visualizations
Grouped Bar Chart: Model comparison across
metrics
Confusion Matrix: Decision Tree performance
Decision Tree Plot: Feature-based splits and
predictions
Decision Tree Structure:
Other Algo Visualization:
11. Interpretation
For the taken dataset(small dataset),decision tress
performed better
All models performed well, but Decision Tree was
the most accurate and interpretable.
Sentiment classification enabled effective filtering
of tweets.
Heuristic sorting added value by ranking tweets for
further analysis.
The pipeline is modular, reproducible, and scalable
for real-world sentiment tasks.
The confusion matrix showed very few
misclassifications, confirming strong reliability.
The tree split on intuitive features like bad, sad,
love, making it highly interpretable.
Heuristic sorting ranked tweets by meaningful
length, helping prioritize rich, informative content.