-
Notifications
You must be signed in to change notification settings - Fork 2
/
sec5b_ml_model_binaryclass_pipeline_exec3_ml_process.py
161 lines (152 loc) · 6.33 KB
/
sec5b_ml_model_binaryclass_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
159
160
161
import sys
import numpy as np
import datetime
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
from sklearn.calibration import CalibratedClassifierCV
import sec5b_ml_model_binaryclass_pipeline as ml_pipeline
local_control_panel = {
'done_switch': False,
}
# Main function
######################################################################
def main(on_switch=False):
if on_switch:
save_switch = False # WARNING: Will overwrite existing if True
run_on_subsampled_data = True
run_on_full_data = False
run_on_unfeatured_data = False
run_on_featured_data = True
# Eg, full list >> [1, 5, 10, 50, 100, 500]
nk_list = [5]
'''Eg, Full list >> ['dummy', 'sex_only', 'name_basic_only', 'name_substring_only', 'name_numeric_only',
'name_metaphone_only', 'name_all', 'loc_basic_only', 'loc_sep_entity_only',
'loc_substring_only', 'loc_all', 'name_all_loc_all']'''
feature_set_list = ['name_all_loc_all_reduced']
ml_algo_dict = {
'LR': { 'clf': LogisticRegression(
tol = 3.359818286283781e-05,
solver = 'liblinear',
penalty = 'l1',
max_iter = 4000,
class_weight = None,
C = 4.281332398719396
)
},
'SVC_LINEAR': { 'clf': CalibratedClassifierCV(LinearSVC(
penalty='l2',
C=0.01)),
},
'NB': { 'clf': BernoulliNB(),
},
}
# Eg, Full list >> ['ab', 'fn', 'metis', 'inuit', 'ch', 'ja', 'en', 'fr', 'ir', 'it', 'rus', 'sc', 'others']
target_set_list = ['ch', 'ab']
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():
for target_label in target_set_list:
print('>> Current time:', datetime.datetime.now())
obj = ml_pipeline.MachineLearningNameEthnicityProjectBinaryClass(control_panel = {
'save_result_switch': save_switch, # WARNING: Will overwrite existing
'use_subsampled_df_switch': False, # WARNING: Switch to False in production
'use_subsampled_df_nk': nk,
'use_featured_df_switch': False,
'use_feature_set': [],
'feature_selection_switch': False,
'cross_validation_switch': False,
'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,
'binary_target_label': target_label,
'eval_score': None,
'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():
for target_label in target_set_list:
print('>> Current time:', datetime.datetime.now())
obj = ml_pipeline.MachineLearningNameEthnicityProjectBinaryClass(control_panel = {
'save_result_switch': save_switch, # WARNING: Will overwrite existing
'use_subsampled_df_switch': False, # WARNING: Switch to False in production
'use_subsampled_df_nk': nk,
'use_featured_df_switch': True,
'use_feature_set': feature_set,
'feature_selection_switch': False,
'cross_validation_switch': False,
'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,
'binary_target_label': target_label,
'eval_score': None,
'random_state': 888,
})
obj.machine_learning_steps()
if run_on_full_data:
# Run once using unfeatured, full dataset
if run_on_unfeatured_data:
for feature_set in feature_set_list:
for algo_key, algo_val in ml_algo_dict.items():
for target_label in target_set_list:
print('>> Current time:', datetime.datetime.now())
obj = ml_pipeline.MachineLearningNameEthnicityProjectBinaryClass(control_panel = {
'save_result_switch': save_switch, # WARNING: Will overwrite existing
'use_subsampled_df_switch': False, # WARNING: Switch to False in production
'use_subsampled_df_nk': 'none',
'use_featured_df_switch': False,
'use_feature_set': [],
'feature_selection_switch': False,
'cross_validation_switch': False,
'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,
'binary_target_label': target_label,
'eval_score': None,
'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():
for target_label in target_set_list:
print('>> Current time:', datetime.datetime.now())
obj = ml_pipeline.MachineLearningNameEthnicityProjectBinaryClass(control_panel = {
'save_result_switch': save_switch, # WARNING: Will overwrite existing
'use_subsampled_df_switch': False, # WARNING: Switch to False in production
'use_subsampled_df_nk': [],
'use_featured_df_switch': True,
'use_feature_set': feature_set,
'feature_selection_switch': False,
'cross_validation_switch': False,
'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,
'binary_target_label': target_label,
'eval_score': None,
'random_state': 888,
})
obj.machine_learning_steps()
if local_control_panel['done_switch']:
hf.done_alert()
if __name__=='__main__':
main(on_switch=True)