-
Notifications
You must be signed in to change notification settings - Fork 0
/
TF_test.py
289 lines (226 loc) · 11 KB
/
TF_test.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import pandas as pd
import streamlit as st
import plotly.express as px
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split as tts
# Done by: Yasser Shkeir
# Final Version of the Scikit Model app (V2.0)
# Need to improve UI
#Title of web app + wide layout instead of centered
st.set_page_config(page_title='Heart Disease and Stroke App', layout='wide')
# Headers in page
st.write("""
# Heart Disease and Stroke Prediction
# """)
# store a boolean for the heart disease and stroke tests that changes to true only if the user presses the buttons for the tests
if 'bool_heart_disease' not in st.session_state:
st.session_state['bool_heart_disease']=False
if 'bool_stroke' not in st.session_state:
st.session_state['bool_stroke']=False
# Create two Columns, use only one to the left for better aesthetics
col1, col2, col3 = st.columns(3)
with col1:
chosen_test = st.selectbox('Please choose the test type:', ('', 'Heart Disease', 'Stroke'))
#if none option is chosen, remove all content
if chosen_test == '':
st.session_state['bool_heart_disease']=False
st.session_state['bool_stroke']=False
#if stroke option is chosen, remove stroke content and display heart disease content
if chosen_test == 'Heart Disease':
st.session_state['bool_heart_disease']=True
st.session_state['bool_stroke']=False
#if stroke option is chosen, remove heart disease content and display stroke content
if chosen_test == 'Stroke':
st.session_state['bool_stroke']=True
st.session_state['bool_heart_disease']=False
def heart_disease_function():
df = pd.read_csv('heart.csv')
st.subheader('1. Dataset')
# ### Data Cleaning
new_df = df[df['Cholesterol'] != 0]
new_df = new_df[new_df['RestingBP'] != 0]
df['Cholesterol'] = df['Cholesterol'].replace(0, new_df['Cholesterol'].mean())
df['RestingBP'] = df['RestingBP'].replace(0, new_df['RestingBP'].mean())
###
### Data Analysis Section ###
df2 = df
df2['HeartDisease'] = df2['HeartDisease'].replace([0],'No')
df2['HeartDisease'] = df2['HeartDisease'].replace([1],'Yes')
df2['Count'] = 1
df2 = df2.sort_values(
by=['HeartDisease'],
ascending=True).iloc[0:len(df2[df2['HeartDisease']=='No'])*2]
da_col1, da_col2 = st.columns(2)
with da_col1:
st.write('1.1. Dataset Sample')
st.dataframe(df2.drop('Count', axis=1), height=550)
with da_col2:
st.write('1.2. Data Analysis Section')
selection = st.selectbox('Choose your chart selection:', (df2.columns[0:-2].to_list()))
if selection != '':
fig = px.histogram(df2, x=selection, y='Count', title='Count of people with or without a Heart Disease based on {}'.format(selection), color='HeartDisease', barmode='group')
st.plotly_chart(fig)
###
cleanup_vals = {"Sex": {"M": 0, "F": 1},
"ChestPainType": {"TA": 0, "ATA": 1, "NAP": 2, "ASY": 3},
"RestingECG": {"Normal": 0, "ST": 1, "LVH": 2},
"ExerciseAngina" : {"Y": 0, "N": 1},
"ST_Slope": {"Up": 0, "Flat": 1, "Down": 2}}
df['Oldpeak']=df['Oldpeak'].apply(lambda x: x + 2.6)
df['Oldpeak']=df['Oldpeak'].apply(lambda x: x * 10)
df = df.replace(cleanup_vals)
df = df.drop('Count', axis=1)
y=df['HeartDisease']
x=df.drop('HeartDisease',axis=1)
x_train,x_test,y_train,y_test=tts(x,y,test_size=0.3)
# st.markdown('**1.2. Data Splits**')
# st.write('Training Set')
# st.info(x_train.shape)
# st.markdown('**1.3. Variable Details**')
# st.write('Data Columns')
# st.info(list(x.columns))
rf = RandomForestClassifier(n_estimators=250, random_state=0)
rf.fit(x_train, y_train)
st.subheader('2. Model ')
ml_col1, ml_col2, ml_col3, ml_col4 = st.columns(4)
with ml_col1:
st.write('Length of Training Dataset')
st.info(len(x_train))
with ml_col2:
st.write('Length of Testing Dataset')
st.info(len(x_test))
with ml_col3:
y_pred = rf.predict(x_test)
st.write('Accuracy Score:')
st.info(accuracy_score(y_test, y_pred))
### INPUT DATA SECTION ###
st.subheader('3. Data Input Prediction ')
# Create 3 columns for the inputs to be aligned
with st.form(key='input form'):
inp_col1, inp_col2, inp_col3 = st.columns(3)
with inp_col1:
test_age=st.text_input('Age:', max_chars=3, value=0)
test_RBP=st.text_input('Resting Blood Pressure:', max_chars=3, value=0)
test_Cholesterol=st.text_input('Cholesterol:', max_chars=3, value=0)
test_MHR=st.text_input('Maximum Heart Rate:', max_chars=3, value=0)
with inp_col2:
test_CPT=st.select_slider('Chest Pain Type:',options=['TA', 'ATA', 'NAP', 'ASY'])
test_RECG=st.select_slider('Resting Electrocardiogram:', options=['Normal', 'ST', 'LVH'])
test_STS=st.select_slider('ST_Slope:', options=['Up', 'Flat', 'Down'])
test_OPk=st.slider('Old Peak: ', -4.0, 7.0, 0.0, 0.1)
with inp_col3:
test_FBS=st.radio('Fasting Blood Sugar:', options=[0,1])
test_sex=st.radio('Sex:',options=['M','F'])
test_ExA=st.radio('Exercise Angina:', options=['N','Y'])
if st.form_submit_button('Confirm'):
predict_data={'Age':[test_age],
'Sex':[test_sex],
'ChestPainType':[test_CPT],
'RestingBP':[test_RBP],
'Cholesterol':[test_Cholesterol],
'FastingBS':[test_FBS],
'RestingECG':[test_RECG],
'MaxHR':[test_MHR],
'ExerciseAngina':[test_ExA],
'Oldpeak':[test_OPk],
'ST_Slope':[test_STS]}
predict_df= pd.DataFrame(predict_data)
# st.write(predict_df)
predict_df['Oldpeak']=predict_df['Oldpeak'].apply(lambda x: x + 2.6)
predict_df['Oldpeak']=predict_df['Oldpeak'].apply(lambda x: x * 10)
predict_df = predict_df.replace(cleanup_vals)
msg = 'There is a % {} patient has a heart disease'.format(round(rf.predict_proba(predict_df)[0][1] *100, 2))
st.error(msg)
def stroke_function():
df = pd.read_csv('stroke-data.csv')
st.subheader('1. Dataset')
#### Data Cleaning
del df["id"]
df = df.dropna()
df = df[df['age'] >= 2]
df = df[df['gender'] != 'Other']
###
df2 = df
df2['stroke'] = df2['stroke'].replace([0],'No')
df2['stroke'] = df2['stroke'].replace([1],'Yes')
df2['Count'] = 1
df2 = df2.sort_values(
by=['stroke'],
ascending=False).iloc[0:len(df2[df2['stroke']=='Yes'])*2]
da_col1, da_col2 = st.columns(2)
with da_col1:
st.write('1.1. Dataset Sample')
st.dataframe(df2.drop('Count', axis=1), height=550)
with da_col2:
st.write('1.2. Data Analysis Section')
selection = st.selectbox('Choose your chart selection:', (df2.columns[0:-2].to_list()))
if selection != '':
fig = px.histogram(df2, x=selection, y='Count', title='Count of people with or without a Stroke based on {}'.format(selection), color='stroke', barmode='group')
st.plotly_chart(fig)
cleanup_vals = {"gender": {"Male": 0, "Female": 1},
"ever_married": {"No": 0, "Yes": 1},
"work_type": {"Never_worked": 0, "children": 1, "Self-employed": 2, "Private": 3, "Govt_job": 4},
"Residence_type" : {"Urban": 0, "Rural": 1},
"smoking_status": {"Unknown": 0, "never smoked": 1, "formerly smoked": 2, "smokes": 3}}
# df['avg_glucose_level']=df['avg_glucose_level'].apply(lambda x: x * 100)
# predict_df['avg_glucose_level'].iloc[0]=predict_df['avg_glucose_level'].iloc[0]*100
# df['bmi']=df['bmi'].apply(lambda x: x * 10)
# predict_df['bmi']=predict_df['bmi'].apply(lambda x: x * 10)
df = df.replace(cleanup_vals)
df = df.drop(columns=['Count'], axis=1)
y=df['stroke']
x=df.drop('stroke',axis=1)
x_train,x_test,y_train,y_test=tts(x,y,test_size=0.25)
rf = RandomForestClassifier(n_estimators=25, random_state=0)
rf.fit(x_train, y_train)
st.subheader('2. Model ')
ml_col1, ml_col2, ml_col3, ml_col4 = st.columns(4)
with ml_col1:
st.write('Length of Training Dataset')
st.info(len(x_train))
with ml_col2:
st.write('Length of Testing Dataset')
st.info(len(x_test))
with ml_col3:
y_pred = rf.predict(x_test)
st.write('Accuracy Score:')
st.info(accuracy_score(y_test, y_pred))
### INPUT DATA SECTION ###
st.subheader('3. Data Input Prediction ')
# Create 3 columns for the inputs to be aligned
with st.form(key='input form'):
inp_col1, inp_col2, inp_col3 = st.columns(3)
with inp_col1:
test_age=st.text_input('Age:', max_chars=3, value=0)
test_Avg_GlucLvl=st.text_input('Average Glucose Level:', max_chars=6, value=0)
test_BMI=st.text_input('BMI:', max_chars=4, value=0)
with inp_col2:
test_Hypertension=st.select_slider('Hypertension: (0 for no hypertension, 1 for hypertension)',options=[0,1])
test_Heart_Disease=st.select_slider('Heart Disease: (0 for no Heart Disease, 1 for Heart Disease)',options=[0,1])
test_Work_Type=st.select_slider('Work Type:', options=['Never_worked','children', 'Self-employed', 'Private', 'Govt_job'])
test_Smoking_Status=st.select_slider('Smoking Status:', options=['Unknown', 'never smoked', 'formerly smoked', 'smokes'])
with inp_col3:
test_sex=st.radio('Sex:',options=['Male','Female'])
test_Residence_Type=st.radio('Residence Type:', options=['Urban', 'Rural'])
test_Ever_Married=st.radio('Ever Married:', options=['No','Yes'])
if st.form_submit_button('Confirm'):
predict_data={'age':[test_age],
'gender':[test_sex],
'hypertension':[test_Hypertension],
'heart_disease':[test_Heart_Disease],
'ever_married':[test_Ever_Married],
'work_type':[test_Work_Type],
'Residence_type':[test_Residence_Type],
'avg_glucose_level':[test_Avg_GlucLvl],
'bmi':[test_BMI],
'smoking_status':[test_Smoking_Status]}
predict_df= pd.DataFrame(predict_data)
predict_df = predict_df.replace(cleanup_vals)
msg = 'There is a % {} chance patient will have/had a stroke'.format(round(rf.predict_proba(predict_df)[0][0] *100, 2))
st.error(msg)
# If the button is pressed, the boolean stored in the session state changes to true, which in this case we call the function
if st.session_state['bool_heart_disease']:
heart_disease_function()
if st.session_state['bool_stroke']:
stroke_function()