Cost-sensitive machine learning for predictive maintenance of Tanzania's rural water pumps: XGBoost vs MLP under a social cost objective (DrivenData Pump it Up)
# Tanzania Water Pump Predictive Maintenance
Cost-sensitive machine learning for predicting the operational status of rural water pumps in Tanzania, built on the DrivenData "Pump it Up" dataset. The project compares XGBoost against a Multi-Layer Perceptron (MLP) under a custom social cost objective, and selects the model that minimizes the real-world cost of mistakes instead of maximizing accuracy.
This is the code behind my DT2 technical report: `docs/technical_report.pdf`. A full line-by-line review of this codebase is in `docs/CODE_REVIEW.md`.
## Problem
Tanzania operates over 59,000 rural water extraction points. Given the metadata of a pump (construction year, water source, extraction type, payment structure, GPS location, managing authority), predict its status:
| Class | Label | Share of data |
|---|---|---|
| 0 | Functional | 54.31% |
| 1 | Non-Functional | 38.42% |
| 2 | Needs Repair | 7.27% |
Missing a pump that needs repair costs more than sending a crew to a healthy pump: residents lose access to clean water (UN SDG 6). So evaluation does not use accuracy. It uses a social cost function:
```
Social Cost = 5 × FN(Class 2) + 1 × FP(Class 2)
```
A missed repair counts five times more than a false alarm. The 5:1 ratio follows industry data putting reactive emergency repair at 3–5× the cost of routine maintenance.
## Results
Both models were evaluated on the same stratified 20% holdout (11,880 pumps, `random_state=42`).
| Metric | XGBoost (weight 5×) | MLP (α = 0.05) |
|---|---|---|
| Social Cost | **2,752** | 2,879 |
| Class 2 Recall | 0.6385 | 0.5944 |
| Class 1 Precision | 0.8753 | 0.8351 |
| Accuracy | 0.7507 | 0.7501 |
| Class 2 Lift | 8.78× | 8.18× |
| Overfit Gap | 0.038 | 0.122 |
**XGBoost with a Class 2 sample weight of 5 is the final recommendation.** It has the lowest social cost and is the only configuration that passes all five technical requirements (TR-01 recall > 0.50, TR-02 precision > 0.80, TR-03 overfit gap 3.0×). The MLP come …