-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit.py
168 lines (147 loc) · 5.87 KB
/
streamlit.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# streamlit test, version1
import pandas as pd
import numpy as np
from pickle import load
import streamlit as st
from PIL import Image
import numpy as np
import pandas as pd
from xgboost import XGBClassifier
from matplotlib import pyplot as plt
from numpy import sqrt
from numpy import argmax
import joblib
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import roc_curve
from sklearn.metrics import classification_report
from sklearn.metrics import roc_auc_score
import sklearn.metrics as metrics
from sklearn.metrics import plot_roc_curve
from sklearn.calibration import CalibratedClassifierCV
from sklearn.metrics import confusion_matrix
from sklearn.utils import resample
from pickle import load
import shap
# loading in the model to predict on the data
## xgb models
scaler, model, expl = joblib.load('sme.pkl')
## im non
im_sur = Image.open('alive.jpg')
## im pro
im_die = Image.open('die.jpg')
# custom def : shap
def shap(
sample_case,
scaler = scaler,
explainer = expl
):
# standardization columns
std_cols=['age','duration','hptd']
# feature extraction from input data UA
sample_case_features = sample_case.loc[:,['duration', 'vac_janssen', 'Metformin', 'Pioglitazone', 'hptd', 'hptv', 'Sitagliptin', 'male', 'age', 'recoverd_N']]
sample_case_features[std_cols] = scaler.transform(sample_case_features[std_cols])
expl_test = explainer.shap_values(sample_case_features.iloc[0])
shap_bar = pd.DataFrame(
{'shap_value(probability)' : expl_test}, index = ['duration', 'vac_janssen', 'Metformin', 'Pioglitazone', 'hptd', 'hptv', 'Sitagliptin', 'male', 'age', 'recoverd_N'])
# clrs = ['blue' if x < 0 else 'red' for x in shap_var['shap']]
return shap_bar
# custom def : standardization and prediction
def model_prediction(
sample_case,
scaler = scaler,
model = model
):
"""
'recoverd_N'
'age'
'male'
'duration'
'vac_janssen'
'hptv'
'Metformin'
'hptd'
'Pioglitazone'
'Sitagliptin'
"""
# standardization columns
std_cols=['age','duration','hptd']
# feature extraction from input data UA
sample_case_features = sample_case.loc[:,['duration', 'vac_janssen', 'Metformin', 'Pioglitazone', 'hptd', 'hptv', 'Sitagliptin', 'male', 'age', 'recoverd_N']]
sample_case_features[std_cols] = scaler.transform(sample_case_features[std_cols])
# predict probability by model
prob = model.predict_proba(sample_case_features)[:,1]
return np.float64(prob)
def data_mapping(df):
"""this function preprocess the user input
return type: pandas dataframe
"""
df.male = df.male.map({'female':0, 'male':1})
df.recoverd_N = df.recoverd_N.map({'Yes':0, 'No':1})
df.vac_janssen = df.vac_janssen.map({'No':0, 'Yes':1})
df.hptv = df.hptv.map({'No':0, 'Yes':1})
df.Metformin = df.Metformin.map({'No':0, 'Yes':1})
df.Pioglitazone = df.Pioglitazone.map({'No':0, 'Yes':1})
df.Sitagliptin = df.Sitagliptin.map({'No':0, 'Yes':1})
#df.he_ubld = df.he_ubld.map({"-":0, "+/-":1, "1+":2, "2+":3, "3+":4, "4+":5})
#df.he_upro = df.he_upro.map({"-":0, "+/-":1, "1+":2, "2+":3, "3+":4, "4+":5})
#df.he_uglu = df.he_uglu.map({"-":0, "+/-":1, "1+":2, "2+":3, "3+":4, "4+":5})
return df
def main():
# giving the webpage a title
st.title("Will you be alive?")
# here we define some of the front end elements of the web page like
# the font and background color, the padding and the text to be displayed
html_temp = """
<div style ="background-color:grey;padding:13px">
<h1 style ="color:black;text-align:center;">Prediction Death in diabetes ML App </h1>
</div>
"""
# this line allows us to display the front end aspects we have
# defined in the above code
st.markdown(html_temp, unsafe_allow_html = True)
# the following lines create text boxes in which the user can enter
# the data required to make the prediction
age = st.sidebar.slider("age", 0, 100, 1)
male = st.sidebar.selectbox("sex", ("female", "male"))
vac_janssen = st.sidebar.selectbox("Have you vaccinated Janssen?", ("Yes", "No"))
recoverd_N = st.sidebar.selectbox("have you recovered?", ("Yes", "No"))
duration = st.sidebar.slider("How long have you been vaccinated?", 0, 365, 1)
hptv = st.sidebar.selectbox("Have you ever been to a hospital?", ("Yes", "No"))
hptd = st.sidebar.slider("How long have you been in the hospital? (day)", 0, 110, 1)
Metformin = st.sidebar.selectbox("Do you take metformin?", ("Yes", "No"))
Pioglitazone = st.sidebar.selectbox("Do you take Pioglitazone?", ("Yes", "No"))
Sitagliptin = st.sidebar.selectbox("Do you take Sitagliptin?", ("Yes", "No"))
features = {
"duration" : duration,
"vac_janssen" : vac_janssen,
"Metformin" : Metformin,
"Pioglitazone": Pioglitazone,
"hptd" : hptd,
"hptv" : hptv,
"Sitagliptin" : Sitagliptin,
"male" : male,
"age" : age,
"recoverd_N" : recoverd_N
}
sample_case = pd.DataFrame(features, index=[0])
result = ""
prob = 0.0
# the below line ensures that when the button called 'Predict' is clicked,
# the prediction function defined above is called to make the prediction
# and store it in the variable result
if st.button("Predict"):
sample_case_map = data_mapping(sample_case)
result = model_prediction(sample_case_map)
prob = result
shap_bar = shap(sample_case_map)
st.success('probability : {}'.format(result))
if prob < 0.432 :
st.success("threshold : 0.432")
st.image(im_sur)
st.bar_chart(data=shap_bar)
else :
st.success("threshold : 0.432")
st.image(im_die)
st.bar_chart(data=shap_bar)
if __name__=='__main__':
main()