File size: 3,619 Bytes
a865f96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import streamlit as st
import pandas as pd
import joblib
import numpy as np

# Load trained models
rf_model = joblib.load("rf_tuned_model.pkl")
xgb_model = joblib.load("xgb_model.pkl")
lgbm_model = joblib.load("lgbm_model.pkl")

# Load encoders and feature order
label_encoders = joblib.load("label_encoders.pkl")
label_encoders_2 = joblib.load("label_encoders_2.pkl")
feature_order = joblib.load("feature_columns.pkl")

st.title("πŸ₯ Hospital Readmission Prediction App")

st.sidebar.header("πŸ“‹ Enter Patient Information")

# User input fields
time_in_hospital = st.sidebar.number_input("Days in Hospital", min_value=1, max_value=30, step=1)
n_lab_procedures = st.sidebar.number_input("Number of Lab Procedures", min_value=0, max_value=100, step=1)
n_procedures = st.sidebar.number_input("Number of Procedures", min_value=0, max_value=10, step=1)
n_medications = st.sidebar.number_input("Number of Medications", min_value=0, max_value=50, step=1)
age = st.sidebar.selectbox("Age Group", ["[0-10)", "[10-20)", "[20-30)", "[30-40)", "[40-50)", "[50-60)", "[60-70)", "[70-80)", "[80-90)", "[90-100)"])
glucose_test = st.sidebar.selectbox("Glucose Test", ["Yes", "No"])
A1Ctest = st.sidebar.selectbox("A1C Test", ["Yes", "No"])
change = st.sidebar.selectbox("Change in Medication", ["Yes", "No"])
diabetes_med = st.sidebar.selectbox("Diabetes Medication", ["Yes", "No"])

# Convert user input into a DataFrame
user_input_df = pd.DataFrame({
    "time_in_hospital": [time_in_hospital],
    "n_lab_procedures": [n_lab_procedures],
    "n_procedures": [n_procedures],
    "n_medications": [n_medications],
    "age": [age],
    "glucose_test": [glucose_test],
    "A1Ctest": [A1Ctest],
    "change": [change],
    "diabetes_med": [diabetes_med]
})

# Encode categorical features
for col in label_encoders:
    if col in user_input_df:
        user_input_df[col] = user_input_df[col].apply(lambda x: x if x in label_encoders[col].classes_ else "Unknown")
        label_encoders[col].classes_ = np.append(label_encoders[col].classes_, "Unknown")
        user_input_df[col] = label_encoders[col].transform(user_input_df[col])

# Ensure the feature order matches the training data
user_input_df = user_input_df.reindex(columns=feature_order, fill_value=0)

# Predict using the models
if st.sidebar.button("πŸ” Predict Readmission"):
    rf_prediction = rf_model.predict(user_input_df)[0]
    rf_proba = rf_model.predict_proba(user_input_df)[0][1]

    xgb_prediction = xgb_model.predict(user_input_df)[0]
    xgb_proba = xgb_model.predict_proba(user_input_df)[0][1]

    lgbm_prediction = lgbm_model.predict(user_input_df)[0]
    lgbm_proba = lgbm_model.predict_proba(user_input_df)[0][1]

    # Display Results
    st.subheader("πŸ“Š Prediction Results")

    def format_prediction(pred, proba):
        if pred == 0:
            return f"πŸ”΄ **Likely to be Readmitted** (Probability: {proba:.2%})"
        else:
            return f"🟒 **Not Likely to be Readmitted** (Probability: {proba:.2%})"

    st.write("**Random Forest:**", format_prediction(rf_prediction, rf_proba))
    st.write("**XGBoost:**", format_prediction(xgb_prediction, xgb_proba))
    st.write("**LightGBM:**", format_prediction(lgbm_prediction, lgbm_proba))
    
    # Choose final prediction (majority vote)
    final_prediction = round((rf_prediction + xgb_prediction + lgbm_prediction) / 3)
    final_proba = (rf_proba + xgb_proba + lgbm_proba) / 3

    st.markdown("### πŸ₯ **Final Prediction:**")
    st.write(format_prediction(final_prediction, final_proba))