Upload folder using huggingface_hub
Browse files- .gitattributes +10 -0
- README.md +200 -242
- images/01_class_distribution.png +3 -0
- images/02_feature_correlation.png +3 -0
- images/03_correlation_matrix.png +3 -0
- images/04_baseline_confusion_matrix.png +0 -0
- images/05_baseline_roc_curve.png +3 -0
- images/06_baseline_precision_recall.png +0 -0
- images/07_baseline_feature_importance.png +3 -0
- images/08_cross_validation.png +3 -0
- images/09_tuned_confusion_matrix.png +0 -0
- images/10_tuned_roc_curve.png +3 -0
- images/11_tuned_precision_recall.png +3 -0
- images/12_tuned_feature_importance.png +3 -0
- images/13_model_comparison.png +3 -0
- twitter_bot_detection_v2.pkl +3 -0
- twitter_features_v2.json +14 -0
- twitter_metrics_v2.txt +39 -0
- twitter_model_comparison.csv +7 -0
- twitter_scaler_v2.pkl +3 -0
.gitattributes
CHANGED
|
@@ -1 +1,11 @@
|
|
| 1 |
*.pkl filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
images/01_class_distribution.png filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
images/02_feature_correlation.png filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
images/03_correlation_matrix.png filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
images/05_baseline_roc_curve.png filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
images/07_baseline_feature_importance.png filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
images/08_cross_validation.png filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
images/10_tuned_roc_curve.png filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
images/11_tuned_precision_recall.png filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
images/12_tuned_feature_importance.png filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
images/13_model_comparison.png filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,315 +1,273 @@
|
|
| 1 |
---
|
| 2 |
-
language: en
|
| 3 |
-
license:
|
|
|
|
| 4 |
tags:
|
| 5 |
-
- bot-detection
|
| 6 |
-
- twitter
|
| 7 |
-
-
|
| 8 |
-
-
|
| 9 |
-
-
|
| 10 |
-
- classification
|
| 11 |
-
metrics:
|
| 12 |
-
- accuracy
|
| 13 |
-
- precision
|
| 14 |
-
- recall
|
| 15 |
-
- f1
|
| 16 |
-
- roc-auc
|
| 17 |
-
library_name: scikit-learn
|
| 18 |
---
|
| 19 |
|
| 20 |
-
#
|
| 21 |
|
| 22 |
-
##
|
| 23 |
|
| 24 |
-
This Random Forest classifier
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
- **Framework**: scikit-learn
|
| 30 |
-
- **Task**: Binary Classification (Bot vs Human)
|
| 31 |
-
- **Language**: Python
|
| 32 |
-
- **License**: MIT
|
| 33 |
|
| 34 |
-
## Performance
|
| 35 |
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
2. **IsVerified** - Whether the account has a verification badge (blue checkmark)
|
| 50 |
-
3. **HasProfilePic** - Whether the account has a profile picture
|
| 51 |
-
4. **FollowingCount** - Number of accounts being followed
|
| 52 |
-
5. **FollowerCount** - Number of followers
|
| 53 |
-
6. **HasLocation** - Whether location information is provided
|
| 54 |
-
7. **HasDescription** - Whether the account has a bio/description
|
| 55 |
-
8. **TweetsCount** - Total number of tweets posted
|
| 56 |
-
9. **FollowToFollowerRatio** - Ratio of following to followers
|
| 57 |
-
10. **AccountAge** - Age of the account (if available)
|
| 58 |
-
11. **HasUrl** - Whether there's a URL in the profile
|
| 59 |
-
12. **DefaultProfileImage** - Whether using default profile image
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
| 66 |
-
- Content moderation and platform integrity
|
| 67 |
-
- Research on social media bot behavior and misinformation campaigns
|
| 68 |
-
- Automated account screening for spam detection
|
| 69 |
-
- Election integrity and political bot detection
|
| 70 |
|
| 71 |
-
###
|
| 72 |
|
| 73 |
-
-
|
| 74 |
-
-
|
| 75 |
-
-
|
| 76 |
-
-
|
| 77 |
-
-
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
|
| 81 |
-
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
```
|
| 86 |
|
| 87 |
-
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
import pandas as pd
|
| 92 |
-
import numpy as np
|
| 93 |
-
from sklearn.preprocessing import MinMaxScaler
|
| 94 |
-
|
| 95 |
-
# Load the model
|
| 96 |
-
model = joblib.load('Twitter_BOT_Detection_Model_v1.pkl')
|
| 97 |
-
|
| 98 |
-
# Prepare your data
|
| 99 |
-
features = ['IsPrivate', 'IsVerified', 'HasProfilePic', 'FollowingCount',
|
| 100 |
-
'FollowerCount', 'HasLocation', 'HasDescription', 'TweetsCount',
|
| 101 |
-
'FollowToFollowerRatio', 'AccountAge', 'HasUrl', 'DefaultProfileImage']
|
| 102 |
-
|
| 103 |
-
# Example account data
|
| 104 |
-
account_data = {
|
| 105 |
-
'IsPrivate': 0,
|
| 106 |
-
'IsVerified': 0,
|
| 107 |
-
'HasProfilePic': 1,
|
| 108 |
-
'FollowingCount': 5000,
|
| 109 |
-
'FollowerCount': 50,
|
| 110 |
-
'HasLocation': 0,
|
| 111 |
-
'HasDescription': 0,
|
| 112 |
-
'TweetsCount': 10000,
|
| 113 |
-
'FollowToFollowerRatio': 100.0,
|
| 114 |
-
'AccountAge': 30, # days
|
| 115 |
-
'HasUrl': 1,
|
| 116 |
-
'DefaultProfileImage': 0
|
| 117 |
-
}
|
| 118 |
|
| 119 |
-
|
| 120 |
-
df = pd.DataFrame([account_data])
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
|
| 128 |
-
prediction = model.predict(df_scaled)
|
| 129 |
-
probability = model.predict_proba(df_scaled)
|
| 130 |
|
| 131 |
-
|
| 132 |
-
print(f"Confidence - Human: {probability[0][0]:.2%}, Bot: {probability[0][1]:.2%}")
|
| 133 |
-
```
|
| 134 |
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
# For multiple accounts
|
| 139 |
-
accounts_df = pd.read_csv('twitter_accounts_to_check.csv')
|
| 140 |
-
accounts_scaled = scaler.transform(accounts_df[features])
|
| 141 |
|
| 142 |
-
|
| 143 |
-
probabilities = model.predict_proba(accounts_scaled)
|
| 144 |
|
| 145 |
-
|
| 146 |
-
accounts_df['is_bot'] = predictions
|
| 147 |
-
accounts_df['bot_probability'] = probabilities[:, 1]
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
| 154 |
|
| 155 |
-
###
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
'FollowingCount', 'FollowerCount', 'HasLocation',
|
| 164 |
-
'HasDescription', 'TweetsCount', 'FollowToFollowerRatio',
|
| 165 |
-
'AccountAge', 'HasUrl', 'DefaultProfileImage']
|
| 166 |
-
|
| 167 |
-
def predict(self, account_features):
|
| 168 |
-
"""Predict if an account is a bot"""
|
| 169 |
-
df = pd.DataFrame([account_features])
|
| 170 |
-
df_scaled = self.scaler.fit_transform(df[self.features])
|
| 171 |
-
prediction = self.model.predict(df_scaled)[0]
|
| 172 |
-
probability = self.model.predict_proba(df_scaled)[0]
|
| 173 |
-
|
| 174 |
-
return {
|
| 175 |
-
'is_bot': bool(prediction),
|
| 176 |
-
'bot_probability': float(probability[1]),
|
| 177 |
-
'human_probability': float(probability[0])
|
| 178 |
-
}
|
| 179 |
-
|
| 180 |
-
# Usage
|
| 181 |
-
detector = TwitterBotDetector('Twitter_BOT_Detection_Model_v1.pkl')
|
| 182 |
-
result = detector.predict(account_data)
|
| 183 |
-
print(result)
|
| 184 |
-
```
|
| 185 |
|
| 186 |
-
|
| 187 |
|
| 188 |
-
|
| 189 |
|
| 190 |
-
|
| 191 |
-
- Various bot types (spam bots, political bots, engagement bots, etc.)
|
| 192 |
-
- Diverse account types, ages, and activity levels
|
| 193 |
-
- Features extracted from public profile information
|
| 194 |
|
| 195 |
-
|
| 196 |
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
-
|
| 200 |
|
| 201 |
-
|
| 202 |
-
2. Calculation of derived features (FollowToFollowerRatio, AccountAge)
|
| 203 |
-
3. Handling of missing values and outliers
|
| 204 |
-
4. MinMax normalization of all features to [0, 1] range
|
| 205 |
-
5. Train-test split with stratification to maintain class balance
|
| 206 |
|
| 207 |
-
|
| 208 |
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
-
|
| 216 |
|
| 217 |
-
##
|
| 218 |
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
- Requires access to Twitter API for feature extraction
|
| 224 |
-
- Performance may degrade over time as bot behaviors evolve rapidly
|
| 225 |
-
- Limited to profile-level features; does not analyze tweet content deeply
|
| 226 |
-
- May struggle with sophisticated bots that mimic human behavior closely
|
| 227 |
-
- Requires regular updates due to platform changes (Twitter → X)
|
| 228 |
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
-
|
| 232 |
-
- Could have temporal biases based on when training data was collected
|
| 233 |
-
- May misclassify legitimate accounts with unusual behavior patterns
|
| 234 |
-
- Potential bias against new accounts or accounts with low activity
|
| 235 |
-
- Could reflect biases in the original labeling process
|
| 236 |
-
- May have difficulty with non-English accounts if training data is primarily English
|
| 237 |
|
| 238 |
-
|
| 239 |
|
| 240 |
-
|
| 241 |
-
- Use as part of a multi-layered detection system including content analysis
|
| 242 |
-
- Implement human review for high-stakes decisions
|
| 243 |
-
- Monitor for false positives and adjust classification thresholds based on use case
|
| 244 |
-
- Combine with tweet content analysis, network analysis, and temporal patterns
|
| 245 |
-
- Consider context (political events, trending topics) when interpreting results
|
| 246 |
-
- Validate performance across different account types and languages
|
| 247 |
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
-
-
|
| 251 |
-
-
|
| 252 |
-
-
|
| 253 |
-
-
|
| 254 |
-
- Provide transparency to users about automated detection systems
|
| 255 |
-
- Allow for appeals and manual review processes
|
| 256 |
-
- Be aware of potential for false accusations
|
| 257 |
-
- Consider impact on freedom of speech and legitimate automated accounts (news bots, etc.)
|
| 258 |
-
- Monitor for discriminatory outcomes across different user groups
|
| 259 |
|
| 260 |
-
|
| 261 |
|
| 262 |
-
|
| 263 |
-
- Platform rebranding (Twitter → X) may introduce new bot patterns
|
| 264 |
-
- Changes in verification system may affect IsVerified feature utility
|
| 265 |
|
| 266 |
-
|
| 267 |
|
| 268 |
-
|
|
|
|
|
|
|
| 269 |
|
| 270 |
-
|
| 271 |
|
| 272 |
-
|
|
|
|
|
|
|
| 273 |
|
| 274 |
-
|
| 275 |
-
@misc{twitter_bot_detection_2024,
|
| 276 |
-
title={Twitter Bot Detection Model v2},
|
| 277 |
-
author={Your Name/Organization},
|
| 278 |
-
year={2024},
|
| 279 |
-
publisher={Hugging Face},
|
| 280 |
-
howpublished={\url{https://huggingface.co/your-username/twitter-bot-detection}}
|
| 281 |
-
}
|
| 282 |
-
```
|
| 283 |
|
| 284 |
-
##
|
| 285 |
|
| 286 |
-
- [
|
| 287 |
-
-
|
|
|
|
|
|
|
| 288 |
|
| 289 |
-
|
| 290 |
|
| 291 |
-
|
| 292 |
|
| 293 |
-
|
| 294 |
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
|
| 299 |
-
|
| 300 |
|
| 301 |
-
|
| 302 |
-
- **v1.0**: Initial release
|
| 303 |
|
| 304 |
-
|
| 305 |
|
| 306 |
-
|
| 307 |
|
| 308 |
-
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
- Integration of tweet content analysis (NLP features)
|
| 312 |
-
- Network graph analysis for coordinated bot detection
|
| 313 |
-
- Temporal pattern analysis
|
| 314 |
-
- Support for multilingual accounts
|
| 315 |
-
- Real-time feature extraction pipeline
|
|
|
|
| 1 |
---
|
| 2 |
+
language: "en"
|
| 3 |
+
license: "apache-2.0"
|
| 4 |
+
library_name: "scikit-learn"
|
| 5 |
tags:
|
| 6 |
+
- "bot-detection"
|
| 7 |
+
- "twitter"
|
| 8 |
+
- "classification"
|
| 9 |
+
- "scikit-learn"
|
| 10 |
+
- "random-forest"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# TWITTER Bot Detection Model
|
| 14 |
|
| 15 |
+
## Overview
|
| 16 |
|
| 17 |
+
This directory contains a trained Random Forest classifier for detecting bot accounts on Twitter.
|
| 18 |
|
| 19 |
+
**Model Version:** v2
|
| 20 |
+
**Training Date:** 2025-11-27 12:08:54
|
| 21 |
+
**Framework:** scikit-learn 1.5.2
|
| 22 |
+
**Algorithm:** Random Forest Classifier with GridSearchCV Hyperparameter Tuning
|
| 23 |
|
| 24 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
## 📊 Model Performance
|
| 27 |
|
| 28 |
+
### Final Metrics (Test Set)
|
| 29 |
|
| 30 |
+
| Metric | Score |
|
| 31 |
+
| --------------------- | --------------- |
|
| 32 |
+
| **Accuracy** | 0.8771 (87.71%) |
|
| 33 |
+
| **Precision** | 0.8595 (85.95%) |
|
| 34 |
+
| **Recall** | 0.7558 (75.58%) |
|
| 35 |
+
| **F1-Score** | 0.8043 (80.43%) |
|
| 36 |
+
| **ROC-AUC** | 0.9354 (93.54%) |
|
| 37 |
+
| **Average Precision** | 0.9008 (90.08%) |
|
| 38 |
|
| 39 |
+
### Model Improvement
|
| 40 |
|
| 41 |
+
- **Baseline ROC-AUC:** 0.9314
|
| 42 |
+
- **Tuned ROC-AUC:** 0.9354
|
| 43 |
+
- **Improvement:** 0.0040 (0.43%)
|
| 44 |
|
| 45 |
+
---
|
| 46 |
|
| 47 |
+
## 🗂️ Files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
| File | Description |
|
| 50 |
+
| ------------------------------ | -------------------------------------- |
|
| 51 |
+
| `twitter_bot_detection_v2.pkl` | Trained Random Forest model |
|
| 52 |
+
| `twitter_scaler_v2.pkl` | MinMaxScaler for feature normalization |
|
| 53 |
+
| `twitter_features_v2.json` | List of features used by the model |
|
| 54 |
+
| `twitter_metrics_v2.txt` | Detailed performance metrics report |
|
| 55 |
+
| `images/` | All visualization plots (13 images) |
|
| 56 |
+
| `README.md` | This file |
|
| 57 |
|
| 58 |
+
---
|
| 59 |
|
| 60 |
+
## 🎯 Dataset Information
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
### Training Configuration
|
| 63 |
|
| 64 |
+
- **Training Samples:** 29,951
|
| 65 |
+
- **Test Samples:** 7,487
|
| 66 |
+
- **Total Samples:** 37,438
|
| 67 |
+
- **Number of Features:** 12
|
| 68 |
+
- **Cross-Validation Folds:** 5
|
| 69 |
+
- **Random State:** 42
|
| 70 |
|
| 71 |
+
### Class Distribution
|
| 72 |
|
| 73 |
+
**Training Set:**
|
| 74 |
|
| 75 |
+
- Human (0): 20,028 (66.87%)
|
| 76 |
+
- Bot (1): 9,923 (33.13%)
|
|
|
|
| 77 |
|
| 78 |
+
**Test Set:**
|
| 79 |
|
| 80 |
+
- Human (0): 4,985 (66.58%)
|
| 81 |
+
- Bot (1): 2,502 (33.42%)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
---
|
|
|
|
| 84 |
|
| 85 |
+
## 🔧 Features (12)
|
| 86 |
+
|
| 87 |
+
1. `has_custom_cover_image`
|
| 88 |
+
2. `description_length`
|
| 89 |
+
3. `favourites_count`
|
| 90 |
+
4. `followers_count`
|
| 91 |
+
5. `friends_count`
|
| 92 |
+
6. `followers_to_friends_ratio`
|
| 93 |
+
7. `has_location`
|
| 94 |
+
8. `username_digit_count`
|
| 95 |
+
9. `username_length`
|
| 96 |
+
10. `statuses_count`
|
| 97 |
+
11. `is_verified`
|
| 98 |
+
12. `account_age_days`
|
| 99 |
|
| 100 |
+
---
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
## 🏆 Top 5 Most Important Features
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
4. **followers_count** - 0.1895
|
| 105 |
+
5. **favourites_count** - 0.1813
|
| 106 |
+
6. **friends_count** - 0.1494
|
| 107 |
+
7. **statuses_count** - 0.1244
|
| 108 |
+
8. **account_age_days** - 0.1010
|
| 109 |
|
| 110 |
+
---
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
## ⚙️ Hyperparameters
|
|
|
|
| 113 |
|
| 114 |
+
### Best Parameters (from GridSearchCV)
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
- **class_weight:** balanced
|
| 117 |
+
- **max_depth:** 20
|
| 118 |
+
- **max_features:** sqrt
|
| 119 |
+
- **min_samples_leaf:** 1
|
| 120 |
+
- **min_samples_split:** 2
|
| 121 |
+
- **n_estimators:** 300
|
| 122 |
|
| 123 |
+
### Parameter Search Space
|
| 124 |
|
| 125 |
+
- **n_estimators:** [100, 200, 300]
|
| 126 |
+
- **max_depth:** [10, 15, 20, None]
|
| 127 |
+
- **min_samples_split:** [2, 5, 10]
|
| 128 |
+
- **min_samples_leaf:** [1, 2, 4]
|
| 129 |
+
- **max_features:** ['sqrt', 'log2']
|
| 130 |
+
- **bootstrap:** [True, False]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
+
**Total combinations tested:** 540
|
| 133 |
|
| 134 |
+
---
|
| 135 |
|
| 136 |
+
## 📈 Cross-Validation Results
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
+
### Mean Scores (5-Fold Stratified CV)
|
| 139 |
|
| 140 |
+
- **Accuracy:** 0.8750 (±0.0053)
|
| 141 |
+
- **Precision:** 0.8658 (±0.0089)
|
| 142 |
+
- **Recall:** 0.7368 (±0.0113)
|
| 143 |
+
- **F1-Score:** 0.7961 (±0.0092)
|
| 144 |
+
- **ROC-AUC:** 0.9325 (±0.0037)
|
| 145 |
|
| 146 |
+
---
|
| 147 |
|
| 148 |
+
## 🖼️ Visualizations
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
+
All visualizations are saved in the `images/` directory:
|
| 151 |
|
| 152 |
+
1. **01_class_distribution.png** - Training/Test set class distribution
|
| 153 |
+
2. **02_feature_correlation.png** - Feature correlation with target variable
|
| 154 |
+
3. **03_correlation_matrix.png** - Feature correlation heatmap
|
| 155 |
+
4. **04_baseline_confusion_matrix.png** - Baseline model confusion matrix
|
| 156 |
+
5. **05_baseline_roc_curve.png** - Baseline ROC curve
|
| 157 |
+
6. **06_baseline_precision_recall.png** - Baseline Precision-Recall curve
|
| 158 |
+
7. **07_baseline_feature_importance.png** - Baseline feature importance
|
| 159 |
+
8. **08_cross_validation.png** - Cross-validation score distribution
|
| 160 |
+
9. **09_tuned_confusion_matrix.png** - Tuned model confusion matrix
|
| 161 |
+
10. **10_tuned_roc_curve.png** - Tuned ROC curve
|
| 162 |
+
11. **11_tuned_precision_recall.png** - Tuned Precision-Recall curve
|
| 163 |
+
12. **12_tuned_feature_importance.png** - Tuned feature importance
|
| 164 |
+
13. **13_model_comparison.png** - Baseline vs Tuned comparison
|
| 165 |
|
| 166 |
+
---
|
| 167 |
|
| 168 |
+
## 🚀 Usage Example
|
| 169 |
|
| 170 |
+
```python
|
| 171 |
+
import joblib
|
| 172 |
+
import pandas as pd
|
| 173 |
+
import numpy as np
|
| 174 |
+
|
| 175 |
+
# Load model and scaler
|
| 176 |
+
model = joblib.load('twitter_bot_detection_v2.pkl')
|
| 177 |
+
scaler = joblib.load('twitter_scaler_v2.pkl')
|
| 178 |
+
|
| 179 |
+
# Prepare your data (example)
|
| 180 |
+
data = {
|
| 181 |
+
'has_custom_cover_image': 0.5,
|
| 182 |
+
'description_length': 0.5,
|
| 183 |
+
'favourites_count': 0.5,
|
| 184 |
+
'followers_count': 0.5,
|
| 185 |
+
'friends_count': 0.5,
|
| 186 |
+
'followers_to_friends_ratio': 0.5,
|
| 187 |
+
'has_location': 0.5,
|
| 188 |
+
'username_digit_count': 0.5,
|
| 189 |
+
'username_length': 0.5,
|
| 190 |
+
'statuses_count': 0.5,
|
| 191 |
+
'is_verified': 0.5,
|
| 192 |
+
'account_age_days': 0.5,
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
# Create DataFrame
|
| 196 |
+
df = pd.DataFrame([data])
|
| 197 |
|
| 198 |
+
# Scale features
|
| 199 |
+
df_scaled = scaler.transform(df)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
+
# Predict
|
| 202 |
+
prediction = model.predict(df_scaled)[0]
|
| 203 |
+
probability = model.predict_proba(df_scaled)[0]
|
| 204 |
+
|
| 205 |
+
print(f"Prediction: {'Bot' if prediction == 1 else 'Human'}")
|
| 206 |
+
print(f"Bot Probability: {probability[1]:.4f}")
|
| 207 |
+
print(f"Human Probability: {probability[0]:.4f}")
|
| 208 |
+
```
|
| 209 |
|
| 210 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
+
## 📋 Confusion Matrix Breakdown
|
| 213 |
|
| 214 |
+
### Tuned Model (Test Set)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
+
```
|
| 217 |
+
Predicted
|
| 218 |
+
Human Bot
|
| 219 |
+
Actual Human 4676 309
|
| 220 |
+
Bot 611 1891
|
| 221 |
+
```
|
| 222 |
|
| 223 |
+
- **True Negatives (TN):** 4,676 (Correctly identified humans)
|
| 224 |
+
- **False Positives (FP):** 309 (Humans incorrectly classified as bots)
|
| 225 |
+
- **False Negatives (FN):** 611 (Bots incorrectly classified as humans)
|
| 226 |
+
- **True Positives (TP):** 1,891 (Correctly identified bots)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
+
---
|
| 229 |
|
| 230 |
+
## 🔍 Model Interpretation
|
|
|
|
|
|
|
| 231 |
|
| 232 |
+
### Strengths
|
| 233 |
|
| 234 |
+
- High ROC-AUC score (0.9354) indicates excellent discrimination capability
|
| 235 |
+
- Balanced precision and recall for both classes
|
| 236 |
+
- Robust cross-validation performance
|
| 237 |
|
| 238 |
+
### Key Insights
|
| 239 |
|
| 240 |
+
1. Top features drive bot classification effectively
|
| 241 |
+
2. GridSearchCV improved performance over baseline by 0.43%
|
| 242 |
+
3. Model generalizes well on unseen test data
|
| 243 |
|
| 244 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
+
## 📝 Notes
|
| 247 |
|
| 248 |
+
- **Feature Scaling:** All features are scaled using MinMaxScaler to [0, 1] range
|
| 249 |
+
- **Missing Values:** Filled with 0 during preprocessing
|
| 250 |
+
- **Class Balance:** Imbalanced dataset
|
| 251 |
+
- **Model Type:** Ensemble method resistant to overfitting
|
| 252 |
|
| 253 |
+
---
|
| 254 |
|
| 255 |
+
## 🔄 Model Updates
|
| 256 |
|
| 257 |
+
To retrain the model:
|
| 258 |
|
| 259 |
+
1. Place new training data in `../data/train_twitter.csv`
|
| 260 |
+
2. Run the training notebook: `5_enhanced_training.ipynb`
|
| 261 |
+
3. Update this README with new metrics
|
| 262 |
|
| 263 |
+
---
|
| 264 |
|
| 265 |
+
## 📧 Contact & Support
|
|
|
|
| 266 |
|
| 267 |
+
For questions or issues regarding this model, please refer to the main project documentation.
|
| 268 |
|
| 269 |
+
---
|
| 270 |
|
| 271 |
+
**Generated:** 2025-11-27 12:08:54
|
| 272 |
+
**Notebook:** `5_enhanced_training.ipynb`
|
| 273 |
+
**Platform:** Twitter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
images/01_class_distribution.png
ADDED
|
Git LFS Details
|
images/02_feature_correlation.png
ADDED
|
Git LFS Details
|
images/03_correlation_matrix.png
ADDED
|
Git LFS Details
|
images/04_baseline_confusion_matrix.png
ADDED
|
images/05_baseline_roc_curve.png
ADDED
|
Git LFS Details
|
images/06_baseline_precision_recall.png
ADDED
|
images/07_baseline_feature_importance.png
ADDED
|
Git LFS Details
|
images/08_cross_validation.png
ADDED
|
Git LFS Details
|
images/09_tuned_confusion_matrix.png
ADDED
|
images/10_tuned_roc_curve.png
ADDED
|
Git LFS Details
|
images/11_tuned_precision_recall.png
ADDED
|
Git LFS Details
|
images/12_tuned_feature_importance.png
ADDED
|
Git LFS Details
|
images/13_model_comparison.png
ADDED
|
Git LFS Details
|
twitter_bot_detection_v2.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8a77a8bb9af5ce0909ae5dd72f18176d12795e3f2ccc658fb4e519d325db19b2
|
| 3 |
+
size 144062585
|
twitter_features_v2.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
"has_custom_cover_image",
|
| 3 |
+
"description_length",
|
| 4 |
+
"favourites_count",
|
| 5 |
+
"followers_count",
|
| 6 |
+
"friends_count",
|
| 7 |
+
"followers_to_friends_ratio",
|
| 8 |
+
"has_location",
|
| 9 |
+
"username_digit_count",
|
| 10 |
+
"username_length",
|
| 11 |
+
"statuses_count",
|
| 12 |
+
"is_verified",
|
| 13 |
+
"account_age_days"
|
| 14 |
+
]
|
twitter_metrics_v2.txt
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
======================================================================
|
| 2 |
+
TWITTER Bot Detection Model - Performance Report
|
| 3 |
+
======================================================================
|
| 4 |
+
|
| 5 |
+
Date: 2025-11-27 12:08:54.462391
|
| 6 |
+
|
| 7 |
+
Training Configuration:
|
| 8 |
+
- Platform: twitter
|
| 9 |
+
- Train samples: 29951
|
| 10 |
+
- Test samples: 7487
|
| 11 |
+
- Features: 12
|
| 12 |
+
- CV Folds: 5
|
| 13 |
+
- Random State: 42
|
| 14 |
+
|
| 15 |
+
Best Hyperparameters:
|
| 16 |
+
- class_weight: balanced
|
| 17 |
+
- max_depth: 20
|
| 18 |
+
- max_features: sqrt
|
| 19 |
+
- min_samples_leaf: 1
|
| 20 |
+
- min_samples_split: 2
|
| 21 |
+
- n_estimators: 300
|
| 22 |
+
|
| 23 |
+
Performance Metrics (Test Set):
|
| 24 |
+
- Accuracy: 0.8771
|
| 25 |
+
- Precision: 0.8595
|
| 26 |
+
- Recall: 0.7558
|
| 27 |
+
- F1: 0.8043
|
| 28 |
+
- Roc_auc: 0.9354
|
| 29 |
+
- Avg_precision: 0.9008
|
| 30 |
+
|
| 31 |
+
Cross-Validation Results:
|
| 32 |
+
- Mean ROC-AUC: 0.9352
|
| 33 |
+
|
| 34 |
+
Feature Importance (Top 5):
|
| 35 |
+
- followers_count: 0.1895
|
| 36 |
+
- favourites_count: 0.1813
|
| 37 |
+
- friends_count: 0.1494
|
| 38 |
+
- statuses_count: 0.1244
|
| 39 |
+
- account_age_days: 0.1010
|
twitter_model_comparison.csv
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Metric,Baseline,Tuned,Improvement,Improvement %
|
| 2 |
+
Accuracy,0.8740483504741552,0.8771203419260051,0.003071991451849887,0.3514669926650382
|
| 3 |
+
Precision,0.8678621991505427,0.8595454545454545,-0.008316744605088244,-0.9583024370952686
|
| 4 |
+
Recall,0.7350119904076738,0.7557953637090328,0.02078337330135893,2.827623708537251
|
| 5 |
+
F1-Score,0.7959316165332179,0.8043385793279455,0.008406962794727635,1.0562418454169766
|
| 6 |
+
ROC-AUC,0.9314009975570197,0.9353899828983353,0.003988985341315643,0.4282779760574006
|
| 7 |
+
Avg Precision,0.8949209792592716,0.9007641701676647,0.005843190908393137,0.6529281404520834
|
twitter_scaler_v2.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c8725c0a395abc30b368dfe7a64051f0095fc7acfb4cfb1ac4229ffe73f02a32
|
| 3 |
+
size 1623
|