A machine learning framework for estimating hospital encounter costs, built to improve healthcare cost transparency for uninsured patients in the U.S.
Uninsured patients in the United States frequently delay or avoid necessary medical care due to uncertainty about costs. Without reliable, upfront pricing information, patients face significant financial risk.
This project develops a predictive model for estimating the cost burden of a patient during a hospital encounter, using realistic synthetic patient data generated by Synthea. By capturing the complex factors that drive healthcare expenses, this framework aims to provide personalized, actionable cost estimates — empowering patients to make more informed healthcare decisions.
Course: DS5003 — University of Virginia, School of Data Science
Team: Claudio Cela, Claire Sullivan, Jack Hays, Sree Prabhav Bandakavi
| File | Description |
|---|---|
DS5003_Groupproject_MLcode_v3_clean.ipynb |
Full modeling pipeline — preprocessing, XGBoost, Random Forest, evaluation, and publication-quality figures |
1kdata_v2.zip |
Synthea 1K synthetic patient dataset (CSV format) |
DS5003_Tableau.twbx |
Tableau workbook for exploratory data analysis |
Presentation_Slides.pptx |
Final project presentation slides |
Data sourced from Synthea — an open-source synthetic patient population simulator produced by The MITRE Corporation.
- 1,000 simulated patients
- Encounter-level observations (each hospital visit = one row)
- 61,459 observations across 240 features after preprocessing
- Features include: age, gender, race, conditions (one-hot encoded), procedures (binned), medications (binned), encounter class, encounter duration, insurance provider, and more
- Target variable:
log(total encounter cost)= base encounter cost + procedure cost
- Merged patient, encounter, condition, procedure, and medication tables on encounter ID
- One-hot encoded all 180 conditions present in the dataset
- Binned procedures into 8 categories and medications into 9 categories
- Log-transformed encounter cost to handle right-skewed distribution
- 70/15/15 train/validation/test split
| Model | Tuning Method | Key Parameters |
|---|---|---|
| XGBoost | Optuna (Bayesian search, 50 trials) | max_depth, learning_rate, subsample, colsample_bytree, reg_alpha, reg_lambda |
| Random Forest | RandomizedSearchCV (20 iterations, 3-fold CV) | n_estimators, max_depth, min_samples_split, min_samples_leaf, max_features |
| Metric | XGBoost | Random Forest |
|---|---|---|
| MAE (log scale) | 0.0926 | 0.0837 |
| RMSE (log scale) | 0.2072 | 0.1962 |
| R² (log scale) | 0.9887 | 0.9899 |
| MAE (dollars) | $735 | $714 |
| RMSE (dollars) | $5,880 | $6,546 |
Both models explain over 98% of the variance in log-transformed encounter cost. Random Forest achieves a lower average error, while XGBoost is more robust to high-cost outliers.
Procedural features consistently outrank diagnosis indicators in both models:
procedure_count— #1 in both modelsproc_obstetrics— #2 in Random ForestENCOUNTER_TIME_MIN— encounter durationENCOUNTERCLASS— inpatient/emergency vs. outpatientAGE,PRIOR_ENCOUNTER_COUNT— moderate signal
Key insight: What is done during an encounter drives cost more than the underlying diagnosis.
pip install pandas numpy scikit-learn xgboost optuna matplotlib shap jupyter# Clone the repo
git clone https://github.com/Claudio-Cela/Predicting-Patient-Encounter-Cost.git
cd Predicting-Patient-Encounter-Cost
# Unzip the data
unzip 1kdata_v2.zip
# Launch Jupyter
jupyter notebook DS5003_Groupproject_MLcode_v3_clean.ipynbNote: The XGBoost model supports GPU acceleration. If running on CPU, set
tree_method='hist'in the XGBoost parameters. Windows users may need to setKMP_DUPLICATE_LIB_OK=TRUEto suppress OMP errors.
- Data loading & preprocessing — merges all Synthea CSVs into a single encounter-level dataframe
- EDA — distribution plots and KDE of log-transformed cost
- XGBoost training — Optuna tuning, evaluation on val/test
- Random Forest training — RandomizedSearchCV tuning, evaluation on val/test
- Publication figures — generates Figures 1–5 for the paper
The final cell generates 5 publication-quality figures saved as PNG:
| Figure | Description |
|---|---|
fig1_model_comparison.png |
MAE, RMSE, R² comparison (log scale) |
fig2_actual_vs_predicted.png |
Actual vs. predicted scatter (99th pct clipped) |
fig3_residual_distributions.png |
Residual histograms (1st–99th pct clipped) |
fig4_feature_importances.png |
Top 15 features — XGBoost gain & RF permutation importance |
fig5_val_vs_test.png |
Validation vs. test generalization |
- Data is synthetic (Synthea) — results may not fully generalize to real-world billing complexity
- High-cost encounter tail is harder to predict accurately (lower dollar-scale R²)
- Procedure and medication binning may obscure granular cost signals
This project is licensed under the MIT License — see LICENSE for details.
- Synthea™ by The MITRE Corporation for the synthetic patient data
- University of Virginia School of Data Science — DS5003