File size: 4,127 Bytes
b9a7b48
7bb8f67
5038aed
85b8ac2
a27bf21
2834e2f
 
 
 
66c2e0a
2834e2f
 
 
 
 
 
 
 
 
 
66c2e0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
import os
os.system('pip install  joblib')
os.system('pip install scikit-learn')
os.system('pip install xgboost')
os.system('pip install lightgbm')
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")

# Set page layout
st.set_page_config(layout="wide")

# Title
st.markdown("<h1 style='text-align: center;'>πŸ₯ Hospital Readmission Prediction App</h1>", unsafe_allow_html=True)

# Centering the input form
st.markdown("<h3 style='text-align: center;'>πŸ“‹ Enter Patient Information</h3>", unsafe_allow_html=True)

# Create a centered layout
col1, col2, col3 = st.columns([1, 3, 1])
with col2:
    time_in_hospital = st.number_input("Days in Hospital", min_value=1, max_value=30, step=1)
    n_lab_procedures = st.number_input("Number of Lab Procedures", min_value=0, max_value=100, step=1)
    n_procedures = st.number_input("Number of Procedures", min_value=0, max_value=10, step=1)
    n_medications = st.number_input("Number of Medications", min_value=0, max_value=50, step=1)
    age = st.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.selectbox("Glucose Test", ["Yes", "No"])
    A1Ctest = st.selectbox("A1C Test", ["Yes", "No"])
    change = st.selectbox("Change in Medication", ["Yes", "No"])
    diabetes_med = st.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)

    # Prediction button
    if st.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.markdown("<h3 style='text-align: center;'>πŸ“Š Prediction Results</h3>", unsafe_allow_html=True)

        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))