Data Preparation, Preprocessing Leaks & Clinical Imbalance
CSC3119 · Lab

Clinical Feature Preparation, Scaler Leakage & Imbalance Simulator

A 5%-positive clinical dataset is where intuition about accuracy breaks. Build the confusion matrix for three models, then see what scaler leakage does to the number you'd report versus the number patients get.

  1. 1 · Predict0/1
  2. 2 · Experiment0/3
  3. 3 · Explain0/1
  4. 4 · Code it0/2
  5. 5 · Challenge
  6. 0/5 tasks done

Predict first

Commit to a guess before you touch anything. Being wrong here is how it sticks.

Prevalence is 5%. What accuracy does 'always predict no stroke' get?

Experiment

Move the controls and watch the outputs. Goals tick off automatically when you reach them.

The accuracy paradox

Confusion matrix
Predicted strokePredicted no stroke
Actual stroke6684
Actual no stroke432807
Accuracy
95.8%
Recall
44.0%
Precision
60.6%
F2
0.465
Strokes missed
84

Recall (sensitivity) is what triage cares about: how many stroke patients we catch.

Leakage: the number you report vs the number you get

Validation recall (what you report)86.0%
Recall on new patients86.0%

Preprocessing fitted on X_train only — validation is an honest preview of production.

Explain it

Put what you saw into words. This is what the exam will ask you to do.

Why does accuracy fail on the stroke dataset, and why must scaling and SMOTE only be fitted on the training split?

Code it

Write real code against real data. Python and SQL run right here in your browser and hidden tests check your work; the AI reviews it like an examiner.

Pythonwarm-up

Metrics that don't lie

Write clinical_metrics(y_true, y_pred) returning a dict with accuracy, recall, precision and f2 (all floats in 0–1). If there are no positive predictions, precision is 0; if precision and recall are both 0, F2 is 0. Use F2 = 5·P·R / (4·P + R).

Then print the metrics for the "always no stroke" model on y (the stroke column of the sample data).

Data in the working directory: stroke_sample.csv
Ctrl/⌘ + Enter to run

Challenge

Fixed variations, a new problem every day, and unlimited AI challenges built from your current settings.

Stroke Class Imbalance Minority Count

In a regional hospital dataset of 5,000 patients, stroke prevalence is exactly 4.8%. How many patients in this cohort have suffered a stroke (the positive minority class)?