Case Study: Personalised Health Alerts from ECG Data — Random Forest vs LSTM
Detecting abnormal heartbeats in 109,000+ ECG recordings. Random Forest reached 93.3% accuracy with few false alarms; the LSTM caught 97.9% of abnormal beats — and the trade-off between them is the real story.
- healthcare
- machine learning
- lstm
- smote
- wearables
Heart disease is the world's leading cause of death, and wearable ECG devices can now watch the heart around the clock. But a stream of heartbeats is only useful if a model can decide, beat by beat, when something is wrong — and raise an alert that people will trust. This project, built with Haythem Elmessiry at Canadian University Dubai, compares two very different ways of making that decision.
- 109,446
- ECG heartbeats (MIT-BIH)
- 17.2%
- Abnormal beats — a heavily imbalanced problem
- 93.3%
- Random Forest accuracy
- 97.9%
- LSTM recall — almost no abnormal beat missed
- 0.958
- Random Forest ROC AUC (LSTM 0.954)
- +12.2 pts
- Recall gained from SMOTE
The problem
Abnormal beats are rare. In the training data only about one heartbeat in six is abnormal, so a model can look accurate while quietly ignoring the cases that matter. And the two kinds of mistakes are not equal: a missed anomaly puts a patient at risk, while too many false alarms teach people to ignore the device. A useful health alert has to balance both.
The data
The project uses the ECG Heartbeat Categorization dataset, a pre-processed version of the MIT-BIH Arrhythmia Database. Every row is a single heartbeat stored as 187 amplitude values. The five original classes were merged into a binary task — Normal against Abnormal — which is how an alert system actually has to decide.
Heartbeats per class in the training set
87,554 training heartbeats. Everything except Normal (N) is merged into the Abnormal class.
- Normal (N)72,471
- Unclassifiable (Q)6,431
- Ventricular (V)5,788
- Supraventricular (S)2,223
- Fusion (F)641
Bar length = heartbeats
To stop the models from ignoring the minority class, the training set was rebalanced with SMOTE, which creates new synthetic abnormal beats between existing ones. It was applied to the training data only, so the test set still reflects real-world proportions. After SMOTE the training set held 72,471 beats of each class.
The approach
Pipeline
The same balanced heartbeats feed two models: one sees summary statistics, the other sees the raw signal.
ECG heartbeat
187 amplitude samples per beat
Binary labels
Normal vs Abnormal
SMOTE
Rebalance the training set only
13 statistical features
Mean, std, kurtosis, skew, IQR, RMS… then scaled
Raw sequence
187 × 1 time series
Models
Random Forest
100 trees
LSTM
64 → 32 units
Health alert
Normal or abnormal beat
The two models
| Random Forest | LSTM | |
|---|---|---|
| Input | 13 statistical features per beat | Raw 187-point ECG sequence |
| Architecture | 100 trees, max depth 15, balanced class weights | LSTM(64) → LSTM(32) with batch norm and dropout → Dense(16) → sigmoid |
| Training | About 1 minute | About 73 minutes, Adam, early stopping |
| Strength | Fast, interpretable, few false alarms | Learns patterns over time, catches more anomalies |
Results
Both models were tested on 21,892 heartbeats they had never seen, at a decision threshold of 0.5.
Test-set performance
From the saved notebook run in the repository.
| Metric | Random Forest | LSTM |
|---|---|---|
| Accuracy | 93.33% | 58.29% |
| Precision | 80.69% | 28.98% |
| Recall (sensitivity) | 80.60% | 97.88% |
| F1-score | 80.65% | 44.72% |
| ROC AUC | 0.958 | 0.954 |
| Training time | 53 s | 73 min |
Two very different kinds of mistake
Confusion matrices on the 21,892 test heartbeats.
Random Forest
20,432 of 21,892 correct
LSTM
12,760 of 21,892 correct
Rows = actual class, columns = predicted class. The diagonal holds correct predictions; darker cells hold a larger share of that row. Hover a cell for details.
Missed anomalies vs false alarms
The clinical trade-off in one picture: the LSTM misses 9× fewer abnormal beats, but raises 12× more false alarms.
- RF missed732
- LSTM missed80
- RF false alarms728
- LSTM false alarms9,052
Bar length = heartbeats
Did SMOTE help?
An ablation study trained the Random Forest with and without SMOTE. Rebalancing lifted recall on abnormal beats by 12.2 percentage points while overall accuracy barely moved — exactly the trade an alert system wants.
Random Forest with and without SMOTE
| Without SMOTE | With SMOTE | Change | |
|---|---|---|---|
| Recall (abnormal) | 68.39% | 80.60% | +12.2 pts |
| F1-score | 78.35% | 80.65% | +2.3 pts |
| Accuracy | 93.49% | 93.33% | −0.2 pts |
How robust are they to a noisy sensor?
Real wearables are noisy, so a second ablation added Gaussian noise to the test signals. The hand-crafted statistics proved fragile: Random Forest F1 almost halved with a tiny amount of noise (σ = 0.01). The LSTM, which reads the raw signal, held steadier — though at a lower level throughout.
F1-score as sensor noise increases
Gaussian noise of standard deviation σ added to each test heartbeat.
Hover the chart to compare F1-score at each point.
Limitations and next steps
- Tune the LSTM's decision threshold on a validation precision–recall curve to reduce alarm fatigue.
- The dataset is split by heartbeat, so beats from the same patient can appear in both training and test sets; a patient-level split would give a more realistic estimate.
- Add noise augmentation during training, or features that survive noise, before using real wearable signals.
- Try CNN-LSTM hybrids and attention, and use SHAP to explain the Random Forest's decisions.
- MIT-BIH comes from 47 patients — the models need validating on larger, more diverse populations.
- LSTM results vary between training runs — another run reached 83.85% accuracy and 96.42% recall — so the figures here come from the single run whose code and outputs are in the repository.
What I learned
Accuracy alone would have told the wrong story here: the less accurate model is the one that almost never misses a sick heartbeat. Choosing between the two is a clinical decision about which mistake is more costly, and the near-identical AUC scores showed that much of the gap comes from a single threshold, not the model itself. Measuring the right things — recall, false alarms, robustness to noise — mattered more than chasing one headline number.
Tools
| Area | Tools |
|---|---|
| Data & features | pandas, NumPy, SciPy |
| Imbalance | imbalanced-learn (SMOTE) |
| Models | scikit-learn (Random Forest), TensorFlow / Keras (LSTM) |
| Visualisation | Matplotlib, Seaborn |