Machine-learning analysis for distinguishing Twitter bot accounts from human accounts using account metadata collected through Twitter's Search API with tweepy.
The notebook also separates tweet-level data for exploratory/NLP analysis, while the implemented classification pipeline focuses on twitter account attributes to identify bots.
After removing duplicate records:
- 869 total accounts
- 705 human accounts
- 164 bots
To address class imbalance, all 164 bot accounts were retained and 164 human accounts were sampled, producing a balanced dataset of 328 accounts.
Account features include:
- Favorites count
- Followers count
- Friends count
- Listed count
- Statuses count
- Verified status
- Load Twitter data from JSON.
- Separate account-level and tweet-level attributes.
- Remove duplicate account records.
- Balance inputs across bot and human classes by random sampling
- Split data into training (262) and test (66) observations.
- Standardize numerical features.
- Examine features' structure using PCA.
- Train and evaluate classification models.
PCA did not provide a strong dimensionality-reduction advantage; the largest principal component only explaining about 33.6% of variance.
| Model | Test Accuracy |
|---|---|
| Logistic Regression | 87.88% |
| Random Forest | 92.42% |
For Logistic Regression, cross-validation selected C = 1e-5 from the tested regularization values.
Random Forest achieved the best result at 92.42% test accuracy, demonstrating that bot and human accounts can be effectively separated using account metadata by employing machine-learning techniques.
- Python
- Pandas / NumPy
- scikit-learn
- NLTK
- Matplotlib / Seaborn
- Tweepy / Twitter Search API