ECG Heartbeat Classifier

A small neural network that sorts single heartbeats into five types. It runs entirely in your browser with ONNX Runtime Web. Nothing is uploaded anywhere.

1. Pick a sample beat

2. Or provide your own beat

The model expects one heartbeat as 187 comma-separated numbers, normalized to the range 0 to 1 (min-max per beat), with the window starting at the R peak. That is the same format as the MIT-BIH beats the model was trained on.

3. Inspect and classify

Result

Confidence is temperature-calibrated (T = 1.74, fit on held-out beats), so a reported 90% means roughly 90 out of 100 such beats are classified correctly. This is a research demo, not a medical device. Do not use it for diagnosis.

The data

The models were trained on the MIT-BIH Arrhythmia Database heartbeat set: 109,446 single heartbeats, each 187 samples long, labeled into five AAMI classes. One example of each class is plotted below, drawn from the sample files.

How well does it really work?

The honest headline comes from a stricter test: 5-fold cross-validation grouped by patient record, so no patient's beats appear on both sides of any split (109,406 beats reconstructed from the 48 MIT-BIH records). Pooled accuracy is 0.87, with per-class F1 of N 0.93, V 0.58, Q 0.68, and S/F near zero. The rare beat classes are the hard part once patient leakage is removed.

The 98%+ figures in the comparison table below are random train/test splits, which mix beats from the same patients in train and test and read optimistic. They are kept as the in-split reference; the grouped number above is the one to quote. Full numbers in the repo README.

Model comparison

Three approaches were trained and compared: a convolutional network with Conv1D layers over the raw beat, a feed-forward ANN (187 inputs, hidden layers of 256, 128 and 64 ReLU units, 5-way softmax), and k-nearest neighbors on the 187-sample beat vectors. The ANN is the model deployed on this page.

ModelTest accuracyTest beatsMeasured
CNN98.7%32,834training notebook
ANN (deployed)98.8%21,892shipped ONNX model
KNN97.5%32,834training notebook

These are random train/test splits, which mix beats from the same patients in train and test and therefore read optimistic. Confusion matrix for the deployed ANN, computed by running the shipped model on the repo's 21,892-beat test file (rows: true label, columns: predicted label):

NSVFQ
N18,037462654
S79472500
V2551,404140
F15071400
Q160801,584

Most errors are supraventricular beats called normal, and fusion beats called normal or ventricular, the known hard cases in this dataset.

Extended evaluation in the notebook: the binary experiment now also trains a from-scratch binary CNN (same convolutional stack, dedicated 1-unit sigmoid head) as a control for the transfer-learning variant. Details in the repo.

About the model

Feed-forward network (187 inputs, hidden layers of 256, 128 and 64 ReLU units, 5-way softmax), trained on the MIT-BIH Arrhythmia Database heartbeat set (109,446 beats, five AAMI classes: normal, supraventricular ectopic, ventricular ectopic, fusion, unknown). On a held-out-style test split it reaches 98.8% accuracy, but that split mixes beats from the same patients in train and test, so it is optimistic. The stricter patient-grouped evaluation (pooled accuracy 0.87) is summarized in the "How well does it really work?" section above. The browser model is the same network converted to ONNX. Full analysis, code and numbers: github.com/nepalanurag/ECG-Classification.