-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmmd.py
990 lines (759 loc) · 45.4 KB
/
dmmd.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
'''
Runs the streamlit app
Call this file in the terminal via `streamlit run app.py`
'''
import shap
import streamlit as st
from streamlit_extras.colored_header import colored_header
from streamlit_option_menu import option_menu
from streamlit_extras.badges import badge
from streamlit_shap import st_shap
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split as TTS
from sklearn.model_selection import cross_val_score as CVS
from sklearn.model_selection import cross_validate as CV
from sklearn.metrics import make_scorer, r2_score
from sklearn.model_selection import LeaveOneOut
from sklearn.svm import SVR
from sklearn.ensemble import RandomForestRegressor as RFR
from sklearn.linear_model import LinearRegression as LinearR
from sklearn.linear_model import Lasso
from sklearn.linear_model import Ridge
from sklearn.neural_network import MLPRegressor
from sklearn.svm import SVR
from sklearn.feature_selection import mutual_info_regression as MIR
from sklearn.metrics import r2_score
from sklearn.metrics import make_scorer
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
import shap
import matplotlib.pyplot as plt
import pickle
from utils import *
from streamlit_extras.badges import badge
import warnings
# import sys
from prettytable import PrettyTable
import scienceplots
warnings.filterwarnings('ignore')
st.set_page_config(
page_title="DMMD",
page_icon="🍁",
layout="centered",
initial_sidebar_state="auto",
# menu_items={
# })
menu_items={
})
sysmenu = '''
<style>
MainMenu {visibility:hidden;}
footer {visibility:hidden;}
'''
# https://icons.bootcss.com/
st.markdown(sysmenu,unsafe_allow_html=True)
# arrow-repeat
with st.sidebar:
st.markdown(
'''
```
copyright
MGI(上海大学材料基因组工程研究院)
```
''')
select_option = option_menu("LBMDM", ["平台主页", "数据可视化", "特征工程", "回归预测", "可解释性机器学习","模型推理"],
icons=['house', 'clipboard-data', 'menu-button-wide','bezier2', 'arrow-repeat','subtract', 'app', 'microsoft'],
menu_icon="boxes", default_index=0)
df = pd.read_csv("./data/lbm.csv")
if select_option == "平台主页":
st.write('''![](https://github.com/Jiaxuan-Ma/Transfer-Learning/assets/61132191/d2b0d25d-1353-46eb-8b8a-13f35dc0ebd5?raw=true)''')
colored_header(label="铅铋腐蚀数据挖掘平台",description="LBM data-mining",color_name="violet-90")
elif select_option == "数据可视化":
# colored_header(label="数据可视化",description=" ",color_name="violet-90")
# check NaN
check_string_NaN(df)
# colored_header(label="数据信息",description=" ",color_name="violet-70")
# nrow = st.slider("rows", 1, len(df)-1, 5)
# df_nrow = df.head(nrow)
# st.write(df_nrow)
colored_header(label="数据初步统计",description=" ",color_name="violet-30")
st.write(df.describe())
# tmp_download_link = download_button(df.describe(), f'数据统计.csv', button_text='download')
# st.markdown(tmp_download_link, unsafe_allow_html=True)
# colored_header(label="特征变量和目标变量", description=" ",color_name="violet-70")
# target_num = st.number_input('目标变量数量', min_value=1, max_value=10, value=1)
# col_feature, col_target = st.columns(2)
target_num = 1
# features
features = df.iloc[:,:-target_num]
# targets
targets = df.iloc[:,-target_num:]
# with col_feature:
# st.write(features.head())
# with col_target:
# st.write(targets.head())
colored_header(label="特征变量统计分布", description=" ",color_name="violet-30")
feature_selected_name = st.selectbox('选择特征变量',list(features))
feature_selected_value = features[feature_selected_name]
plot = customPlot()
col1, col2 = st.columns([1,3])
with col1:
with st.expander("绘图参数"):
options_selected = [plot.set_title_fontsize(1),plot.set_label_fontsize(2),
plot.set_tick_fontsize(3),plot.set_legend_fontsize(4),plot.set_color('line color',6,5),plot.set_color('bin color',0,6)]
with col2:
plot.feature_hist_kde(options_selected,feature_selected_name,feature_selected_value)
#=========== Targets visulization ==================
colored_header(label="目标变量统计分布", description=" ",color_name="violet-30")
target_selected_name = st.selectbox('选择目标变量',list(targets))
target_selected_value = targets[target_selected_name]
plot = customPlot()
col1, col2 = st.columns([1,3])
with col1:
with st.expander("绘图参数"):
options_selected = [plot.set_title_fontsize(7),plot.set_label_fontsize(8),
plot.set_tick_fontsize(9),plot.set_legend_fontsize(10), plot.set_color('line color',6,11), plot.set_color('bin color',0,12)]
with col2:
plot.target_hist_kde(options_selected,target_selected_name,target_selected_value)
#=========== Features analysis ==================
colored_header(label="特征变量配方(合金成分)", description=" ",color_name="violet-30")
feature_range_selected_name = st.slider('选择特征变量个数',1,len(features.columns), (1,2))
min_feature_selected = feature_range_selected_name[0]-1
max_feature_selected = feature_range_selected_name[1]
feature_range_selected_value = features.iloc[:,min_feature_selected: max_feature_selected]
data_by_feature_type = df.groupby(list(feature_range_selected_value))
feature_type_data = create_data_with_group_and_counts(data_by_feature_type)
IDs = [str(id_) for id_ in feature_type_data['ID']]
Counts = feature_type_data['Count']
col1, col2 = st.columns([1,3])
with col1:
with st.expander("绘图参数"):
options_selected = [plot.set_title_fontsize(13),plot.set_label_fontsize(14),
plot.set_tick_fontsize(15),plot.set_legend_fontsize(16),plot.set_color('bin color',0, 17)]
with col2:
plot.featureSets_statistics_hist(options_selected,IDs, Counts)
# colored_header(label="特征变量在数据集中的分布", description=" ",color_name="violet-30")
# feature_selected_name = st.selectbox('选择特征变量', list(features),1)
# feature_selected_value = features[feature_selected_name]
# col1, col2 = st.columns([1,3])
# with col1:
# with st.expander("绘图参数"):
# options_selected = [plot.set_title_fontsize(18),plot.set_label_fontsize(19),
# plot.set_tick_fontsize(20),plot.set_legend_fontsize(21), plot.set_color('bin color', 0, 22)]
# with col2:
# plot.feature_distribution(options_selected,feature_selected_name,feature_selected_value)
# colored_header(label="特征变量和目标变量关系", description=" ",color_name="violet-30")
# col1, col2 = st.columns([1,3])
# with col1:
# with st.expander("绘图参数"):
# options_selected = [plot.set_title_fontsize(23),plot.set_label_fontsize(24),
# plot.set_tick_fontsize(25),plot.set_legend_fontsize(26),plot.set_color('scatter color',0, 27),plot.set_color('line color',6,28)]
# with col2:
# plot.features_and_targets(options_selected,df, list(features), list(targets))
# st.write("### Targets and Targets ")
# if targets.shape[1] != 1:
# colored_header(label="目标变量和目标变量关系", description=" ",color_name="violet-30")
# col1, col2 = st.columns([1,3])
# with col1:
# with st.expander("绘图参数"):
# options_selected = [plot.set_title_fontsize(29),plot.set_label_fontsize(30),
# plot.set_tick_fontsize(31),plot.set_legend_fontsize(32),plot.set_color('scatter color',0, 33),plot.set_color('line color',6,34)]
# with col2:
# plot.targets_and_targets(options_selected,df, list(targets))
# st.write('---')
elif select_option == "特征工程":
with st.sidebar:
sub_option = option_menu(None, ["特征和目标相关性", "特征重要性"])
if sub_option == "特征和目标相关性":
# colored_header(label="特征和目标相关性",description=" ",color_name="violet-90")
# check_string_NaN(df)
# colored_header(label="数据信息", description=" ",color_name="violet-70")
# nrow = st.slider("rows", 1, len(df)-1, 5)
# df_nrow = df.head(nrow)
# st.write(df_nrow)
# colored_header(label="特征变量和目标变量",description=" ",color_name="violet-70")
# target_num = st.number_input('目标变量数量', min_value=1, max_value=10, value=1)
target_num = 1
# col_feature, col_target = st.columns(2)
# features
features = df.iloc[:,:-target_num]
# targets
targets = df.iloc[:,-target_num:]
# with col_feature:
# st.write(features.head())
# with col_target:
# st.write(targets.head())
colored_header(label="丢弃与目标的低相关性特征",description=" ",color_name="violet-70")
fs = FeatureSelector(features, targets)
plot = customPlot()
target_selected_option = st.selectbox('选择特征', list(fs.targets))
col1, col2 = st.columns([1,3])
with col1:
corr_method = st.selectbox("相关性分析方法",["pearson"], key=15)
if corr_method != "MIR":
option_dropped_threshold = 0.1
if corr_method == 'MIR':
options_seed = st.checkbox('random state 1024',True)
with st.expander('绘图参数'):
options_selected = [plot.set_title_fontsize(11),plot.set_label_fontsize(12),
plot.set_tick_fontsize(13),plot.set_legend_fontsize(14),plot.set_color('bin color',19,16)]
with col2:
target_selected = fs.targets[target_selected_option]
if corr_method != "MIR":
corr_matrix = pd.concat([fs.features, target_selected], axis=1).corr(corr_method).abs()
fs.judge_drop_f_t([target_selected_option], corr_matrix, option_dropped_threshold)
fs.features_dropped_f_t = fs.features.drop(columns=fs.ops['f_t_low_corr'])
corr_f_t = pd.concat([fs.features_dropped_f_t, target_selected], axis=1).corr(corr_method)[target_selected_option][:-1]
plot.corr_feature_target(options_selected, corr_f_t)
# with st.expander('处理之后的数据'):
# data = pd.concat([fs.features_dropped_f_t, targets], axis=1)
# st.write(data)
# tmp_download_link = download_button(data, f'丢弃与目标的低相关性特征数据.csv', button_text='download')
# st.markdown(tmp_download_link, unsafe_allow_html=True)
else:
if options_seed:
corr_mir = MIR(fs.features, target_selected, random_state=1024)
else:
corr_mir = MIR(fs.features, target_selected)
corr_mir = pd.DataFrame(corr_mir).set_index(pd.Index(list(fs.features.columns)))
corr_mir.rename(columns={0: 'mutual info'}, inplace=True)
plot.corr_feature_target_mir(options_selected, corr_mir)
st.write('---')
elif sub_option == "特征重要性":
colored_header(label="特征重要性",description=" ",color_name="violet-90")
file = st.file_uploader("Upload `.csv`file", type=['csv'], label_visibility="collapsed")
if file is None:
table = PrettyTable(['上传文件名称', '名称','数据说明'])
table.add_row(['file_1','dataset','数据集'])
st.write(table)
if file is not None:
df = pd.read_csv(file)
# 检测缺失值
check_string_NaN(df)
colored_header(label="数据信息", description=" ",color_name="violet-70")
nrow = st.slider("rows", 1, len(df)-1, 5)
df_nrow = df.head(nrow)
st.write(df_nrow)
colored_header(label="特征变量和目标变量",description=" ",color_name="violet-70")
target_num = st.number_input('目标变量数量', min_value=1, max_value=10, value=1)
col_feature, col_target = st.columns(2)
# features
features = df.iloc[:,:-target_num]
# targets
targets = df.iloc[:,-target_num:]
with col_feature:
st.write(features.head())
with col_target:
st.write(targets.head())
fs = FeatureSelector(features,targets)
colored_header(label="选择目标变量", description=" ", color_name="violet-70")
target_selected_name = st.selectbox('目标变量', list(fs.targets)[::-1])
fs.targets = targets[target_selected_name]
colored_header(label="Selector", description=" ",color_name="violet-70")
model_path = './models/feature importance'
template_alg = model_platform(model_path=model_path)
colored_header(label="Training", description=" ",color_name="violet-70")
inputs, col2 = template_alg.show()
# st.write(inputs)
if inputs['model'] == 'LinearRegressor':
fs.model = LinearR()
with col2:
option_cumulative_importance = st.slider('累计重要性阈值',0.0, 1.0, 0.95)
Embedded_method = st.checkbox('Embedded method',False)
if Embedded_method:
cv = st.number_input('cv',1,10,5)
with st.container():
button_train = st.button('train', use_container_width=True)
if button_train:
fs.LinearRegressor()
fs.identify_zero_low_importance(option_cumulative_importance)
fs.feature_importance_select_show()
if Embedded_method:
threshold = fs.cumulative_importance
feature_importances = fs.feature_importances.set_index('feature',drop = False)
features = []
scores = []
cumuImportance = []
for i in range(1, len(fs.features.columns) + 1):
features.append(feature_importances.iloc[:i, 0].values.tolist())
X_selected = fs.features[features[-1]]
score = CVS(fs.model, X_selected, fs.targets, cv=cv ,scoring='r2').mean()
cumuImportance.append(feature_importances.loc[features[-1][-1], 'cumulative_importance'])
scores.append(score)
cumu_importance = np.array(cumuImportance)
scores = np.array(scores)
fig, ax = plt.subplots()
ax = plt.plot(cumu_importance, scores,'o-')
plt.xlabel("cumulative feature importance")
plt.ylabel("r2")
st.pyplot(fig)
elif inputs['model'] == 'LassoRegressor':
fs.model = Lasso(random_state=inputs['random state'])
with col2:
option_cumulative_importance = st.slider('累计重要性阈值',0.0, 1.0, 0.95)
Embedded_method = st.checkbox('Embedded method',False)
if Embedded_method:
cv = st.number_input('cv',1,10,5)
with st.container():
button_train = st.button('train', use_container_width=True)
if button_train:
fs.LassoRegressor()
fs.identify_zero_low_importance(option_cumulative_importance)
fs.feature_importance_select_show()
if Embedded_method:
threshold = fs.cumulative_importance
feature_importances = fs.feature_importances.set_index('feature',drop = False)
features = []
scores = []
cumuImportance = []
for i in range(1, len(fs.features.columns) + 1):
features.append(feature_importances.iloc[:i, 0].values.tolist())
X_selected = fs.features[features[-1]]
score = CVS(fs.model, X_selected, fs.targets, cv=cv, scoring='r2').mean()
cumuImportance.append(feature_importances.loc[features[-1][-1], 'cumulative_importance'])
scores.append(score)
cumu_importance = np.array(cumuImportance)
scores = np.array(scores)
fig, ax = plt.subplots()
ax = plt.plot(cumu_importance, scores,'o-')
plt.xlabel("cumulative feature importance")
plt.ylabel("r2")
st.pyplot(fig)
elif inputs['model'] == 'RidgeRegressor':
fs.model = Ridge(random_state=inputs['random state'])
with col2:
option_cumulative_importance = st.slider('累计重要性阈值',0.0, 1.0, 0.95)
Embedded_method = st.checkbox('Embedded method',False)
if Embedded_method:
cv = st.number_input('cv',1,10,5)
with st.container():
button_train = st.button('train', use_container_width=True)
if button_train:
fs.RidgeRegressor()
fs.identify_zero_low_importance(option_cumulative_importance)
fs.feature_importance_select_show()
if Embedded_method:
threshold = fs.cumulative_importance
feature_importances = fs.feature_importances.set_index('feature',drop = False)
features = []
scores = []
cumuImportance = []
for i in range(1, len(fs.features.columns) + 1):
features.append(feature_importances.iloc[:i, 0].values.tolist())
X_selected = fs.features[features[-1]]
score = CVS(fs.model, X_selected, fs.targets, cv=cv, scoring='r2').mean()
cumuImportance.append(feature_importances.loc[features[-1][-1], 'cumulative_importance'])
scores.append(score)
cumu_importance = np.array(cumuImportance)
scores = np.array(scores)
fig, ax = plt.subplots()
ax = plt.plot(cumu_importance, scores,'o-')
plt.xlabel("cumulative feature importance")
plt.ylabel("r2")
st.pyplot(fig)
elif inputs['model'] == 'LassoRegressor':
fs.model = Lasso(random_state=inputs['random state'])
with col2:
option_cumulative_importance = st.slider('累计重要性阈值',0.0, 1.0, 0.95)
Embedded_method = st.checkbox('Embedded method',False)
if Embedded_method:
cv = st.number_input('cv',1,10,5)
with st.container():
button_train = st.button('train', use_container_width=True)
if button_train:
fs.LassoRegressor()
fs.identify_zero_low_importance(option_cumulative_importance)
fs.feature_importance_select_show()
if Embedded_method:
threshold = fs.cumulative_importance
feature_importances = fs.feature_importances.set_index('feature',drop = False)
features = []
scores = []
cumuImportance = []
for i in range(1, len(fs.features.columns) + 1):
features.append(feature_importances.iloc[:i, 0].values.tolist())
X_selected = fs.features[features[-1]]
score = CVS(fs.model, X_selected, fs.targets, cv=cv, scoring='r2').mean()
cumuImportance.append(feature_importances.loc[features[-1][-1], 'cumulative_importance'])
scores.append(score)
cumu_importance = np.array(cumuImportance)
scores = np.array(scores)
fig, ax = plt.subplots()
ax = plt.plot(cumu_importance, scores,'o-')
plt.xlabel("cumulative feature importance")
plt.ylabel("r2")
st.pyplot(fig)
elif inputs['model'] == 'RandomForestRegressor':
fs.model = RFR(criterion = inputs['criterion'], n_estimators=inputs['nestimators'] ,random_state=inputs['random state'],max_depth=inputs['max depth'],min_samples_leaf=inputs['min samples leaf'],
min_samples_split=inputs['min samples split'],oob_score=inputs['oob score'], warm_start=inputs['warm start'],
n_jobs=inputs['njobs'])
with col2:
option_cumulative_importance = st.slider('累计重要性阈值',0.5, 1.0, 0.95)
Embedded_method = st.checkbox('Embedded method',False)
if Embedded_method:
cv = st.number_input('cv',1,10,5)
with st.container():
button_train = st.button('train', use_container_width=True)
if button_train:
fs.RandomForestRegressor()
fs.identify_zero_low_importance(option_cumulative_importance)
fs.feature_importance_select_show()
if Embedded_method:
threshold = fs.cumulative_importance
feature_importances = fs.feature_importances.set_index('feature',drop = False)
features = []
scores = []
cumuImportance = []
for i in range(1, len(fs.features.columns) + 1):
features.append(feature_importances.iloc[:i, 0].values.tolist())
X_selected = fs.features[features[-1]]
score = CVS(fs.model, X_selected, fs.targets, cv=cv ,scoring='r2').mean()
cumuImportance.append(feature_importances.loc[features[-1][-1], 'cumulative_importance'])
scores.append(score)
cumu_importance = np.array(cumuImportance)
scores = np.array(scores)
fig, ax = plt.subplots()
ax = plt.plot(cumu_importance, scores,'o-')
plt.xlabel("cumulative feature importance")
plt.ylabel("r2")
st.pyplot(fig)
st.write('---')
elif select_option == "回归预测":
# colored_header(label="回归预测",description=" ",color_name="violet-90")
# file = st.file_uploader("Upload `.csv`file", type=['csv'], label_visibility="collapsed")
# if file is None:
# table = PrettyTable(['上传文件名称', '名称','数据说明'])
# table.add_row(['file_1','dataset','数据集'])
# st.write(table)
# if file is not None:
# df = pd.read_csv(file)
# # 检测缺失值
# check_string_NaN(df)
# colored_header(label="数据信息", description=" ",color_name="violet-70")
# nrow = st.slider("rows", 1, len(df)-1, 5)
# df_nrow = df.head(nrow)
# st.write(df_nrow)
# colored_header(label="特征变量和目标变量",description=" ",color_name="violet-70")
# target_num = st.number_input('目标变量数量', min_value=1, max_value=10, value=1)
target_num = 1
# col_feature, col_target = st.columns(2)
# features
features = df.iloc[:,:-target_num]
# targets
targets = df.iloc[:,-target_num:]
# with col_feature:
# st.write(features.head())
# with col_target:
# st.write(targets.head())
# =================== model ====================================
reg = REGRESSOR(features,targets)
# colored_header(label="选择目标变量", description=" ", color_name="violet-70")
target_selected_option = list(reg.targets)[::-1]
reg.targets = targets[target_selected_option]
colored_header(label="Regressor", description=" ",color_name="violet-30")
model_path = './models/regressors'
template_alg = model_platform(model_path)
inputs, col2 = template_alg.show()
if inputs['model'] == 'RandomForestRegressor':
with col2:
with st.expander('Operator'):
# operator = st.selectbox('data operator', ('train test split','cross val score','leave one out','oob score'))
operator = st.selectbox('data operator', (['leave one out']))
# if operator == 'train test split':
# inputs['test size'] = st.slider('test size',0.1, 0.5, 0.2)
# reg.Xtrain, reg.Xtest, reg.Ytrain, reg.Ytest = TTS(reg.features,reg.targets,test_size=inputs['test size'],random_state=inputs['random state'])
# elif operator == 'cross val score':
# cv = st.number_input('cv',1,10,5)
if operator == 'leave one out':
loo = LeaveOneOut()
# elif operator == 'oob score':
# inputs['oob score'] = st.selectbox('oob score',[True], disabled=True)
# inputs['warm start'] = True
colored_header(label="Training", description=" ",color_name="violet-30")
with st.container():
button_train = st.button('Train', use_container_width=True)
if button_train:
# if operator == 'train test split':
# reg.model = RFR( n_estimators=inputs['nestimators'] ,random_state=inputs['random state'],max_depth=inputs['max depth'],min_samples_leaf=inputs['min samples leaf'],
# min_samples_split=inputs['min samples split'],oob_score=inputs['oob score'], warm_start=inputs['warm start'],
# n_jobs=inputs['njobs'])
# reg.RandomForestRegressor()
# result_data = pd.concat([reg.Ytest, pd.DataFrame(reg.Ypred)], axis=1)
# result_data.columns = ['actual','prediction']
# plot_and_export_results(reg, "RFR")
# elif operator == 'cross val score':
# reg.model = RFR(n_estimators=inputs['nestimators'],random_state=inputs['random state'],max_depth=inputs['max depth'],min_samples_leaf=inputs['min samples leaf'],
# min_samples_split=inputs['min samples split'],oob_score=inputs['oob score'], warm_start=inputs['warm start'],
# n_jobs=inputs['njobs'])
# cvs = CV(reg.model, reg.features, reg.targets, cv = cv, scoring=make_scorer(r2_score), return_train_score=False, return_estimator=True)
# plot_cross_val_results(cvs, "RFR_cv")
# elif operator == 'oob score':
# reg.model = RFR(criterion = inputs['criterion'],n_estimators=inputs['nestimators'] ,random_state=inputs['random state'],max_depth=inputs['max depth'],min_samples_leaf=inputs['min samples leaf'],
# min_samples_split=inputs['min samples split'],oob_score=inputs['oob score'], warm_start=inputs['warm start'],
# n_jobs=inputs['njobs'])
# reg_res = reg.model.fit(reg.features, reg.targets)
# oob_score = reg_res.oob_score_
# st.write(f'oob score : {oob_score}')
# if operator == 'leave one out':
reg.model = RFR(criterion = inputs['criterion'],n_estimators=inputs['nestimators'] ,random_state=inputs['random state'])
export_loo_results(reg, loo, "RFR_loo")
if inputs['model'] == 'SupportVector':
with col2:
with st.expander('Operator'):
# preprocess = st.selectbox('data preprocess',['StandardScaler','MinMaxScaler'])
preprocess = st.selectbox('data preprocess',['StandardScaler'])
# operator = st.selectbox('operator', ('train test split','cross val score', 'leave one out'), label_visibility='collapsed')
operator = st.selectbox('operator', ['leave one out'], label_visibility='collapsed')
# if operator == 'train test split':
# inputs['test size'] = st.slider('test size',0.1, 0.5, 0.2)
# if preprocess == 'StandardScaler':
# reg.features = StandardScaler().fit_transform(reg.features)
# if preprocess == 'MinMaxScaler':
# reg.features = MinMaxScaler().fit_transform(reg.features)
# reg.features = pd.DataFrame(reg.features)
# reg.Xtrain, reg.Xtest, reg.Ytrain, reg.Ytest = TTS(reg.features,reg.targets,test_size=inputs['test size'],random_state=inputs['random state'])
# elif operator == 'cross val score':
# if preprocess == 'StandardScaler':
# reg.features = StandardScaler().fit_transform(reg.features)
# if preprocess == 'MinMaxScaler':
# reg.features = MinMaxScaler().fit_transform(reg.features)
# cv = st.number_input('cv',1,10,5)
if operator == 'leave one out':
if preprocess == 'StandardScaler':
reg.features = StandardScaler().fit_transform(reg.features)
if preprocess == 'MinMaxScaler':
reg.features = MinMaxScaler().fit_transform(reg.features)
loo = LeaveOneOut()
colored_header(label="Training", description=" ",color_name="violet-30")
with st.container():
button_train = st.button('Train', use_container_width=True)
if button_train:
# if operator == 'train test split':
# reg.model = SVR(kernel=inputs['kernel'], C=inputs['C'])
# reg.SupportVector()
# result_data = pd.concat([reg.Ytest, pd.DataFrame(reg.Ypred)], axis=1)
# result_data.columns = ['actual','prediction']
# plot_and_export_results(reg, "SVR")
# elif operator == 'cross val score':
# reg.model = SVR(kernel=inputs['kernel'], C=inputs['C'])
# cvs = CV(reg.model, reg.features, reg.targets, cv = cv, scoring=make_scorer(r2_score), return_train_score=False, return_estimator=True)
# plot_cross_val_results(cvs, "SVR_cv")
if operator == 'leave one out':
reg.model = SVR(kernel=inputs['kernel'], C=inputs['C'])
export_loo_results(reg, loo, "SVR_loo")
# if inputs['model'] == 'LassoRegressor':
# with col2:
# with st.expander('Operator'):
# preprocess = st.selectbox('data preprocess',['StandardScaler','MinMaxScaler'])
# operator = st.selectbox('', ('train test split','cross val score', 'leave one out'), label_visibility='collapsed')
# if operator == 'train test split':
# inputs['test size'] = st.slider('test size',0.1, 0.5, 0.2)
# if preprocess == 'StandardScaler':
# reg.features = StandardScaler().fit_transform(reg.features)
# if preprocess == 'MinMaxScaler':
# reg.features = MinMaxScaler().fit_transform(reg.features)
# reg.features = pd.DataFrame(reg.features)
# reg.Xtrain, reg.Xtest, reg.Ytrain, reg.Ytest = TTS(reg.features,reg.targets,test_size=inputs['test size'],random_state=inputs['random state'])
# elif operator == 'cross val score':
# if preprocess == 'StandardScaler':
# reg.features = StandardScaler().fit_transform(reg.features)
# if preprocess == 'MinMaxScaler':
# reg.features = MinMaxScaler().fit_transform(reg.features)
# cv = st.number_input('cv',1,10,5)
# elif operator == 'leave one out':
# if preprocess == 'StandardScaler':
# reg.features = StandardScaler().fit_transform(reg.features)
# if preprocess == 'MinMaxScaler':
# reg.features = MinMaxScaler().fit_transform(reg.features)
# loo = LeaveOneOut()
# colored_header(label="Training", description=" ",color_name="violet-30")
# with st.container():
# button_train = st.button('Train', use_container_width=True)
# if button_train:
# if operator == 'train test split':
# reg.model = Lasso(alpha=inputs['alpha'],random_state=inputs['random state'])
# reg.LassoRegressor()
# result_data = pd.concat([reg.Ytest, pd.DataFrame(reg.Ypred)], axis=1)
# result_data.columns = ['actual','prediction']
# plot_and_export_results(reg, "LassoR")
# elif operator == 'cross val score':
# reg.model = Lasso(alpha=inputs['alpha'],random_state=inputs['random state'])
# cvs = CV(reg.model, reg.features, reg.targets, cv = cv, scoring=make_scorer(r2_score), return_train_score=False, return_estimator=True)
# plot_cross_val_results(cvs, "LassoR_cv")
# elif operator == 'leave one out':
# reg.model = Lasso(alpha=inputs['alpha'],random_state=inputs['random state'])
# export_loo_results(reg, loo, "LassoR_loo")
if inputs['model'] == 'MLPRegressor':
with col2:
with st.expander('Operator'):
preprocess = st.selectbox('data preprocess',['StandardScaler'])
operator = st.selectbox('operator', (['leave one out']), label_visibility='collapsed')
# if operator == 'train test split':
# inputs['test size'] = st.slider('test size',0.1, 0.5, 0.2)
# if preprocess == 'StandardScaler':
# reg.features = StandardScaler().fit_transform(reg.features)
# if preprocess == 'MinMaxScaler':
# reg.features = MinMaxScaler().fit_transform(reg.features)
# reg.features = pd.DataFrame(reg.features)
# reg.Xtrain, reg.Xtest, reg.Ytrain, reg.Ytest = TTS(reg.features,reg.targets,test_size=inputs['test size'],random_state=inputs['random state'])
# elif operator == 'cross val score':
# if preprocess == 'StandardScaler':
# reg.features = StandardScaler().fit_transform(reg.features)
# if preprocess == 'MinMaxScaler':
# reg.features = MinMaxScaler().fit_transform(reg.features)
# cv = st.number_input('cv',1,10,5)
if operator == 'leave one out':
if preprocess == 'StandardScaler':
reg.features = StandardScaler().fit_transform(reg.features)
if preprocess == 'MinMaxScaler':
reg.features = MinMaxScaler().fit_transform(reg.features)
loo = LeaveOneOut()
colored_header(label="Training", description=" ",color_name="violet-30")
with st.container():
button_train = st.button('Train', use_container_width=True)
if button_train:
# if operator == 'train test split':
# reg.model = MLPRegressor(hidden_layer_sizes=inputs['hidden layer size'], activation= inputs['activation'], solver=inputs['solver']
# , max_iter=inputs['max iter'], random_state=inputs['random state'])
# reg.MLPRegressor()
# result_data = pd.concat([reg.Ytest, pd.DataFrame(reg.Ypred)], axis=1)
# result_data.columns = ['actual','prediction']
# plot_and_export_results(reg, "MLP")
# elif operator == 'cross val score':
# reg.model = MLPRegressor(hidden_layer_sizes=inputs['hidden layer size'], activation= inputs['activation'], solver=inputs['solver'],
# batch_size=inputs['batch size'], learning_rate= inputs['learning rate'], max_iter=inputs['max iter'],
# random_state=inputs['random state'])
# cvs = CV(reg.model, reg.features, reg.targets, cv = cv, scoring=make_scorer(r2_score), return_train_score=False, return_estimator=True)
# plot_cross_val_results(cvs, "MLP_cv")
if operator == 'leave one out':
reg.model = MLPRegressor(hidden_layer_sizes=inputs['hidden layer size'], activation= inputs['activation'], solver=inputs['solver'],
max_iter=inputs['max iter'],random_state=inputs['random state'])
export_loo_results(reg, loo, "MLP_loo")
st.write('---')
elif select_option == "可解释性机器学习":
# colored_header(label="可解释性机器学习",description=" ",color_name="violet-90")
# file = st.file_uploader("Upload `.csv`file", type=['csv'], label_visibility="collapsed")
# if file is None:
# table = PrettyTable(['上传文件名称', '名称','数据说明'])
# table.add_row(['file_1','dataset','数据集'])
# st.write(table)
# if file is not None:
# df = pd.read_csv(file)
# 检测缺失值
# check_string_NaN(df)
# colored_header(label="数据信息", description=" ",color_name="violet-70")
# nrow = st.slider("rows", 1, len(df)-1, 5)
# df_nrow = df.head(nrow)
# st.write(df_nrow)
# colored_header(label="特征变量和目标变量",description=" ",color_name="violet-70")
# target_num = st.number_input('目标变量数量', min_value=1, max_value=10, value=1)
target_num = 1
# col_feature, col_target = st.columns(2)
# features
features = df.iloc[:,:-target_num]
# targets
targets = df.iloc[:,-target_num:]
# with col_feature:
# st.write(features.head())
# with col_target:
# st.write(targets.head())
colored_header(label="Shapley value",description=" ",color_name="violet-70")
fs = FeatureSelector(features, targets)
target_selected_option = st.selectbox('choose target', list(fs.targets))
fs.targets = fs.targets[target_selected_option]
# regressor = st.selectbox('tree',['linear','kernel','sampling'])
reg = RFR()
X_train, X_test, y_train, y_test = TTS(fs.features, fs.targets, random_state=0)
test_size = 0.01
random_state = st.checkbox('random state 42',True)
if random_state:
random_state = 42
else:
random_state = None
fs.Xtrain,fs.Xtest, fs.Ytrain, fs.Ytest = TTS(fs.features,fs.targets,test_size=test_size,random_state=random_state)
reg.fit(fs.Xtrain, fs.Ytrain)
explainer = shap.TreeExplainer(reg)
shap_values = explainer(fs.features)
# colored_header(label="SHAP Feature Importance", description=" ",color_name="violet-30")
# nfeatures = st.slider("features", 2, fs.features.shape[1],fs.features.shape[1])
# st_shap(shap.plots.bar(shap_values, max_display=nfeatures))
# st.write(shap_values)
# colored_header(label="SHAP Feature Cluster", description=" ",color_name="violet-30")
# clustering = shap.utils.hclust(fs.features, fs.targets)
# clustering_cutoff = st.slider('clustering cutoff', 0.0,1.0,0.5)
# nfeatures = st.slider("features", 2, fs.features.shape[1],fs.features.shape[1], key=2)
# st_shap(shap.plots.bar(shap_values, clustering=clustering, clustering_cutoff=clustering_cutoff, max_display=nfeatures))
# colored_header(label="SHAP Beeswarm", description=" ",color_name="violet-30")
# rank_option = st.selectbox('rank option',['max','mean'])
# max_dispaly = st.slider('max display',2, fs.features.shape[1],fs.features.shape[1])
# if rank_option == 'max':
# st_shap(shap.plots.beeswarm(shap_values, order = shap_values.abs.max(0), max_display =max_dispaly))
# else:
# st_shap(shap.plots.beeswarm(shap_values, order = shap_values.abs.mean(0), max_display =max_dispaly))
colored_header(label="SHAP Dependence", description=" ",color_name="violet-30")
shap_values = explainer.shap_values(fs.features)
list_features = fs.features.columns.tolist()
feature = st.selectbox('feature',list_features)
interact_feature = st.selectbox('interact feature', list_features)
st_shap(shap.dependence_plot(feature, shap_values, fs.features, display_features=fs.features,interaction_index=interact_feature))
elif select_option == "模型推理":
# colored_header(label="模型推理",description=" ",color_name="violet-90")
file = st.file_uploader("Upload `.csv`file ", label_visibility="collapsed")
# if len(file) < 2:
# table = PrettyTable(['上传文件名称', '名称','数据说明'])
# table.add_row(['file_1','data set','数据集'])
# table.add_row(['file_2','model','模型'])
# st.write(table)
# elif len(file) == 2:
# df = pd.read_csv(file[0])
# model_file = file[1]
if file:
df = pd.read_csv(file)
# check_string_NaN(df)
# colored_header(label="数据信息", description=" ",color_name="violet-70")
# nrow = st.slider("rows", 1, len(df)-1, 5)
# df_nrow = df.head(nrow)
# st.write(df_nrow)
# colored_header(label="特征变量和目标变量",description=" ",color_name="violet-70")
# target_num = st.number_input('目标变量数量', min_value=1, max_value=10, value=1)
target_num = 1
col_feature, col_target = st.columns(2)
# features
# features = df.iloc[:,:-target_num]
# targets
# targets = df.iloc[:,-target_num:]
# with col_feature:
# st.write(features.head())
# with col_target:
# st.write(targets.head())
# colored_header(label="选择目标变量", description=" ", color_name="violet-70")
# target_selected_option = st.selectbox('target', list(targets)[::-1])
# targets = targets[target_selected_option]
preprocess = st.selectbox('data preprocess',['StandardScaler'])
# if preprocess == 'StandardScaler':
features = StandardScaler().fit_transform(df)
# elif preprocess == 'MinMaxScaler':
# features = MinMaxScaler().fit_transform(features)
model_path = './data/svm.pickle'
# model_file = pickle.dumps(model_path)
# model = pickle.loads(model_path)
with open(model_path,'rb') as f:
model = pickle.load(f) #将模型存储在变量clf_load中
# print(clf_load.predict(X[0:1000])) #调用模型并预测结果
prediction = model.predict(features)
st.write("预测结果")
result_data = pd.DataFrame(prediction)
# st.write(result_data)
result_data.columns = ['TOL预测值']
# plot = customPlot()
# plot.pred_vs_actual(df, prediction)
# r2 = r2_score(df, prediction)
# st.write('R2: {}'.format(r2))
# result_data = pd.concat([targets, pd.DataFrame(prediction)], axis=1)
# result_data.columns = ['actual','prediction']
# with st.expander('预测结果'):
st.write(result_data)
tmp_download_link = download_button(result_data, f'预测结果.csv', button_text='download')
st.markdown(tmp_download_link, unsafe_allow_html=True)
st.write('---')