Machine learning
What five models taught me about predicting heart disease risk
Training and comparing models on clinical data, and what the numbers do not tell you.
For our final-year project we built CVD-AI: a web app that takes 15 inputs (age, sex, blood pressure, cholesterol, smoking and so on) and predicts whether a person will develop coronary heart disease within ten years. Behind the form sit five scikit-learn classifiers, served by a small FastAPI service. The repo's README has a results table, and the line under it names the SVM as the best model with a ROC-AUC of 0.7919. This post is about how to read that table, and where it stops being evidence. File paths below are relative to the repo's cvd-ai-python/ folder.
The data is lopsided before any model sees it
The source is the Framingham dataset, data/raw/framingham.csv: 4,240 rows, 15 feature columns and a target called TenYearCHD. Counting the target column gives 644 positives, about 15%. A model that answers "no" for everyone is already right about 85% of the time.
Preprocessing lives in src/data_preprocess.py. It fills missing values (glucose has the most gaps, 388 rows, filled with the median glucose of non-diabetic patients), drops outliers with a 1.5 times IQR rule applied to seven continuous columns one after another, and standardizes every column with StandardScaler. What comes out, in data/processed/Y_dataset.csv, is 3,639 rows with 484 positives, about 13%.
Counting both files shows a side effect of the outlier step. It removed 160 of the 644 positives, roughly a quarter, but only 441 of the 3,596 negatives, roughly an eighth. Rows with extreme blood pressure or glucose are positive more often than the rest, so trimming "outliers" trimmed the class the project exists to find.
Five models and SMOTE
We trained five model types: a decision tree, a random forest, logistic regression, a support vector machine with an RBF kernel, and k-nearest neighbours. In src/train.py they share one 80/20 split, and the imbalance is handled with SMOTE. This is the core of that script:
x_train, _x_test, y_train, _y_test = train_test_split(
x_df, y_df, test_size=0.2, random_state=4
)
smote = SMOTE(random_state=4)
x_train_balanced, y_train_balanced = smote.fit_resample(x_train, y_train)
for model_filename, build_model in MODEL_BUILDERS.items():
model = build_model()
model.fit(x_train_balanced, y_train_balanced)
# ... each model is then pickled into Trained_Models/SMOTE creates synthetic minority rows by interpolating between a real positive and one of its nearest positive neighbours, until both classes are the same size. It runs on the training portion only, so the test set keeps the real ratio. Oversample before splitting and synthetic rows built from test patients end up in the training set.
One thing to know before reading the table: the repo has two sets of training code. Model_Training/ holds one script per model, four of them with a randomized hyperparameter search. src/train.py is a single script with fixed settings, and it is the one the README and the Makefile point to. The pickles in Trained_Models/, which are what the table scores, match the per-model scripts and not train.py. Their stored parameters show it: the saved tree has max_depth=2 where train.py sets 8, and the saved SVM has random_state=42 where train.py sets 4.
How they were compared
src/evaluate.py rebuilds the split with test_size=0.2, random_state=4, loads each pickle, predicts on the test rows and computes five metrics. The README table and VisualizedImages/model_metrics.csv hold the same numbers:
Model Accuracy Precision Recall F1 ROC-AUC
Decision Tree 0.8846 0.0000 0.0000 0.0000 0.6807
Random Forest 0.8063 0.2645 0.3810 0.3122 0.6781
Logistic Regression 0.6909 0.2191 0.6548 0.3284 0.7400
SVM 0.7761 0.3054 0.7381 0.4321 0.7919
KNN 0.7033 0.1413 0.3095 0.1940 0.5707Accuracy picks the wrong winner
Sort by accuracy and the decision tree wins at 0.8846. Its precision and recall are both zero. The saved confusion matrix (VisualizedImages/confusion_matrix_best_model.png, drawn for the SVM) shows the test set: 728 people, 644 negative and 84 positive. 644 divided by 728 is 0.8846. The tree answered "no risk" for all 728 and took the top accuracy score for it. The cause is in Model_Training/DecisionTree.py, which imports SMOTE but fits on the unbalanced training data. The saved tree has four leaves, and negatives are the majority in every one, so it cannot answer "yes" for any input.
Two other columns describe what a screening model does for people:
- Recall: of the people who did develop heart disease, what share did the model flag?
- Precision: of the people the model flagged, what share did develop it?
For the SVM the matrix reads 62 true positives, 22 false negatives, 141 false positives and 503 true negatives. It caught 62 of the 84 real cases (recall 0.7381) and raised 203 flags, of which 141 were false alarms (precision 0.3054). In screening, a missed case usually costs more than an extra check-up, so favouring recall is defensible. Seven in ten flags being wrong is still a high price.
ROC-AUC, the column the README ranks by, measures something else: the chance that the model gives a randomly chosen positive a higher score than a randomly chosen negative. It judges the ranking, not any single threshold. 0.5 is a coin flip. The SVM's 0.7919 means its ranking is useful but far from sharp, and KNN's 0.5707 is close to guessing. The other four columns use the default decision rule of model.predict. We never tuned a threshold, so precision and recall could be traded against each other without retraining anything.
What the table does not tell you
One split, 84 positives. A single patient moves recall by more than a percentage point. evaluate.py does no cross-validation and reports no intervals, so some of the gaps between models may be noise.
The best row is the weakest evidence. Model_Training/SVM.py splits with random_state=42, while evaluate.py splits with random_state=4. Different seeds give different test sets, so most of the rows the SVM is scored on, roughly four in five, sat in its own training split. The other four scripts use seed 4. The SVM script also rescales its training data with a second scaler that evaluate.py never applies, so the model is scored on inputs prepared differently from the ones it was fitted on.
The scaler saw the test rows. data_preprocess.py fits StandardScaler on all rows before any split. It is a small leak, but it is one.
The app does not feed the models what they were trained on. The scaler is fitted during preprocessing and never saved. The FastAPI controllers put the request values straight into the model:
def predict_svm(input_data: ModelInput) -> ModelOutput:
features = np.array([[v for v in input_data.model_dump().values()]])
prediction = svm_model.predict(features)[0]
probabilities = svm_model.predict_proba(features)[0].tolist()
# ... returns ModelOutput(prediction, probabilities)The React form (CVDPredictionForm.tsx in cvd-frontend/) scales the numeric inputs itself, with a min-max formula to the range -1 to 1 over hand-written bounds, and sends the yes/no fields as 0 or 1. That is not the transform the models were fitted on, so the table says nothing about the predictions the form returns.
A study project, not a diagnostic tool
Even with those four problems fixed, this is one public dataset, one split, no check that predicted probabilities match observed rates, and no second dataset to validate on. Nothing here should inform a decision about a real person's health.
What the project does show is narrower. On imbalanced medical data, accuracy rewards a model that finds no one. Recall and precision say who gets missed and who gets a false alarm. And a score is only as trustworthy as the split and the preprocessing behind it. If I rebuilt it, the scaler would sit in a scikit-learn Pipeline saved with each model, every model would share one split, and the comparison would run on cross-validation.