-
Notifications
You must be signed in to change notification settings - Fork 4
/
__init__.py
407 lines (366 loc) · 15.9 KB
/
__init__.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import matplotlib.pyplot as plt
import seaborn as sns
import io
import base64
import streamlit as st
import classification as classify
import regression as reg
import cluster
import pandas as pd
import mail
import os
# sns.set_theme(style="whitegrid")
# ax2 = sns.barplot(x="", y="", data="")
path = [os.getcwd()+'\\Images\\classi', os.getcwd()+'\\Images\\reg', os.getcwd()+'\\Images\\classi']
for i in path:
if not os.path.isdir(i):
os.makedirs(i)
def plot_graph(model, how):
try:
if how == "":
return classify.plot_model(model)
else:
return classify.plot_model(model, plot=how)
except:
return None
def upload():
st.header(uploader.name, ' upload successfully')
index = st.checkbox("Include first column as index")
if index:
df = pd.read_csv(uploader)
else:
df = pd.read_csv(uploader, index_col=False)
st.write(df.head())
if len(df.index) > 5:
str1 = "There are " + str(len(df.index)) + " rows of data, only the first 5 is shown."
st.caption(str1)
# create_graph()
def run_classification_model(df, data_split, seed, label, id, column):
class_model = classify.ClassificationAutoML()
columns = []
for i in column:
if not column[i] and i != '':
columns.append(i)
st.markdown('<hr>', unsafe_allow_html=True)
with st.spinner('The setup is loading...'):
results = class_model.classificationAutoML(df, trainSize=data_split, random_seed=seed,
targetName=label,
idColumnName=id, ignoreFeatures=columns)
with st.container():
st.header("Data Type")
st.write(results)
data_type = st.selectbox("Is the data type correct?", ["Yes", "No"])
proceed = st.checkbox("Proceed", key=1)
proceed1 = False
categorical, numerical = [], []
if proceed and data_type is "No":
type = {}
for i in column:
if column[i]:
type[i] = st.selectbox(i, ['Categorical', 'Numerical'])
for i in column:
if i not in columns:
if type[i] is 'Categorical':
categorical.append(i)
elif type[i] is 'Numerical':
numerical.append(i)
st.write(categorical)
st.write(numerical)
proceed1 = st.checkbox("Proceed", key=2)
if proceed1:
with st.spinner('The setup is loading...'):
st.write(split_size)
class_model.classificationAutoML(df, trainSize=split_size, random_seed=seed_number,
categoricalFeatures=categorical,
numericFeatures=numerical,
targetName=label,
idColumnName=id, ignoreFeatures=columns)
elif (proceed and data_type is "Yes") or proceed1:
st.markdown('<hr>', unsafe_allow_html=True)
with st.spinner('The model is under training, it might take few minutes to execute'):
best, results = class_model.fitClassificationModel()
st.header("Comparison")
st.write(results)
results = results.data
st.subheader("Accuracy graph")
plt.figure(figsize=(6, 2))
sns.set_theme(style="whitegrid")
ax = sns.barplot(x=results.index, y='Accuracy', data=results)
plt.xticks(rotation=90)
st.pyplot(plt)
plt.savefig(os.getcwd() + '\\Images\\classi\\Accuracy.png')
plt.clf()
st.subheader("AUC graph")
plt.figure(figsize=(6, 2))
sns.set_theme(style="whitegrid")
ax = sns.barplot(x=results.index, y='AUC', data=results)
plt.xticks(rotation=90)
st.pyplot(plt)
plt.savefig(os.getcwd() + '\\Images\\classi\\AUC.png')
plt.clf()
st.subheader("F1 graph")
plt.figure(figsize=(6, 2))
sns.set_theme(style="whitegrid")
ax = sns.barplot(x=results.index, y='F1', data=results)
plt.xticks(rotation=90)
st.pyplot(plt)
plt.savefig(os.getcwd() + '\\Images\\classi\\F1.png')
plt.clf()
st.markdown('<hr>', unsafe_allow_html=True)
st.header('The best model is ' + results.Model[0])
st.subheader('Best parameter to use')
st.code(best)
st.subheader('Performance of ' + results.Model[0])
plot_graph(best, '')
plot_graph(best, 'confusion_matrix')
plot_graph(best, 'class_report')
tuning = st.checkbox("Perform Hyperparameter Tuning?")
if tuning:
with st.spinner('Tuning might take some time, please be patience'):
best = class_model.tune(best)
st.subheader('Parameter of tuned model')
st.code(best)
download = st.button('Download model')
if download:
class_model.save(best)
st.write("check out this [link](https://colab.research.google.com/drive/1EJ_feSoTAnxcbM0wnfzlqGo5Vg6Vf7XI?usp=sharing) on how to implement the downloaded model")
def run_regression_model(df, data_split, seed, label, id, column):
reg_model = reg.RegressionAutoML()
columns = []
for i in column:
if not column[i] and i != '':
columns.append(i)
st.markdown('<hr>', unsafe_allow_html=True)
with st.spinner('The setup is loading...'):
results = reg_model.regressionAutoML(df, trainSize=data_split, random_seed=seed, targetName=label,
idColumnName=id, ignoreFeatures=columns)
with st.container():
st.header("Data Type")
st.write(results)
data_type = st.selectbox("Is the data type correct?", ["Yes", "No"])
proceed = st.checkbox("Proceed", key=1)
proceed1 = False
categorical, numerical = [], []
if proceed and data_type is "No":
type = {}
for i in column:
if column[i]:
type[i] = st.selectbox(i, ['Categorical', 'Numerical'])
for i in column:
if i not in columns:
if type[i] is 'Categorical':
categorical.append(i)
elif type[i] is 'Numerical':
numerical.append(i)
st.write(categorical)
st.write(numerical)
proceed1 = st.checkbox("Proceed", key=2)
if proceed1:
with st.spinner('The setup is loading...'):
reg_model.regressionAutoML(df, trainSize=split_size, random_seed=seed_number,
categoricalFeatures=categorical,
numericFeatures=numerical,
targetName=label,
idColumnName=id, ignoreFeatures=columns)
elif (proceed and data_type is "Yes") or proceed1:
st.markdown('<hr>', unsafe_allow_html=True)
with st.spinner('The model is under training, it might take few minutes to execute'):
best, results = reg_model.fitRegressionModels()
st.header("Comparison")
st.write(results)
results = results.data
st.subheader("MAE graph")
plt.figure(figsize=(6, 2))
sns.set_theme(style="whitegrid")
ax = sns.barplot(x=results.index, y='MAE', data=results)
plt.xticks(rotation=90)
st.pyplot(plt)
plt.savefig(os.getcwd() + '\\Images\\reg\\MAE.png')
plt.clf()
st.subheader("MSE graph")
plt.figure(figsize=(6, 2))
sns.set_theme(style="whitegrid")
ax = sns.barplot(x=results.index, y='MSE', data=results)
plt.xticks(rotation=90)
st.pyplot(plt)
plt.savefig(os.getcwd() + '\\Images\\reg\\MSE.png')
plt.clf()
st.subheader("RMSE graph")
plt.figure(figsize=(6, 2))
sns.set_theme(style="whitegrid")
ax = sns.barplot(x=results.index, y='RMSE', data=results)
plt.xticks(rotation=90)
st.pyplot(plt)
plt.savefig(os.getcwd() + '\\Images\\reg\\RMSE.png')
plt.clf()
st.subheader("R2 graph")
plt.figure(figsize=(6, 2))
sns.set_theme(style="whitegrid")
ax = sns.barplot(x=results.index, y='R2', data=results)
plt.xticks(rotation=90)
st.pyplot(plt)
plt.savefig(os.getcwd() + '\\Images\\R2.png')
plt.clf()
with st.container():
st.markdown('<hr>', unsafe_allow_html=True)
st.header('The best model is ' + results.Model[0])
st.subheader('Best parameter to use')
st.code(best)
st.subheader('Performance of '+ results.Model[0])
plot_graph(best, 'vc')
plot_graph(best, 'error')
plot_graph(best, 'residuals')
tuning = st.checkbox("Perform Hyperparameter Tuning?")
if tuning:
with st.spinner('Tuning might take some time, please be patience'):
best = reg_model.tune(best)
st.subheader('Parameter of tuned model')
st.code(best)
download = st.button('Download model')
if download:
reg_model.save(best)
def run_cluster_model(df, data_split, seed, id, column):
cluster_model = cluster.ClusterAutoML()
columns = []
for i in column:
if not column[i] and i != '':
columns.append(i)
st.markdown('<hr>', unsafe_allow_html=True)
with st.spinner('The setup is loading...'):
results = cluster_model.clusterAutoML(df, trainSize=data_split, random_seed=seed,
idColumnName=id, ignoreFeatures=columns)
with st.container():
st.header("Data Type")
st.write(results)
data_type = st.selectbox("Is the data type correct?", ["Yes", "No"])
proceed = st.checkbox("Proceed", key=1)
proceed1 = False
categorical, numerical = [], []
if proceed and data_type is "No":
type = {}
for i in column:
if column[i]:
type[i] = st.selectbox(i, ['Categorical', 'Numerical'])
for i in column:
if i not in columns:
if type[i] is 'Categorical':
categorical.append(i)
elif type[i] is 'Numerical':
numerical.append(i)
st.write(categorical)
st.write(numerical)
proceed1 = st.checkbox("Proceed", key=2)
if proceed1:
with st.spinner('The setup is loading...'):
cluster_model.clusterAutoML(df, trainSize=split_size, random_seed=seed_number,
categoricalFeatures=categorical,
numericFeatures=numerical,
idColumnName=id, ignoreFeatures=columns)
elif (proceed and data_type is "Yes") or proceed1:
st.markdown('<hr>', unsafe_allow_html=True)
with st.spinner('The model is under training, it might take few minutes to execute'):
results, result, best = cluster_model.get_models()
st.header("Comparison")
st.write(results)
st.subheader("Silhouette graph")
plt.figure(figsize=(6, 2))
sns.set_theme(style="whitegrid")
ax = sns.barplot(x=results.index, y='Silhouette', data=results)
plt.xticks(rotation=90)
st.pyplot(plt)
plt.savefig(os.getcwd() + '\\Images\\cluster\\Silhouette.png')
plt.clf()
st.subheader("Calinski-Harabasz graph")
plt.figure(figsize=(6, 2))
sns.set_theme(style="whitegrid")
ax = sns.barplot(x=results.index, y='Calinski-Harabasz', data=results)
plt.xticks(rotation=90)
st.pyplot(plt)
plt.savefig(os.getcwd() + '\\Images\\cluster\\Calinski-Harabasz.png')
plt.clf()
st.subheader("Davies-Bouldin graph")
plt.figure(figsize=(6, 2))
sns.set_theme(style="whitegrid")
ax = sns.barplot(x=results.index, y='Davies-Bouldin', data=results)
plt.xticks(rotation=90)
st.pyplot(plt)
plt.savefig(os.getcwd() + '\\Images\\cluster\\Davies-Bouldin.png')
plt.clf()
st.markdown('<hr>', unsafe_allow_html=True)
st.header('The best model is '+results.model[0])
st.subheader('Best parameter to use')
st.code(best)
st.subheader('Performance of '+ results.model[0])
plot_graph(best, 'distance')
plot_graph(best, 'elbow')
plot_graph(best, 'silhouette')
download = st.button('Download model')
if download:
cluster_model.save(best)
def imagedownload(plt, filename):
s = io.BytesIO()
plt.savefig(s, format='pdf', bbox_inches='tight')
plt.close()
b64 = base64.b64encode(s.getvalue()).decode() # strings <-> bytes conversions
href = f'<a href="data:image/png;base64,{b64}" download={filename}>Download {filename} File</a>'
return href
# Main page
st.title("MSG AutoML")
st.write("Developed by: Millenium Square Gang")
# chart = st.line_chart(last_rows)
# /Main page
def print_table(uploader, index=False):
df = pd.read_csv(uploader, index_col=index)
st.dataframe(df.head())
# Side bar
# progress_bar = st.sidebar.progress(0)
status_text = st.sidebar.empty()
with st.sidebar.header("1. Upload your CSV data"):
uploader = st.sidebar.file_uploader("Upload your input CSV file", help="Upload your dataset file here", type=['csv','xlsx'])
with st.sidebar.header('2. Choose model'):
model_type = st.sidebar.selectbox("Please choose a model to predict", ["Classification", "Regression", "Clustering"])
with st.sidebar.header('3. Set Parameters'):
split_size = st.sidebar.slider('Data split ratio (% for Training Set)', 10, 90, 80, 5)
seed_number = st.sidebar.slider('Set the random seed number', 1, 100, 42, 1)
with st.sidebar.header("4. Email"):
receivers = st.sidebar.text_area('User email(s)*', help='Separate multiple email by coma ( , )')
cc = st.sidebar.text_area('CC email(s)', help='Separate multiple email by coma ( , )')
# /Sidebar
# Dataset
if uploader is not None and receivers != '':
st.header(uploader.name+' preview')
df = pd.read_csv(uploader, index_col=False)
st.dataframe(df.head())
if len(df.index) > 5:
str1 = "There are " + str(len(df.index)-1) + " rows of data, only the first 5 is shown."
st.caption(str1)
label = ''
if model_type is not "Clustering":
st.subheader("Choose the Label column")
label = st.selectbox('Label column', df.columns, index=len(df.columns) - 1)
st.subheader("Choose the ID column")
ids = df.columns.tolist()
ids.insert(0, "--Not Selected--")
id = st.selectbox('ID column', ids, index=0)
if id == "--Not Selected--":
id=''
st.subheader("Choose feature")
column = {}
for i in df.columns:
if not (i == id or i == label):
column[i] = st.checkbox(i, value=True)
run = st.checkbox("Click to Run")
if run:
path = os.getcwd() + "\\Images"
if model_type is "Classification":
run_classification_model(df, split_size/100.0, seed_number, label, id, column)
path = path + '\\classi'
elif model_type is "Regression":
run_regression_model(df, split_size/100.0, seed_number, label, id, column)
path = path + '\\reg'
elif model_type is "Clustering":
run_cluster_model(df, split_size/100.0, seed_number, id, column)
path = path + '\\cluster'
mail.sendReport(path, model_type, receivers, cc=cc)
else:
st.info('Awaiting for dataset to be uploaded or user email')