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:

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

Worked example: Sensitivity & specificity

Given: TP 90, FN 10, FP 50, TN 850

Find: Se, Sp.

Answer: 0.90, 0.944

Worked example: Screening PPV

Given: Se 0.9, Sp 0.944, prevalence 1%

Find: PPV.

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.

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

Applications

Key takeaways

Going further