Skip to content

About

End-to-end regression pipeline predicting annual medical costs from health, lifestyle, and policy data — with explicit data leakage removal, model comparison, and hyperparameter tuning.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🩺 Annual Medical Cost Prediction

Python scikit-learn Pandas XGBoost Status

An end-to-end machine learning regression project that predicts an individual's annual medical cost using demographic, lifestyle, clinical, and insurance policy information. The project focuses on building a realistic prediction pipeline while identifying and preventing data leakage.


📖 Overview

This project predicts annual medical costs using a large healthcare insurance dataset containing 100,000 records and 54 features.

Instead of simply training models for the highest possible score, the focus was on building a realistic machine learning workflow similar to what would be used in production. One of the biggest challenges in this project was identifying and removing data leakage—features that contained information unavailable at prediction time.

The complete workflow covered:

  • Data Understanding
  • Data Cleaning
  • Exploratory Data Analysis
  • Feature Engineering
  • Preprocessing
  • Pipeline Construction
  • Model Training
  • Hyperparameter Tuning
  • Model Evaluation
  • Model Serialization

Duration: July 16 – July 21, 2026


📊 Dataset

  • Source: Kaggle (medical_insurance.csv)
  • Rows: 100,000
  • Columns: 54
  • Target: annual_medical_cost

The dataset includes information such as:

  • Demographics (Age, Gender, Region, Income, Education)
  • Lifestyle Factors (BMI, Smoking Status, Alcohol Consumption)
  • Clinical Measurements (Blood Pressure, LDL, HbA1c)
  • Chronic Medical Conditions
  • Healthcare Utilization
  • Insurance Policy Information

Several billing and claims-related columns were intentionally removed after discovering they leaked information about the target.


🎯 Objective

The goal of this project was to build a regression model capable of estimating a person's annual medical cost before the coverage year begins, using only information that would realistically be available at prediction time.

Rather than maximizing accuracy through leaked information, the project focused on building a model that could generalize to real-world scenarios.


🛠️ Tools Used

  • Python
  • Jupyter Notebook
  • Pandas
  • NumPy
  • Matplotlib
  • Seaborn
  • Scikit-learn
  • XGBoost
  • Joblib

🧹 Data Cleaning & Preventing Data Leakage

One of the most important parts of this project was identifying features that introduced target leakage.

The following columns were removed because they contained information generated after the medical costs had already occurred:

  • Annual Premium
  • Monthly Premium
  • Claims Count
  • Average Claim Amount
  • Total Claims Paid
  • Major Procedure Information
  • Procedure Count Features
  • Person ID

During preprocessing another issue appeared in the alcohol_freq column, where pandas automatically interpreted the string "None" as missing values. This was corrected by treating those values as the actual category representing people who never consume alcohol.

After further analysis:

  • risk_score was retained as a legitimate predictive feature.
  • is_high_risk was removed because it was simply a thresholded version of risk_score.

📈 Exploratory Data Analysis

Some important observations from the dataset:

  • Annual medical cost is heavily right-skewed, with most individuals having relatively low costs and a small number of very expensive cases.
  • Smoking status showed one of the clearest relationships with medical expenses.
  • Medical costs steadily increased with the number of chronic conditions.
  • Region had very little influence on annual cost.
  • BMI and blood pressure alone showed surprisingly weak relationships with the target.
  • Approximately 6.8% of the dataset contained high-cost outliers, but these represented legitimate medical cases rather than errors, so they were retained.

Because of the strong skew in the target variable, log1p transformation was applied before training.


🏗️ Feature Engineering

Two additional features were created:

Feature Purpose
smoker_high_bmi Captures the combined effect of smoking and obesity
total_utilization Combines doctor visits and hospitalizations into a single healthcare utilization metric

Several other engineered features were tested but ultimately excluded because they added little additional information.


⚙️ Preprocessing Pipeline

A complete preprocessing pipeline was built using ColumnTransformer and Pipeline, allowing every model to share exactly the same preprocessing steps.

The pipeline included:

  • Ordinal Encoding for ordered categorical features
  • One-Hot Encoding for nominal categorical variables
  • Standard Scaling for continuous numerical features
  • Binary health condition columns passed directly without transformation

The dataset was split using an 80/20 Train-Test Split, with all preprocessing occurring inside the pipeline to avoid data leakage.


🤖 Models Compared

Seven regression algorithms were trained and evaluated:

  • Linear Regression
  • Ridge Regression
  • Lasso Regression
  • Decision Tree Regressor
  • Random Forest Regressor
  • Gradient Boosting Regressor
  • XGBoost Regressor

The baseline experiments revealed an interesting pattern:

Almost every model achieved a similar performance, suggesting that the limitation came from the available features rather than the choice of algorithm.


🎯 Hyperparameter Tuning

Hyperparameter tuning was performed using:

  • GridSearchCV for simpler models
  • RandomizedSearchCV for ensemble models

Best Results

Model Test R²
Ridge Regression 0.2115
Lasso Regression 0.2118
Decision Tree 0.2006
Random Forest 0.2138
Gradient Boosting 0.2175
XGBoost 0.2171

Hyperparameter tuning roughly doubled the performance of most baseline models while also significantly reducing overfitting.


🏆 Final Model

Gradient Boosting Regressor

Performance

  • R²: 0.2175
  • Best performing tuned model
  • Small train-test gap indicating good generalization

The complete preprocessing pipeline and trained model were saved using Joblib for future predictions.


🔍 Feature Importance

The model identified the following features as the strongest predictors:

  • Chronic Condition Count
  • Risk Score
  • Days Hospitalized
  • Smoking Status
  • Number of Hospitalizations
  • Doctor Visits
  • BMI

Together, Chronic Condition Count and Risk Score accounted for nearly 60% of the model's predictive importance.


🔑 Key Takeaways

  • Detecting and removing data leakage was the most important lesson from this project.
  • Building a proper preprocessing pipeline made model comparison fair and reproducible.
  • Hyperparameter tuning improved every model significantly.
  • Chronic medical conditions explained annual medical cost much better than lifestyle variables alone.
  • More complex algorithms could not overcome the limitations of the available features, demonstrating that high-quality data is often more important than model complexity.

📂 Project Structure

annual-medical-cost-prediction/
│
├── data/
│   └── medical_insurance.csv
│
├── models/
│   └── best_gradient_boosting_model.pkl
│
├── notebooks/
│   └── annual-medical-cost-prediction.ipynb
│
├── .gitignore
└── README.md

🚀 How to Run

Clone the repository:

git clone https://github.com/kushwahaaa27/annual-medical-cost-prediction.git

Install the required libraries:

pip install pandas numpy matplotlib seaborn scikit-learn xgboost joblib

Open the notebook and run all the cells.


💬 Feedback

If you have any suggestions or notice something that could be improved, feel free to open an issue or share your feedback.


📝 Note

This project was completed between July 16 and July 21, 2026, and uploaded later as part of my Machine Learning portfolio.

About

End-to-end regression pipeline predicting annual medical costs from health, lifestyle, and policy data — with explicit data leakage removal, model comparison, and hyperparameter tuning.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages