Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ import streamlit as st
|
|
7 |
import pandas as pd
|
8 |
import joblib
|
9 |
import numpy as np
|
|
|
10 |
# Load trained models
|
11 |
rf_model = joblib.load("rf_tuned_model.pkl")
|
12 |
xgb_model = joblib.load("xgb_model.pkl")
|
@@ -17,71 +18,78 @@ label_encoders = joblib.load("label_encoders.pkl")
|
|
17 |
label_encoders_2 = joblib.load("label_encoders_2.pkl")
|
18 |
feature_order = joblib.load("feature_columns.pkl")
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
import pandas as pd
|
8 |
import joblib
|
9 |
import numpy as np
|
10 |
+
|
11 |
# Load trained models
|
12 |
rf_model = joblib.load("rf_tuned_model.pkl")
|
13 |
xgb_model = joblib.load("xgb_model.pkl")
|
|
|
18 |
label_encoders_2 = joblib.load("label_encoders_2.pkl")
|
19 |
feature_order = joblib.load("feature_columns.pkl")
|
20 |
|
21 |
+
# Set page layout
|
22 |
+
st.set_page_config(layout="wide")
|
23 |
+
|
24 |
+
# Title
|
25 |
+
st.markdown("<h1 style='text-align: center;'>π₯ Hospital Readmission Prediction App</h1>", unsafe_allow_html=True)
|
26 |
+
|
27 |
+
# Centering the input form
|
28 |
+
st.markdown("<h3 style='text-align: center;'>π Enter Patient Information</h3>", unsafe_allow_html=True)
|
29 |
+
|
30 |
+
# Create a centered layout
|
31 |
+
col1, col2, col3 = st.columns([1, 3, 1])
|
32 |
+
with col2:
|
33 |
+
time_in_hospital = st.number_input("Days in Hospital", min_value=1, max_value=30, step=1)
|
34 |
+
n_lab_procedures = st.number_input("Number of Lab Procedures", min_value=0, max_value=100, step=1)
|
35 |
+
n_procedures = st.number_input("Number of Procedures", min_value=0, max_value=10, step=1)
|
36 |
+
n_medications = st.number_input("Number of Medications", min_value=0, max_value=50, step=1)
|
37 |
+
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)"])
|
38 |
+
glucose_test = st.selectbox("Glucose Test", ["Yes", "No"])
|
39 |
+
A1Ctest = st.selectbox("A1C Test", ["Yes", "No"])
|
40 |
+
change = st.selectbox("Change in Medication", ["Yes", "No"])
|
41 |
+
diabetes_med = st.selectbox("Diabetes Medication", ["Yes", "No"])
|
42 |
+
|
43 |
+
# Convert user input into a DataFrame
|
44 |
+
user_input_df = pd.DataFrame({
|
45 |
+
"time_in_hospital": [time_in_hospital],
|
46 |
+
"n_lab_procedures": [n_lab_procedures],
|
47 |
+
"n_procedures": [n_procedures],
|
48 |
+
"n_medications": [n_medications],
|
49 |
+
"age": [age],
|
50 |
+
"glucose_test": [glucose_test],
|
51 |
+
"A1Ctest": [A1Ctest],
|
52 |
+
"change": [change],
|
53 |
+
"diabetes_med": [diabetes_med]
|
54 |
+
})
|
55 |
+
|
56 |
+
# Encode categorical features
|
57 |
+
for col in label_encoders:
|
58 |
+
if col in user_input_df:
|
59 |
+
user_input_df[col] = user_input_df[col].apply(lambda x: x if x in label_encoders[col].classes_ else "Unknown")
|
60 |
+
label_encoders[col].classes_ = np.append(label_encoders[col].classes_, "Unknown")
|
61 |
+
user_input_df[col] = label_encoders[col].transform(user_input_df[col])
|
62 |
+
|
63 |
+
# Ensure the feature order matches the training data
|
64 |
+
user_input_df = user_input_df.reindex(columns=feature_order, fill_value=0)
|
65 |
+
|
66 |
+
# Prediction button
|
67 |
+
if st.button("π Predict Readmission"):
|
68 |
+
rf_prediction = rf_model.predict(user_input_df)[0]
|
69 |
+
rf_proba = rf_model.predict_proba(user_input_df)[0][1]
|
70 |
+
|
71 |
+
xgb_prediction = xgb_model.predict(user_input_df)[0]
|
72 |
+
xgb_proba = xgb_model.predict_proba(user_input_df)[0][1]
|
73 |
+
|
74 |
+
lgbm_prediction = lgbm_model.predict(user_input_df)[0]
|
75 |
+
lgbm_proba = lgbm_model.predict_proba(user_input_df)[0][1]
|
76 |
+
|
77 |
+
# Display Results
|
78 |
+
st.markdown("<h3 style='text-align: center;'>π Prediction Results</h3>", unsafe_allow_html=True)
|
79 |
+
|
80 |
+
def format_prediction(pred, proba):
|
81 |
+
if pred == 0:
|
82 |
+
return f"π΄ **Likely to be Readmitted** (Probability: {proba:.2%})"
|
83 |
+
else:
|
84 |
+
return f"π’ **Not Likely to be Readmitted** (Probability: {proba:.2%})"
|
85 |
+
|
86 |
+
st.write("**Random Forest:**", format_prediction(rf_prediction, rf_proba))
|
87 |
+
st.write("**XGBoost:**", format_prediction(xgb_prediction, xgb_proba))
|
88 |
+
st.write("**LightGBM:**", format_prediction(lgbm_prediction, lgbm_proba))
|
89 |
+
|
90 |
+
# Choose final prediction (majority vote)
|
91 |
+
final_prediction = round((rf_prediction + xgb_prediction + lgbm_prediction) / 3)
|
92 |
+
final_proba = (rf_proba + xgb_proba + lgbm_proba) / 3
|
93 |
+
|
94 |
+
st.markdown("### π₯ **Final Prediction:**")
|
95 |
+
st.write(format_prediction(final_prediction, final_proba))
|