In healthcare, a prediction on its own isn’t enough. If a model says a patient is at high risk of stroke, a doctor will ask why. If the answer doesn’t make clinical sense, they won’t trust the model, and they shouldn’t.
Explainable AI (XAI) tools answer that “why”. In this post I train a simple stroke prediction model and explain it two ways:
- SHAP for global feature importance: what drives the model across all patients.
- LIME for local explanations: why the model scored one specific patient the way it did.
Code: Zilch123/Stroke-Prediction-XAI (there’s an “Open in Colab” link in the notebook)
The data
I used the public stroke prediction dataset from Kaggle. Each row is a patient with age, gender, hypertension, heart disease, average glucose level, BMI, smoking status, work type, residence type and marital status, plus whether they had a stroke.
Only about 5% of patients had a stroke, so the classes are very imbalanced. I label-encoded the categorical columns, filled missing BMI with the mean, and balanced the training set with SMOTE:
from imblearn.over_sampling import SMOTE
from sklearn.model_selection import train_test_split
X = df_encoded[numerical_col + categorical_col]
y = df_encoded['stroke']
X.loc[X['bmi'].isna(), 'bmi'] = round(X['bmi'].mean(), 1)
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.3, random_state=42)
X_train_res, y_train_res = SMOTE().fit_resample(X_train, y_train.ravel())Picking a model
I tried XGBoost, random forest and logistic regression. For screening, missing a real stroke case is worse than a false alarm, so I compared recall on the stroke class:
| Model | Stroke recall | Stroke precision | Accuracy |
|---|---|---|---|
| Logistic regression | 0.61 | 0.11 | 0.75 |
| XGBoost | 0.36 | 0.11 | 0.83 |
| Random forest | 0.30 | 0.13 | 0.87 |
Logistic regression caught the most stroke cases, so I went with it. Its precision is low, which means a lot of false positives. That’s acceptable for a first screen, but not for a diagnosis.
SHAP: what matters overall
import shap
explainer = shap.Explainer(log_cl, X)
shap_values = explainer(X)
shap.plots.beeswarm(shap_values)
Each dot is a patient. Its position shows how much that feature pushed the prediction towards stroke (right) or away from it (left), and its colour shows the feature value (red is high, blue is low).
Age dominates. Older patients (red) are pushed strongly towards stroke and younger ones away from it, which matches what we know clinically. Glucose level behaves sensibly too: high values push slightly towards stroke.
LIME: one patient at a time
SHAP shows the overall picture, but a doctor looks at one patient at a time. LIME fits a small, simple model around a single prediction to explain it:
import lime.lime_tabular
explainer = lime.lime_tabular.LimeTabularExplainer(
X.values, feature_names=X.columns.tolist(),
class_names=['Not to have Stroke', 'Prob to have Stroke'], mode='classification')
exp = explainer.explain_instance(X.values[10], log_cl.predict_proba, num_features=5)
exp.show_in_notebook(show_table=True)Patient 10 is 81 with hypertension. The model gives a 67% probability of stroke, and age above 61 is the biggest reason.

Patient 3 is 49 with no hypertension. The model gives a 20% probability.

What the explanations caught
Look at hypertension in both views. In the SHAP plot, patients with hypertension (the red dots) are pushed away from stroke. LIME shows the same thing: for patient 10, hypertension counts against stroke, and for patient 3, having no hypertension counts towards it.
That’s backwards. Hypertension is a major stroke risk factor. Residence type ranking as the second most important feature is also hard to explain clinically.
The accuracy table would never have shown this. The likely causes are in how the model was built:
- The features weren’t scaled, and the logistic regression solver didn’t fully converge.
- Hypertension is strongly tied to age, so the model can shift weight between them in odd ways.
- SMOTE creates synthetic patients by interpolating, which gives yes/no columns like hypertension in-between values.
- Label encoding turns categories like work type and residence type into numbers with a fake order, which a linear model then takes literally.
This is where XAI earns its place in healthcare. It shows you when a model has learned something wrong, before anyone relies on it. The next step would be to scale the features, one-hot encode the categories and check that the explanations line up with clinical knowledge.