Healthcare Data Science & Medical AI
Turning medical data into decisions. Learn medical data types (signals, images, EHR, genomics), diagnostic test metrics (sensitivity, specificity, PPV, NPV, ROC/AUC) and the effect of prevalence, machine-learning basics (logistic regression, trees, CNNs), training/validation/test splits and leakage, bias and fairness, explainability, clinical validation and regulation of AI as a medical device — with Python computing test metrics and training a logistic-regression model.
Hospitals generate vast data: ECGs, images, lab results, notes, genomic sequences, wearable streams. Machine learning can detect diabetic retinopathy, arrhythmias, lung nodules and sepsis risk — sometimes as well as specialists.
But a model that looks accurate in the lab can fail in the clinic: different patients, scanners and prevalence change its behaviour. Understanding evaluation metrics, bias and validation is as important as building the model.
An analogy
A diagnostic model is like a smoke alarm: sensitivity is how reliably it goes off when there's a fire; specificity how rarely it goes off when there isn't. In a house that almost never burns (low prevalence), most alarms will be false — even with a good detector.
Medical data
Structured EHR data (labs, vitals, codes), unstructured notes (NLP), time series (ECG, ICU monitors, wearables), images (radiology, pathology slides, retinal photos), and omics. Data are messy: missing values, inconsistent units, label noise, and privacy constraints (HIPAA, GDPR, DPDP Act) govern access.
Evaluating diagnostic performance
Accuracy is misleading when classes are imbalanced. Report sensitivity and specificity (properties of the test), PPV and NPV (depend on prevalence), ROC curves and AUC (threshold-independent), precision–recall curves for rare conditions, and calibration (do predicted probabilities match observed rates?). Choose operating thresholds by the clinical cost of false negatives vs false positives.
Model development pitfalls
Common failure modes:
- Data leakage — the same patient in train and test, or features that encode the label
- Shortcut learning — models detecting hospital markers or scanner types instead of disease
- Dataset shift — new sites, devices or populations change performance
- Bias — under-represented groups receive worse predictions (e.g. SpO₂ and skin tone)
- Overfitting — use cross-validation, regularisation, and external validation
Clinical deployment and regulation
Software as a Medical Device (SaMD) is regulated: FDA 510(k)/De Novo with Good Machine Learning Practice principles and predetermined change control plans; EU MDR plus the AI Act for high-risk systems. Clinical validation uses prospective studies and reporting guidelines (TRIPOD-AI, CONSORT-AI, STARD). Deployment needs integration into workflow, monitoring for drift, explainability (saliency maps, SHAP) and human oversight.
Formulas
- Sensitivity & specificity:
Se = TP/(TP + FN); Sp = TN/(TN + FP) - PPV with prevalence p (Bayes):
PPV = Se·p / (Se·p + (1 − Sp)(1 − p)) - F1 score:
F1 = 2·PPV·Se/(PPV + Se) - Logistic regression:
P(y=1) = 1/(1 + e^(−(w·x + b))) - Cross-entropy loss:
L = −Σ[y ln p + (1 − y) ln(1 − p)]
Worked example: Sensitivity & specificity
Given: TP 90, FN 10, FP 50, TN 850
Find: Se, Sp.
- 90/100
- 850/900
Answer: 0.90, 0.944
Worked example: Screening PPV
Given: Se 0.9, Sp 0.944, prevalence 1%
Find: PPV.
- 0.009/(0.009 + 0.0554)
Answer: 0.14 — 6 of 7 positives are false
Worked example: Number needed to screen
Given: Prevalence 1%, Se 0.9
Find: People screened per true case found.
- 1/(0.01 × 0.9)
Answer: ≈ 111
Diagnostic metrics, prevalence and a logistic-regression classifier
# Diagnostic test metrics from a confusion matrix, and the effect of prevalence
TP, FN, FP, TN = 90, 10, 50, 850
sens = TP / (TP + FN); spec = TN / (TN + FP)
ppv = TP / (TP + FP); npv = TN / (TN + FN)
acc = (TP + TN) / (TP + FN + FP + TN)
f1 = 2 * ppv * sens / (ppv + sens)
print(f"sensitivity {sens:.2f}, specificity {spec:.3f}, PPV {ppv:.3f}, NPV {npv:.3f}, accuracy {acc:.3f}, F1 {f1:.3f}")
for prev in (0.10, 0.01):
p = sens * prev / (sens * prev + (1 - spec) * (1 - prev))
print(f"prevalence {prev*100:.0f}%: PPV = {p:.3f}")
# A tiny logistic-regression model trained by gradient descent (heart-rate variability feature)
import math, random
random.seed(0)
data = [(random.gauss(60, 10), 0) for _ in range(100)] + [(random.gauss(35, 10), 1) for _ in range(100)]
w, b = 0.0, 0.0
for epoch in range(2000):
gw = gb = 0.0
for x, y in data:
z = w * (x - 50) / 10 + b
p = 1 / (1 + math.exp(-z))
gw += (p - y) * (x - 50) / 10; gb += (p - y)
w -= 0.05 * gw / len(data); b -= 0.05 * gb / len(data)
correct = sum((1 / (1 + math.exp(-(w * (x - 50) / 10 + b))) > 0.5) == bool(y) for x, y in data)
print(f"logistic model: w={w:.2f}, b={b:.2f}, training accuracy {correct/len(data)*100:.1f}%")
# sensitivity 0.90, specificity 0.944, PPV 0.643, NPV 0.988, accuracy 0.940, F1 0.750
# prevalence 10%: PPV = 0.643
# prevalence 1%: PPV = 0.141
# logistic model: w=-2.59, b=-0.86, training accuracy 89.5%The same test that is right 64% of the time when it says 'positive' in a 10%-prevalence clinic is right only 14% of the time in 1%-prevalence population screening — Bayes' theorem in action. The logistic model learns that low HRV predicts the positive class; real models are trained on separate data and validated on held-out and external datasets.
Common mistakes
- Wrong: Reporting accuracy on imbalanced data. — Right: Report Se, Sp, PPV, NPV, AUC and calibration.
- Wrong: Random split of images from the same patient. — Right: Split by patient; validate externally.
- Wrong: Assuming lab performance transfers. — Right: Prospective validation and drift monitoring.
Applications
- Radiology AI: Nodule, fracture and stroke detection triage.
- Cardiology: AF detection on smartwatches; ECG-based ejection fraction.
- Ophthalmology: Autonomous diabetic-retinopathy screening.
- Hospital operations: Sepsis and deterioration early warning.
Key takeaways
- Se/Sp describe the test; PPV/NPV depend on prevalence.
- Beware leakage, shortcuts, bias and dataset shift.
- Validate externally and prospectively.
- AI in care is regulated as a medical device.
Going further
- Federated learning: Training across hospitals without sharing raw data.
- Foundation models: Large pre-trained models for images and clinical text.
- Causal inference: Moving from prediction to treatment-effect estimation.