vikasdeep commited on
Commit
66c2e0a
Β·
verified Β·
1 Parent(s): a27bf21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -68
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
- st.title("πŸ₯ Hospital Readmission Prediction App")
21
-
22
- st.sidebar.header("πŸ“‹ Enter Patient Information")
23
-
24
- # User input fields
25
- time_in_hospital = st.sidebar.number_input("Days in Hospital", min_value=1, max_value=30, step=1)
26
- n_lab_procedures = st.sidebar.number_input("Number of Lab Procedures", min_value=0, max_value=100, step=1)
27
- n_procedures = st.sidebar.number_input("Number of Procedures", min_value=0, max_value=10, step=1)
28
- n_medications = st.sidebar.number_input("Number of Medications", min_value=0, max_value=50, step=1)
29
- 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)"])
30
- glucose_test = st.sidebar.selectbox("Glucose Test", ["Yes", "No"])
31
- A1Ctest = st.sidebar.selectbox("A1C Test", ["Yes", "No"])
32
- change = st.sidebar.selectbox("Change in Medication", ["Yes", "No"])
33
- diabetes_med = st.sidebar.selectbox("Diabetes Medication", ["Yes", "No"])
34
-
35
- # Convert user input into a DataFrame
36
- user_input_df = pd.DataFrame({
37
- "time_in_hospital": [time_in_hospital],
38
- "n_lab_procedures": [n_lab_procedures],
39
- "n_procedures": [n_procedures],
40
- "n_medications": [n_medications],
41
- "age": [age],
42
- "glucose_test": [glucose_test],
43
- "A1Ctest": [A1Ctest],
44
- "change": [change],
45
- "diabetes_med": [diabetes_med]
46
- })
47
-
48
- # Encode categorical features
49
- for col in label_encoders:
50
- if col in user_input_df:
51
- user_input_df[col] = user_input_df[col].apply(lambda x: x if x in label_encoders[col].classes_ else "Unknown")
52
- label_encoders[col].classes_ = np.append(label_encoders[col].classes_, "Unknown")
53
- user_input_df[col] = label_encoders[col].transform(user_input_df[col])
54
-
55
- # Ensure the feature order matches the training data
56
- user_input_df = user_input_df.reindex(columns=feature_order, fill_value=0)
57
-
58
- # Predict using the models
59
- if st.sidebar.button("πŸ” Predict Readmission"):
60
- rf_prediction = rf_model.predict(user_input_df)[0]
61
- rf_proba = rf_model.predict_proba(user_input_df)[0][1]
62
-
63
- xgb_prediction = xgb_model.predict(user_input_df)[0]
64
- xgb_proba = xgb_model.predict_proba(user_input_df)[0][1]
65
-
66
- lgbm_prediction = lgbm_model.predict(user_input_df)[0]
67
- lgbm_proba = lgbm_model.predict_proba(user_input_df)[0][1]
68
-
69
- # Display Results
70
- st.subheader("πŸ“Š Prediction Results")
71
-
72
- def format_prediction(pred, proba):
73
- if pred == 0:
74
- return f"πŸ”΄ **Likely to be Readmitted** (Probability: {proba:.2%})"
75
- else:
76
- return f"🟒 **Not Likely to be Readmitted** (Probability: {proba:.2%})"
77
-
78
- st.write("**Random Forest:**", format_prediction(rf_prediction, rf_proba))
79
- st.write("**XGBoost:**", format_prediction(xgb_prediction, xgb_proba))
80
- st.write("**LightGBM:**", format_prediction(lgbm_prediction, lgbm_proba))
81
-
82
- # Choose final prediction (majority vote)
83
- final_prediction = round((rf_prediction + xgb_prediction + lgbm_prediction) / 3)
84
- final_proba = (rf_proba + xgb_proba + lgbm_proba) / 3
85
-
86
- st.markdown("### πŸ₯ **Final Prediction:**")
87
- st.write(format_prediction(final_prediction, final_proba))
 
 
 
 
 
 
 
 
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))