File size: 1,764 Bytes
e911c73
 
 
 
 
351e2dd
74640ac
351e2dd
 
 
 
 
 
 
 
 
 
74640ac
351e2dd
 
 
 
 
 
 
 
 
 
 
74640ac
351e2dd
 
 
74640ac
351e2dd
 
 
e911c73
 
 
 
 
4bfee26
 
 
e911c73
 
4bfee26
 
 
e911c73
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import pandas as pd
import shap

def preprocess(patient, schema):
    df = pd.DataFrame([patient])

    # Binary yes/no mappings
    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 mappings
    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)

    # One-hot encoding
    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)

    # Align to model schema
    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