Nahiyan14 commited on
Commit
a636864
·
verified ·
1 Parent(s): 8e7ca1d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -0
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import joblib
4
+ import shap
5
+ import matplotlib.pyplot as plt
6
+
7
+ # Load the model
8
+ @st.cache_resource
9
+ def load_model():
10
+ return joblib.load('best_model.pkl')
11
+
12
+ model = load_model()
13
+
14
+ # Streamlit app title and description
15
+ st.title("Retention Probability Predictor")
16
+ st.markdown("""
17
+ Predict the probability of retention based on patient and treatment details.
18
+ Provide the required inputs below to see the prediction and feature contributions.
19
+ """)
20
+
21
+ # Sidebar for inputs
22
+ st.sidebar.header("Input Features")
23
+
24
+ # Input fields for features
25
+ st.sidebar.subheader("Episode Details")
26
+ previous_bup_episodes = st.sidebar.number_input(
27
+ "Number of Previous Buprenorphine Episodes:", min_value=0, value=20, step=1
28
+ )
29
+ previous_failed_bup_episodes = st.sidebar.number_input(
30
+ "Number of Previous Failed Buprenorphine Episodes:", min_value=0, value=20, step=1
31
+ )
32
+
33
+ st.sidebar.subheader("Days Details")
34
+ days_covered = st.sidebar.number_input(
35
+ "Days Covered in Current Episode:", min_value=0, value=30, step=1
36
+ )
37
+ days_since_start = st.sidebar.number_input(
38
+ "Days Since Start of the Episode:", min_value=1, value=90, step=1
39
+ )
40
+
41
+ st.sidebar.subheader("Other Features")
42
+ prior_BUP_days_supply = st.sidebar.number_input(
43
+ "Prior BUP Days Supply:", min_value=0, value=30, step=1
44
+ )
45
+ YearOfInitiation = st.sidebar.number_input(
46
+ "Year of Initiation:", min_value=1900, max_value=2100, value=2023, step=1
47
+ )
48
+ Previous_Meth_Episodes = st.sidebar.radio(
49
+ "Previous Methadone Episodes (Yes/No):",
50
+ options=[0, 1], # Binary options
51
+ format_func=lambda x: "Yes" if x == 1 else "No" # Display "Yes" or "No" instead of 0/1
52
+ )
53
+
54
+ # Calculate derived features
55
+ if st.sidebar.button("Predict Retention Probability"):
56
+ # Calculate Failed Buprenorphine Rate
57
+ if previous_bup_episodes > 0:
58
+ Failed_Bup_Rate = previous_failed_bup_episodes / previous_bup_episodes
59
+ else:
60
+ Failed_Bup_Rate = 0.0 # No prior episodes, so no failures
61
+
62
+ # Calculate Proportion of Days Covered
63
+ if days_since_start > 0:
64
+ proportionofDaysCovered = days_covered / days_since_start
65
+ else:
66
+ proportionofDaysCovered = 0.0 # Avoid division by zero
67
+
68
+ # Prepare input data for prediction
69
+ input_data = pd.DataFrame({
70
+ 'proportionofDaysCovered': [proportionofDaysCovered],
71
+ 'Failed_Bup_Rate': [Failed_Bup_Rate],
72
+ 'prior_BUP_days_supply': [prior_BUP_days_supply],
73
+ 'YearOfInitiation': [YearOfInitiation],
74
+ 'Previous_Meth_Episodes': [Previous_Meth_Episodes]
75
+ })
76
+
77
+ # Make prediction
78
+ retention_probability = model.predict_proba(input_data)[:, 1][0]
79
+
80
+ # Display the prediction
81
+ st.subheader("Prediction Result")
82
+ st.success(f"The predicted retention probability is: **{retention_probability:.2f}**")
83
+
84
+ # Display calculated features
85
+ st.subheader("Calculated Features")
86
+ st.write(f"**Failed Buprenorphine Rate:** {Failed_Bup_Rate:.2%}")
87
+ st.write(f"**Proportion of Days Covered:** {proportionofDaysCovered:.2%}")
88
+
89
+ # Explain model prediction using SHAP
90
+ st.subheader("Feature Contribution to Prediction")
91
+ explainer = shap.Explainer(model, input_data) # Create a SHAP explainer
92
+ shap_values = explainer(input_data)
93
+
94
+ # Plot feature importance
95
+ shap.force_plot(
96
+ explainer.expected_value[1],
97
+ shap_values.values[0],
98
+ input_data.iloc[0],
99
+ matplotlib=True,
100
+ show=False
101
+ )
102
+ st.pyplot(plt.gcf()) # Display SHAP force plot
103
+
104
+ st.write("The above visualization shows how each feature contributes to the prediction.")
105
+
106
+ # Feature importance bar chart
107
+ st.subheader("Feature Importance (Bar Chart)")
108
+ shap.summary_plot(shap_values, input_data, plot_type="bar", show=False)
109
+ st.pyplot(plt.gcf())