Bora Acer
projects
Predicting Diabetes Risk with Machine Learning

Predicting Diabetes Risk with Machine Learning

Jul 10, 2023·4 min read
Python

This project builds and deploys two machine learning model, a Random Forest classifier and a Keras neural network to predict diabetes risk using patient health metrics from a 100,000-record Kaggle dataset, achieving 92% accuracy through careful data preprocessing, class rebalancing, and hyperparameter tuning.


Introduction

Diabetes affects hundreds of millions of people worldwide and remains one of the most prevalent chronic diseases. Early detection is critical for effective intervention, yet many individuals go undiagnosed until complications arise. This project explores whether readily available patient health indicators:

  • Age
  • BMI
  • Blood glucose
  • Lifestyle factors

These can reliably predict diabetes risk using machine learning.

The dataset used is the Diabetes Prediction Dataset from Kaggle, which contains 100,000 patient records with nine features including gender, age, hypertension, heart disease, smoking history, BMI, HbA1c level, blood glucose level, and a binary diabetes label.

The Process

The end-to-end pipeline follows a structured flow from raw data to a deployed web application:

Data Cleaning and Exploration

The raw dataset required several cleaning steps before it was ready for modeling. First, duplicate records were dropped from the 100,000 rows, reducing the dataset to approximately 96,128 unique entries. The rare "Other" category in the gender column was removed since it had too few samples to contribute meaningfully to the model.

Smoking history originally contained six categories: never, No Info, current, ever, former, and not current. These were consolidated into three cleaner groups: non-smoker (including "never" and "No Info"), current, and past_smoker (including "ever", "former", and "not current"). This recategorization reduced noise while preserving the meaningful signal in smoking behavior.

Feature Engineering

Categorical variables (gender and smoking history) were transformed using one-hot encoding, converting them into binary columns the models could process. Numerical features: age, BMI, HbA1c level, and blood glucose level were standardized using StandardScaler to normalize their distributions. A correlation matrix heatmap revealed that HbA1c level and blood glucose level had the strongest positive correlations with diabetes, confirming their clinical significance.

Handling Class Imbalance

One of the biggest challenges was the severe class imbalance: 91% of records were negative and only 9% were positive for diabetes. Training directly on this distribution would produce a model that simply predicts "no diabetes" most of the time.

The solution combined SMOTE (Synthetic Minority Oversampling Technique) to generate synthetic positive samples with Random Undersampling to reduce the majority class, producing a balanced training set.

Model Training

Two models were developed:

Simple Model: Random Forest Classifier: A scikit-learn Random Forest was trained with hyperparameter tuning via GridSearchCV across estimator counts (50, 100, 200), max depths (None, 10, 20), minimum samples split (2, 5, 10), and minimum samples leaf (2, 4). The best configuration achieved 92% accuracy with a recall of 94% on the positive class when optimized for recall.

Complex Model: Keras Neural Network: A sequential neural network with three dense layers (16, 4, and 1 neurons) using ReLU and sigmoid activations was trained over 10 epochs with Adam optimizer and binary crossentropy loss. PCA was applied for dimensionality reduction and visualization, projecting the feature space into principal components to understand cluster separability.

Deployment

Both trained models were serialized and loaded into a Flask web application. The app provides two prediction modes: a "Simple" mode using fewer input features and a "Complex" mode that accepts the full feature set including HbA1c and blood glucose levels. Users enter their health data through a web form and receive an instant risk prediction of either HIGH or LOW.

The live application is deployed at diabetes.acerfamily.com.

Results

Both models achieved approximately 92% overall accuracy on the test set. The recall-optimized Random Forest reached 94% recall on the positive (diabetic) class, meaning it correctly identified 94 out of every 100 actual diabetic patients, a critical metric for a medical screening tool where missing a positive case carries real consequences.

Dataset


Command Palette

Search for a command to run...