Downloads · 30 days
0
drukeroni/medical-cost-gradient-boosting
medical-cost-gradient-boosting is a machine learning model from drukeroni. Use it for the machine learning task on the model card, and read the license before you ship it in a product. The card lists the license as mit.
https://www.youtube.com/watch?v=s9wRsR2CN6A https://www.kaggle.com/datasets/mohankrishnathalla/medical-insurance-cost-prediction
Downloads · 30 days
0
Access
Public
Updated Dec 9, 2025
Repo size
552 KB
Likes
0
Public
Click a slice to open those files.
.ipynb3.3 MB · 85%
From the Hugging Face model README
https://www.youtube.com/watch?v=s9wRsR2CN6A
https://www.kaggle.com/datasets/mohankrishnathalla/medical-insurance-cost-prediction
This project was completed as part of Assignment #2 – Classification, Regression, Clustering, Evaluation.
The goal is to predict patients’ annual medical cost based on their demographic, clinical, utilization and policy characteristics, and then re-frame the problem as a multi-class classification task (low / medium / high cost).
We analyze a health-insurance dataset to predict each patient’s annual medical cost. After performing EDA and feature engineering (including standardized financial/utilization variables and a KMeans-based risk_cluster feature), we compare a baseline Linear Regression model with Random Forest and Gradient Boosting regressors. Random Forest has the lowest MAE, but Gradient Boosting achieves the best RMSE and R² and is therefore selected as our final model. We then convert the problem into a three-class classification task (low, medium and high cost) and train Logistic Regression, Random Forest and Gradient Boosting classifiers. Gradient Boosting again provides the best Accuracy and macro-F1, and is selected as the final winning model.
annual_medical_cost (continuous).age, sex, region, incomeplan_id, network_tier_*, monthly_premium, annual_premium, total_claims_paidrisk_score, claims_count, proc_surgery_count, had_major_procedure, chronic_count, is_high_risk, comorbidity flags (e.g. diabetes, liver_disease)The data is split into 80% train and 20% test and this split is used consistently for all models.
Main questions:
We summarize key numeric variables:
age, bmi, risk_scoreannual_medical_cost, total_claims_paid, claims_countand visualize them with histograms.
Findings:
age and bmi are roughly bell-shaped around their means.annual_medical_cost, total_claims_paid and claims_count are strongly right-skewedFigure 1: Histograms of the six numeric features.

We examine distributions of:
sex, smoker, regionusing relative frequencies and bar plots.
Findings:
sex is roughly balanced.Figure 2: Bar charts for
sex,smokerandregion.
We compute correlations of all numeric features with annual_medical_cost and show the top 10 in a horizontal bar plot.
Findings:
monthly_premium, annual_premiumtotal_claims_paidavg_claim_amount (if present)risk_score and chronic_count, show moderate positive correlation.Figure 3: Top correlations with
annual_medical_cost.
We use boxplots for:
annual_medical_cost by smoker status.annual_medical_cost by is_high_risk_label (derived from is_high_risk).Findings:
Figure 4: Boxplots of annual cost by smoker status.
Figure 5: Boxplots of annual cost by high-risk status.
Model: LinearRegression trained on the original encoded features (X_encoded).
A scatter plot of true vs. predicted cost (not mandatory for the assignment) shows that the linear model captures the global trend, but errors become large for high-cost patients, motivating more advanced models.
To improve model performance and capture non-linear patterns, several engineered features are added.
Examples:
cost_per_claim = total_claims_paid / claims_countrisk_x_premium = risk_score * monthly_premiumhas_chronic = indicator if chronic_count > 0age_group = age binned into categories (e.g. 0–19, 20–39, …)These features aim to compress important relationships such as “cost per utilization” and interactions between risk and premiums.
Key numeric features are standardized using StandardScaler, producing new *_scaled columns, e.g.:
income_scaled, annual_premium_scaled, monthly_premium_scaledtotal_claims_paid_scaled, claims_count_scaled, risk_score_scaledThis helps distance-based and tree-based models and keeps magnitudes comparable.
risk_cluster)We apply KMeans (n_clusters=4) on scaled versions of:
age, risk_score, chronic_count, claims_count, annual_premiumThe resulting cluster assignments are stored in:
risk_cluster (categorical, values 0–3).This groups patients into 4 risk-based clusters with similar profiles and is used later as a categorical feature (one-hot encoded).
(Optional improvement for visualization): add a 2D PCA scatter plot colored by
risk_clusterto show cluster separation.
annual_medical_cost, person_id, helper labels).age_group and risk_cluster).Using the engineered features (X_fe_encoded) with the same 80/20 split, three regression models are trained:
Test performance:
Linear Regression (FE)
Random Forest Regressor (FE)
Gradient Boosting Regressor (FE)
Figure 6: Table (
results_fe) comparing MAE, RMSE and R² for the three improved models.
Although Random Forest achieves the lowest MAE, Gradient Boosting achieves the best RMSE and highest R², which put more weight on large errors.
Since in a medical cost setting large under/over-estimates are particularly problematic, RMSE and R² are chosen as the main criteria.
The model is also exported to a pickle file and uploaded to Hugging Face for reuse.
To convert the task into a 3-class classification problem, the continuous target is binned using train-set quantiles:
annual_medical_cost < 1,4321,432 ≤ cost < 2,951cost ≥ 2,951The thresholds are computed only on the train set and then applied to the test set to avoid data leakage.
Class distributions on both train and test are roughly balanced (about 1/3 in each class), so no special rebalancing techniques are needed. Global metrics such as accuracy and macro F1 are therefore meaningful.
Using the same engineered features, three classification models are trained:
For each model, we compute a classification report (precision, recall, F1) and show a confusion matrix.
| Model | Accuracy | Macro F1 |
|---|---|---|
| Logistic Regression | ~0.82 | ~0.82 |
| Random Forest | ~0.93 | ~0.93 |
| Gradient Boosting | ~0.99 | ~0.99 |
Gradient Boosting Classifier clearly outperforms the others, with very few misclassifications for any of the three cost classes.
Because the classes are balanced and all of them are important, macro F1 is used as the main comparison metric (not only raw accuracy).
Figures 7–9: Confusion matrices for Logistic Regression, Random Forest and Gradient Boosting.
![]()
![]()
Figure 10: Table comparing Accuracy and macro F1 for all three classifiers.
medical_cost_gb_classifier.pkl (and uploaded to Hugging Face).risk_cluster feature) helps non-linear models capture complex patterns.