Machine Learning · Research
Explainable diabetes prediction with Random Forest and SHAP
How a tuned Random Forest reached 0.87 ROC-AUC on diabetes risk, why ROC-AUC mattered more than accuracy, and how SHAP explains every prediction.
Adithya Reddy · · Updated · 5 min read
Explainable diabetes prediction means using a machine-learning model that estimates a person's diabetes risk and also shows which inputs produced that estimate. In my peer-reviewed study, a tuned Random Forest reached over 82% accuracy and a ROC-AUC of 0.87, and SHAP attributes each prediction to the measurements behind it. The model runs as an interactive Streamlit web app.
The problem: a risk score needs a reason
A risk score without a reason is a number someone must take on faith. In healthcare, that isn't enough: a clinician or patient needs to know why the model reached its answer.
That principle drove my peer-reviewed paper, Diabetes Data Analysis and Machine Learning Based Prediction Model on Streamlit Web App (IJSRED, October 2022). The goal was a model that predicts diabetes risk accurately and explains each prediction. The same work is summarised on the Diabetes Risk Prediction project page.
The approach: a tuned Random Forest
The model is a Random Forest, which trains many decision trees on bootstrapped samples of the data and averages their votes. It captures non-linear interactions between clinical features and needs no feature scaling.
Why ROC-AUC mattered more than accuracy
Accuracy counts correct predictions at one fixed threshold. For screening, that threshold is a policy choice: you may accept more false alarms to miss fewer real cases. The model should be judged on how well it ranks risk, independent of where the line is drawn.
ROC-AUC measures exactly that. It equals the probability that the model scores a randomly chosen positive case above a randomly chosen negative one:
An AUC of means the model ranks a person with diabetes above a person without it in 87% of random pairs. A coin flip scores ; a perfect ranker scores .
Tuning with GridSearchCV
Default hyperparameters are rarely optimal, so I tuned them with GridSearchCV. It scores every combination in a grid using -fold cross-validation, so the winner isn't flattered by one lucky split. The cost is easy to predict. A grid with , and values for three parameters, cross-validated over folds, trains
models. The sketch below uses a grid with 5 folds, so it trains forests before refitting the best one.
# A minimal sketch of the approach, not the paper's exact code.
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
grid = {
"n_estimators": [100, 300, 500],
"max_depth": [None, 6, 10],
"min_samples_leaf": [1, 3, 5],
}
search = GridSearchCV(
RandomForestClassifier(random_state=42),
grid,
scoring="roc_auc", # optimise ranking, not raw accuracy
cv=5,
n_jobs=-1,
)
search.fit(X_train, y_train)
model = search.best_estimator_Setting scoring="roc_auc" matters: the search optimises the metric that matches the decision, not a default.
How each prediction is explained with SHAP
Hundreds of trees make an accurate but opaque model. SHAP (SHapley Additive exPlanations) opens it using Shapley values from cooperative game theory. Each feature is a player, and its contribution is its average marginal effect across every possible coalition of the other features :
The property that makes SHAP trustworthy is additivity. A prediction decomposes exactly into a baseline plus one contribution per feature:
Here is the average prediction across the data. Nothing is hidden in a residual: every point of risk is attributed to a specific input.
That gives two views of the model:
- Global: which features drive risk across the population.
- Local: for one person, which values raised the score and which lowered it.
The local view builds trust. Instead of "your risk is high", the model shows which measurements produced that score.
import shap
explainer = shap.TreeExplainer(model) # exact, fast Shapley values for tree models
shap.plots.waterfall(explainer(X_test)[0]) # one person's prediction, feature by featureTreeExplainer computes Shapley values exactly for tree ensembles in polynomial time, avoiding the exponential cost of the formula above.
The Streamlit app
A model in a notebook helps nobody. I deployed it as a Streamlit web app: enter clinical values, receive a risk estimate and the SHAP breakdown behind it. Streamlit turns a Python script into an interactive interface with no separate front end.
Results
The tuned Random Forest achieved:
- Accuracy: over 82%.
- ROC-AUC: 0.87, so it ranks a person with diabetes above a person without it in 87% of random pairs.
- Explanation: one SHAP breakdown for every prediction.
These are the figures reported in the peer-reviewed paper (IJSRED, October 2022).
Limitations
- Not a diagnostic tool. It is research software built to show how an explainable risk model works. It cannot diagnose diabetes or replace a clinician.
- Ranking is not calibration. A ROC-AUC of 0.87 says the model orders people well. It does not say a predicted risk of, say, 30% matches a true 30% chance.
- Explanations describe the model, not causes. SHAP shows which inputs moved this model's score. A feature that raises the score is not thereby a cause of diabetes.
- Reported figures come from one study. Results depend on the dataset used and may differ for other populations.
What's next
Two lessons from this work carried forward:
- Choose the metric that matches the decision. How the output will be used defines what "good" means.
- Ship the explanation with the prediction. People act on a model they can question and ignore one they can't.
The second lesson shapes the agentic systems I build today; see what I'm building in 2026 and the GBNI framework.
Frequently asked questions
What does a ROC-AUC of 0.87 mean?
It means the model ranks a randomly chosen person with diabetes above a randomly chosen person without it 87% of the time. A random guess scores 0.5 and a perfect model scores 1.0.
Why use SHAP for healthcare machine learning?
SHAP attributes each prediction exactly to its input features, so clinicians and patients can see which measurements raised or lowered a risk score. That transparency is essential when a model informs health decisions.
Why choose a Random Forest for diabetes prediction?
A Random Forest captures non-linear relationships between clinical features, is robust to noise, needs no feature scaling, and works with SHAP's fast TreeExplainer for exact explanations.
How accurate was the diabetes prediction model?
The tuned Random Forest achieved over 82% accuracy and a ROC-AUC of 0.87, published in a peer-reviewed paper in IJSRED in October 2022.