Najma Nour
← All articles

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
anomalyCASE STUDY /Cardiacanomalies.NAJMA NOUR

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.

The two models

Random ForestLSTM
Input13 statistical features per beatRaw 187-point ECG sequence
Architecture100 trees, max depth 15, balanced class weightsLSTM(64) → LSTM(32) with batch norm and dropout → Dense(16) → sigmoid
TrainingAbout 1 minuteAbout 73 minutes, Adam, early stopping
StrengthFast, interpretable, few false alarmsLearns 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.

MetricRandom ForestLSTM
Accuracy93.33%58.29%
Precision80.69%28.98%
Recall (sensitivity)80.60%97.88%
F1-score80.65%44.72%
ROC AUC0.9580.954
Training time53 s73 min

Two very different kinds of mistake

Confusion matrices on the 21,892 test heartbeats.

Random Forest

20,432 of 21,892 correct

NormalAbnormalNormal17,390728Abnormal7323,042

LSTM

12,760 of 21,892 correct

NormalAbnormalNormal9,0669,052Abnormal803,694

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 SMOTEWith SMOTEChange
Recall (abnormal)68.39%80.60%+12.2 pts
F1-score78.35%80.65%+2.3 pts
Accuracy93.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.

Random ForestLSTM
0.0%25.0%50.0%75.0%100.0%00.010.050.100.20Noise level (σ)Random ForestLSTM

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

AreaTools
Data & featurespandas, NumPy, SciPy
Imbalanceimbalanced-learn (SMOTE)
Modelsscikit-learn (Random Forest), TensorFlow / Keras (LSTM)
VisualisationMatplotlib, Seaborn