|
import pandas as pd |
|
import shap |
|
|
|
def preprocess(patient, schema): |
|
df = pd.DataFrame([patient]) |
|
|
|
|
|
binary_fields = [ |
|
"FollowUp_Scheduled", "Language_Barrier", "Housing_Instability", |
|
"Caregiver_Support", "Food_Insecurity", "Transportation_Access", |
|
"Lives_Alone", "FollowUp_Within_7_Days", "Primary_Care_Established", |
|
"Recent_Surgery" |
|
] |
|
for field in binary_fields: |
|
if field in df.columns: |
|
df[field] = df[field].map({"Yes": 1, "No": 0}) |
|
|
|
|
|
ordinal_maps = { |
|
"Medication_Adherence": {"Low": 0, "Medium": 1, "High": 2}, |
|
"Health_Literacy": {"Low": 0, "Medium": 1, "High": 2}, |
|
"Gender": {"M": 1, "F": 0} |
|
} |
|
for col, mapping in ordinal_maps.items(): |
|
if col in df.columns: |
|
df[col] = df[col].map(mapping) |
|
|
|
df.drop(columns=["Chronic_Condition_List"], errors="ignore", inplace=True) |
|
|
|
|
|
onehot_cols = ["Primary_Diagnosis", "Discharge_Disposition", "Insurance_Type"] |
|
df = pd.get_dummies(df, columns=[col for col in onehot_cols if col in df.columns], drop_first=True) |
|
|
|
|
|
for col in schema: |
|
if col not in df.columns: |
|
df[col] = 0 |
|
df = df[schema] |
|
|
|
return df |
|
|
|
def predict_readmission(model, patient): |
|
schema = model.named_steps["preprocessor"].get_feature_names_out() |
|
X = preprocess(patient, schema) |
|
return model.named_steps["model"].predict(X)[0] |
|
|
|
def explain_with_shap(model, patient): |
|
schema = model.named_steps["preprocessor"].get_feature_names_out() |
|
X = preprocess(patient, schema) |
|
explainer = shap.Explainer(model.named_steps["model"]) |
|
shap_values = explainer(X) |
|
return shap_values, X |
|
|