Downloads · 30 days
0
YXNXNXN/Yotam_Axen_Models
Yotam_Axen_Models is a machine learning model from YXNXNXN. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
Presentation Video - https://youtu.be/gymV9vCqlwo
Downloads · 30 days
0
Access
Public
Updated Dec 11, 2025
Repo size
5.8 MB
Likes
0
Public
Click a slice to open those files.
.pkl2.6 MB · 43%
From the Hugging Face model README
Presentation Video - https://youtu.be/gymV9vCqlwo
This project predicts hospital length of stay for diabetic patients using demographics, diagnoses, procedures, medications, feature engineering, clustering, regression models, and classification models.
It is based on the Diabetes 130-US Hospitals (1999–2008) dataset from Kaggle.
The goal of this project is to understand which patient and hospital features are associated with how long a patient stays in hospital, and to build models that can predict length of stay early in the admission process.
The task is approached in two stages:
time_in_hospital).Special focus is placed on the long-stay group, which is clinically important but under-represented.
Regression target: time_in_hospital (integer days)
Classification target: stay_length_class (0 = short, 1 = medium, 2 = long)
Source: Diabetes 130-US hospitals for years 1999–2008 (Kaggle)
Size: ~100,000 encounters, dozens of features
Includes:
race, gender, ageadmission_type_id, admission_source_id, discharge_disposition_idtime_in_hospital, num_lab_procedures, num_procedures, number_diagnosesNo, Steady, Up, Down)readmittedRemoved upfront:
encounter_id, patient_nbrweight, payer_code, medical_specialty, max_glu_serum, A1Cresult"?" with NaN in categorical columns.df.describe() and df.info().
time_in_hospital ranges from 1–14 days, skewed toward shorter stays → a natural motivation for short/medium/long buckets.
time_in_hospital, num_lab_procedures, num_procedures, and num_medications by gender.race and age split by gender.
admission_type_iddischarge_disposition_idtime_in_hospitalnumber_diagnoses
time_in_hospital correlates most with:
num_medicationsnum_lab_proceduresnumber_diagnoses

To capture clinical and operational complexity, several features were engineered:
Age Midpoint
age_mid: converts age ranges (e.g. [40-50)) into numeric midpoints.Visit Intensity
total_visits = number_outpatient + number_emergency + number_inpatient.Medication Dynamics
med_changes = count of drugs with Up or Down.num_meds_active = count of drugs not equal to No.Readmission & Admission
readmit_encoded = {NO: 0, >30: 1, <30: 2}.is_emergency = 1 if admission_type_id == 1, else 0.Intensity Ratios
(leakage-safe)
proc_med_ratio = num_procedures / (num_medications + 1)lab_intensity = num_lab_procedures / (number_diagnoses + 1)Diagnosis Grouping
diag1_group, diag2_group, diag3_group = first character of each diagnosis code.Low-Variance Removal
handle_unknown='ignore').X_train_scaled_dfX_test_scaled_dfTo incorporate latent structure, I introduced clustering on the scaled feature space.
I defined a helper function optimal_k_large() that:
This procedure was also run once on the full dataset and consistently suggested k = 3 clusters.
Silhouette analysis (k = 2…10):

Elbow method (k = 1…10):

Both curves point to a small optimal k, with a preference around k = 3, which was then used for K-Means.
KMeans(n_clusters=3) on X_train_scaled_df.cluster_id – cluster assignmentcluster_distance – distance to nearest cluster center
cluster_id.
These cluster features later appear as non-trivial predictors in feature importance plots.
Using engineered + clustered features (X_train_scaled_df):
time_in_hospital, yielding an unrealistic R² ≈ 0.999.lab_intensity, performance dropped to a plausible level.| Model | MAE | RMSE | R² |
|---|---|---|---|
| Gradient Boosting | ~1.64 | ~2.22 | ~0.43 |
| Linear Regression | ~1.70 | ~2.28 | ~0.40 |
| Random Forest | ~1.71 | ~2.31 | ~0.39 |
| Baseline Linear Reg | ~1.90 | ~2.52 | ~0.27 |
Winner: GradientBoostingRegressor

num_medications, num_lab_procedures, discharge_disposition_iddiag_1_414, diag_1_V57)proc_med_ratio, age_mid, number_diagnosescluster_distance)best_model_regression.pklTo make predictions more actionable, time_in_hospital was converted into 3 classes:
[0, 3, 7, 14]0 → short stay (1–3 days)1 → medium stay (4–7 days)2 → long stay (8–14 days)Imbalance: long stays are under-represented (~15%) but still numerous → accuracy can be misleading.
Metric choice: macro F1, with particular attention to recall/F1 of class 2 (long stay), since missing long stays is clinically costly.
All trained on X_train_scaled_df / X_test_scaled_df with target stay_length_class:
class_weight='balanced'n_estimators=200, class_weight='balanced'| Model | Macro F1 | Notes |
|---|---|---|
| HistGradientBoosting | ~0.58 | Best overall & best for class 2 |
| Logistic Regression | ~0.53 | Strong baseline |
| Random Forest Classifier | ~0.51 | Good but weaker on long stays |
HistGradientBoostingClassifier
num_medications, num_lab_procedures, discharge_disposition_idproc_med_ratio, admission_type_idcluster_distance, number_diagnoses, age_mid, med_changesbest_model_classification.pklData Leakage:
A single engineered feature that used the target in its formula inflated R² to 0.999. Detecting and removing it was a critical lesson in validating feature design.
Imbalanced Classes:
Accuracy alone looked “good” while long stays were being predicted poorly. Macro F1 and per-class metrics told a much truer story.
Memory Limits in Colab:
One-hot encoding, scaling, clustering, and tuning on a large dataset is RAM-heavy. I had to:
del ...) and call gc.collect().Clustering as Features:
Unsupervised clusters (cluster_id, cluster_distance) carried useful signal and showed up in feature importance, highlighting the value of mixing supervised and unsupervised steps.
Regression vs Classification Trade-off:
Regression provides fine-grained estimates but is noisier; classification yields clearer, more actionable categories (short/medium/long) at the cost of granularity.