-
Notifications
You must be signed in to change notification settings - Fork 2
/
sec5a_ml_model_multiclass_pipeline_exec3_ml_process.py
158 lines (149 loc) · 5.57 KB
/
sec5a_ml_model_multiclass_pipeline_exec3_ml_process.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
import sys
import numpy as np
import helper_functions.helper_functions as hf
from sklearn.multiclass import OneVsRestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import BernoulliNB
from sklearn.svm import LinearSVC, SVC
import sec5a_ml_model_multiclass_pipeline as ml_pipeline
local_control_panel = {
'done_switch': False,
}
# Main function
######################################################################
def main(on_switch=False):
if on_switch:
save_switch = False
run_on_subsampled_data = False
run_on_full_data = True
run_on_unfeatured_data = False
run_on_featured_data = True
# Eg, full list >> [5, 50, 500]
nk_list = [5]
# Eg, ['dummy', 'sex_only', 'name_all', 'name_all_loc_all', 'name_all_loc_all_reduced']
feature_set_list = ['name_all_loc_all_reduced']
ml_algo_dict = {
'LR': {'clf': LogisticRegression(
tol=0.01438449888287663,
solver='liblinear',
penalty='l2',
multi_class='ovr',
max_iter=50,
class_weight=None,
C=10000.0)
},
'SVC': {'clf': OneVsRestClassifier(LinearSVC(
tol=0.0001,
penalty='l2',
multi_class='ovr',
max_iter=50,
loss='squared_hinge',
class_weight='balanced',
C=0.08858667904100823)),
},
'NB': {'clf': BernoulliNB(
fit_prior=True,
binarize=0.6,
alpha=0.00026366508987303583)
},
}
if run_on_subsampled_data:
# Loop through subsampling n set with unfeatured data
if run_on_unfeatured_data:
for nk in nk_list:
for algo_key, algo_val in ml_algo_dict.items():
obj = ml_pipeline.MachineLearningNameEthnicityProjectMultiClass(control_panel = {
'save_result_switch': save_switch, # WARNING: Will overwrite existing
'use_subsampled_df_switch': True,
'use_subsampled_df_nk': nk,
'use_featured_df_switch': False,
'use_feature_set': [],
'feature_selection_switch': False,
'cross_validation_switch': False,
'cross_validation_repeat': None,
'ml_process_on_test_data_switch': True,
'ml_process_on_training_data_switch': False,
'ml_process_on_ext_data_switch': False,
'ml_algo': [algo_key, algo_val],
'ml_algo_param_grid': None,
'eval_score': None,
'label_varname': 'ETHNICITY_RECAT',
'random_state': 888,
})
obj.machine_learning_steps()
# Loop through feature set and subsampling n set
if run_on_featured_data:
for feature_set in feature_set_list:
for nk in nk_list:
for algo_key, algo_val in ml_algo_dict.items():
obj = ml_pipeline.MachineLearningNameEthnicityProjectMultiClass(control_panel = {
'save_result_switch': save_switch, # WARNING: Will overwrite existing
'use_subsampled_df_switch': True,
'use_subsampled_df_nk': nk,
'use_featured_df_switch': True,
'use_feature_set': feature_set,
'feature_selection_switch': False,
'cross_validation_switch': False,
'cross_validation_repeat': None,
'ml_process_on_test_data_switch': True,
'ml_process_on_training_data_switch': False,
'ml_process_on_ext_data_switch': False,
'ml_algo': [algo_key, algo_val],
'ml_algo_param_grid': None,
'eval_score': None,
'label_varname': 'ETHNICITY_RECAT',
'random_state': 888,
})
obj.machine_learning_steps()
if run_on_full_data:
# Run once using unfeatured, full dataset
if run_on_unfeatured_data:
for algo_key, algo_val in ml_algo_dict.items():
obj = ml_pipeline.MachineLearningNameEthnicityProjectMultiClass(control_panel = {
'save_result_switch': save_switch, # WARNING: Will overwrite existing
'use_subsampled_df_switch': False,
'use_subsampled_df_nk': [],
'use_featured_df_switch': True,
'use_feature_set': [],
'feature_selection_switch': False,
'cross_validation_switch': False,
'cross_validation_repeat': None,
'ml_process_on_test_data_switch': True,
'ml_process_on_training_data_switch': False,
'ml_process_on_ext_data_switch': False,
'ml_algo': [algo_key, algo_val],
'ml_algo_param_grid': None,
'eval_score': None,
'label_varname': 'ETHNICITY_RECAT',
'random_state': 888,
})
obj.machine_learning_steps()
# Run once using featured, full dataset
if run_on_featured_data:
for feature_set in feature_set_list:
for algo_key, algo_val in ml_algo_dict.items():
obj = ml_pipeline.MachineLearningNameEthnicityProjectMultiClass(control_panel = {
'save_result_switch': save_switch, # WARNING: Will overwrite existing
'use_subsampled_df_switch': False,
'use_subsampled_df_nk': [],
'use_featured_df_switch': True,
'use_feature_set': feature_set,
'feature_selection_switch': False,
'cross_validation_switch': False,
'cross_validation_repeat': None,
'ml_process_on_test_data_switch': True,
'ml_process_on_training_data_switch': False,
'ml_process_on_ext_data_switch': False,
'ml_algo': [algo_key, algo_val],
'ml_algo_param_grid': None,
'eval_score': None,
'label_varname': 'ETHNICITY_RECAT',
'random_state': 888,
})
obj.machine_learning_steps()
if local_control_panel['done_switch']:
hf.done_alert()
if __name__=='__main__':
main(on_switch=False)