Machine learning analysis of M-Pesa transaction patterns using K-Means clustering, PCA, and Isolation Forest
# M-PESA Transaction Pattern Analysis
## ✨ Shukran. Thank You for taking an interest in this project. Looking forward to building impactful projects with You!
# M-Pesa Transaction Pattern Analysis
**Author:** Clement Ongera Nyangoya
**Date:** April 2026
**Tools:** Python, Pandas, scikit-learn, Matplotlib
## Overview
Reproducible machine learning analysis of mobile money transaction patterns
from M-Pesa, Kenya's dominant mobile payment platform. This project applies
unsupervised learning (K-Means clustering, Isolation Forest anomaly detection)
and time-series analysis to identify spending behaviors, detect anomalies, and
characterize financial patterns.
## Methods
- **Data cleaning and feature engineering:** Temporal features, transaction
categorization via rule-based NLP, behavioral indicators
- **Clustering:** K-Means with PCA visualization to identify distinct transaction
behavior segments
- **Anomaly detection:** Isolation Forest for unusual transaction identification
- **Time-series analysis:** Daily aggregation with moving averages and volatility
metrics
## Key Findings
### 🎯 3 Distinct Customer Segments Identified
Using K-Means clustering on 1,215 transactions:
| Cluster | Size | Behavior | Avg Amount | Peak Time |
|---------|------|----------|------------|-----------|
| **Cluster 0** | 517 (43%) | "Social Senders" - Person-to-person transfers | -65 KES | 2:00 PM |
| **Cluster 1** | 336 (28%) | "Morning Shoppers" - Merchant payments | -63 KES | 9:00 AM |
| **Cluster 2** | 362 (30%) | "Night Owls" - Small evening purchases | -26 KES | 8:00 PM |
**Key insight:** All three segments are net spenders, with "Night Owls" making the smallest but most frequent evening purchases.
### 🔍 Anomaly Detection Results
- **60 anomalies detected** (4.9% of transactions)
- **Typical anomaly amount:** 800+ KES (10-30x normal)
- **Most anomalous:** Person transfers (53%) and merchant payments (47%)
- **Isolation Forest precision:** Estimated 95%
### 📊 Transaction Catego …