-
Notifications
You must be signed in to change notification settings - Fork 2
/
diabete.py
318 lines (251 loc) · 11.5 KB
/
diabete.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import pandas as pd
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from PIL import Image
import streamlit as st
#Sidebar Configuration
st.markdown(
"""
<style>
.sidebar .sidebar-content {
background-image: linear-gradient(#99ffcc,#99ffcc);
color: purple;
}
</style>
""",
unsafe_allow_html=True,
)
# create a title and a sub-title
st.write("""
# Diabetes Detection
""")
image=Image.open('diabetes.jpg')
st.image(image,caption='Machine Learning Project by Hack Inversion',use_column_width=True)
st.subheader("Gender:M/F/other")
text1=st.text_input("Enter your Gender: M/F/Others")
#if not option1:
if not text1:
st.warning('Please enter your Gender')
st.stop()
if text1=='M' or text1=='F' or text1=='Others':
df=pd.read_csv('diabetes.csv')
st.subheader('Dataset Used: ')
# show data as table
if text1=='F':
st.dataframe(df)
print('')
st.write(df.describe())
# visualize data
st.header('Display Graphs')
select1=st.selectbox('Select the Input variable',('Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age'))
radio1=st.radio('',('Bar Chart','Line Chart'))
if radio1=='Bar Chart':
if select1=='Pregnancies':
st.bar_chart(df['Pregnancies'])
if select1=='Glucose':
st.bar_chart(df['Glucose'])
if select1=='BloodPressure':
st.bar_chart(df['BloodPressure'])
if select1=='SkinThickness':
st.bar_chart(df['SkinThickness'])
if select1=='Insulin':
st.bar_chart(df['Insulin'])
if select1=='BMI':
st.bar_chart(df['BMI'])
if select1=='DiabetesPedigreeFunction':
st.bar_chart(df['DiabetesPedigreeFunction'])
if select1=='Age':
st.bar_chart(df['Age'])
else:
if select1=='Pregnancies':
st.line_chart(df['Pregnancies'])
if select1=='Glucose':
st.line_chart(df['Glucose'])
if select1=='BloodPressure':
st.bar_chart(df['BloodPressure'])
if select1=='SkinThickness':
st.line_chart(df['SkinThickness'])
if select1=='Insulin':
st.line_chart(df['Insulin'])
if select1=='BMI':
st.line_chart(df['BMI'])
if select1=='DiabetesPedigreeFunction':
st.line_chart(df['DiabetesPedigreeFunction'])
if select1=='Age':
st.line_chart(df['Age'])
check2=st.checkbox("Show Area Chart")
if check2:
select2=st.selectbox('Select Input variable',('Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age'))
if select2=='Pregnancies':
st.area_chart(df['Pregnancies'])
if select2=='Glucose':
st.area_chart(df['Glucose'])
if select2=='BloodPressure':
st.bar_chart(df['BloodPressure'])
if select2=='SkinThickness':
st.area_chart(df['SkinThickness'])
if select2=='Insulin':
st.area_chart(df['Insulin'])
if select2=='BMI':
st.area_chart(df['BMI'])
if select2=='DiabetesPedigreeFunction':
st.area_chart(df['DiabetesPedigreeFunction'])
if select2=='Age':
st.area_chart(df['Age'])
# split data
X=df.iloc[:,0:8].values
Y=df.iloc[:,-1].values
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.2,random_state=0)
# get feature input from the user
def get_user_input():
st.sidebar.title('USER INPUTS')
pregnancies=st.sidebar.slider('Pregnancies',0,15,2) #range0-15 and default is 2
glucose=st.sidebar.slider('Glucose',0,200,110)
blood_pressure=st.sidebar.slider('Blood Pressure',0,100,70)
skin_thickness=st.sidebar.slider('Skin Thickness',0,80,30)
insulin=st.sidebar.slider('Insulin',0.0,700.0,220.0)
BMI=st.sidebar.slider('BMI',0.0,60.0,30.0)
DFF=st.sidebar.slider('DFF',0.0,2.0,0.08)
age=st.sidebar.slider('Age',10,90,35)
# store a dictionary into a variable
user_data={'Pregnancies':pregnancies,'Glucose':glucose,'Blood Pressure':blood_pressure,'Skin Thickness':skin_thickness,'Insulin':insulin,'BMI':BMI,'DFF':DFF,'Age':age}
# transform data into data frame
features=pd.DataFrame(user_data,index=[0])
return features
# store user input into a variable
user_input=get_user_input()
# set subheader and display user input
st.subheader('User Input: ')
st.write(user_input)
# create and train model
RandomForestClassifier=RandomForestClassifier()
RandomForestClassifier.fit(X_train,Y_train)
# show the model metrics
st.subheader('Model Test Accuracy score: ')
st.write(str(metrics.accuracy_score(Y_test,RandomForestClassifier.predict(X_test))*100)+'%')
st.subheader('Mean absolute Error: ')
st.write(str(metrics.mean_absolute_error(Y_test,RandomForestClassifier.predict(X_test))*100)+'%')
#st.subheader('Squared Error:')
#st.write(str(metrics.mean_squared_error(Y_test,RandomForestClassifier.predict(X_test))*100)+'%')
st.subheader('R2-score:')
st.write(str(metrics.r2_score(Y_test,RandomForestClassifier.predict(X_test))*100)+'%')
st.subheader('Confusion Matrix:')
st.write(metrics.confusion_matrix(Y_test,RandomForestClassifier.predict(X_test)))
# store the model predictions in a variable
prediction=RandomForestClassifier.predict(user_input)
# set a subheader and display the classifications
st.subheader('For diabetic person output is 1 else 0')
if st.button('Show Prediction'):
st.subheader('Classification: ')
st.write(prediction)
if prediction==0:
st.success('You are Healthy :) ')
if prediction==1:
st.warning('You are Diabetic :( ')
if text1=="M" or text1=="Others":
df.drop(columns=['Pregnancies'],axis=1,inplace=True)
st.dataframe(df)
print('')
st.write(df.describe())
# visualize data
st.header('Display Graphs')
select3=st.selectbox('Select the Input variable',('Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age'))
radio2=st.radio('',('Bar Chart','Line Chart'))
if radio2=='Bar Chart':
if select3=='Glucose':
st.bar_chart(df['Glucose'])
if select3=='BloodPressure':
st.bar_chart(df['BloodPressure'])
if select3=='SkinThickness':
st.bar_chart(df['SkinThickness'])
if select3=='Insulin':
st.bar_chart(df['Insulin'])
if select3=='BMI':
st.bar_chart(df['BMI'])
if select3=='DiabetesPedigreeFunction':
st.bar_chart(df['DiabetesPedigreeFunction'])
if select3=='Age':
st.bar_chart(df['Age'])
else:
if select3=='Glucose':
st.line_chart(df['Glucose'])
if select3=='BloodPressure':
st.bar_chart(df['BloodPressure'])
if select3=='SkinThickness':
st.line_chart(df['SkinThickness'])
if select3=='Insulin':
st.line_chart(df['Insulin'])
if select3=='BMI':
st.line_chart(df['BMI'])
if select3=='DiabetesPedigreeFunction':
st.line_chart(df['DiabetesPedigreeFunction'])
if select3=='Age':
st.line_chart(df['Age'])
check3=st.checkbox("Show Area Chart")
if check3:
select4=st.selectbox('Select Input variable',('Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age'))
if select4=='Glucose':
st.area_chart(df['Glucose'])
if select4=='BloodPressure':
st.bar_chart(df['BloodPressure'])
if select4=='SkinThickness':
st.area_chart(df['SkinThickness'])
if select4=='Insulin':
st.area_chart(df['Insulin'])
if select4=='BMI':
st.area_chart(df['BMI'])
if select4=='DiabetesPedigreeFunction':
st.area_chart(df['DiabetesPedigreeFunction'])
if select4=='Age':
st.area_chart(df['Age'])
# split data
X=df.iloc[:,0:7].values
Y=df.iloc[:,-1].values
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.2,random_state=0)
# get feature input from the user
def get_user_input():
st.sidebar.title('USER INPUTS')
#pregnancies=st.sidebar.slider('Pregnancies',0,15,2) #range0-15 and default is 2
glucose=st.sidebar.slider('Glucose',0,200,110)
blood_pressure=st.sidebar.slider('Blood Pressure',0,100,70)
skin_thickness=st.sidebar.slider('Skin Thickness',0,80,30)
insulin=st.sidebar.slider('Insulin',0.0,700.0,220.0)
BMI=st.sidebar.slider('BMI',0.0,60.0,30.0)
DFF=st.sidebar.slider('DFF',0.0,2.0,0.08)
age=st.sidebar.slider('Age',10,90,35)
# store a dictionary into a variable
user_data={'Glucose':glucose,'Blood Pressure':blood_pressure,'Skin Thickness':skin_thickness,'Insulin':insulin,'BMI':BMI,'DFF':DFF,'Age':age}
# transform data into data frame
features=pd.DataFrame(user_data,index=[0])
return features
# store user input into a variable
user_input=get_user_input()
# set subheader and display user input
st.subheader('User Input: ')
st.write(user_input)
# create and train model
RandomForestClassifier=RandomForestClassifier()
RandomForestClassifier.fit(X_train,Y_train)
# show the model metrics
st.subheader('Model Test Accuracy score: ')
st.write(str(metrics.accuracy_score(Y_test,RandomForestClassifier.predict(X_test))*100)+'%')
st.subheader('Mean absolute Error: ')
st.write(str(metrics.mean_absolute_error(Y_test,RandomForestClassifier.predict(X_test))*100)+'%')
#st.subheader('Squared Error:')
#st.write(str(metrics.mean_squared_error(Y_test,RandomForestClassifier.predict(X_test))*100)+'%')
st.subheader('R2-score:')
st.write(str(metrics.r2_score(Y_test,RandomForestClassifier.predict(X_test))*100)+'%')
st.subheader('Confusion Matrix:')
st.write(metrics.confusion_matrix(Y_test,RandomForestClassifier.predict(X_test)))
# store the model predictions in a variable
prediction=RandomForestClassifier.predict(user_input)
# set a subheader and display the classifications
st.subheader('For diabetic person output is 1 else 0')
if st.button('Show Prediction'):
st.subheader('Classification: ')
st.write(prediction)
if prediction==0:
st.success('You are Healthy :) ')
if prediction==1:
st.warning('You are Diabetic :( ')