-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
664 lines (531 loc) · 18.7 KB
/
script.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
# This file has been autogenerated by version 1.44.0 of the Azure Automated Machine Learning SDK.
import numpy
import numpy as np
import pandas as pd
import pickle
import argparse
def setup_instrumentation():
import logging
import sys
from azureml.core import Run
from azureml.telemetry import INSTRUMENTATION_KEY, get_telemetry_log_handler
from azureml.telemetry._telemetry_formatter import ExceptionFormatter
logger = logging.getLogger("azureml.training.tabular")
try:
logger.setLevel(logging.INFO)
# Add logging to STDOUT
stdout_handler = logging.StreamHandler(sys.stdout)
logger.addHandler(stdout_handler)
# Add telemetry logging with formatter to strip identifying info
telemetry_handler = get_telemetry_log_handler(
instrumentation_key=INSTRUMENTATION_KEY, component_name="azureml.training.tabular"
)
telemetry_handler.setFormatter(ExceptionFormatter())
logger.addHandler(telemetry_handler)
# Attach run IDs to logging info for correlation if running inside AzureML
try:
run = Run.get_context()
parent_run = run.parent
return logging.LoggerAdapter(logger, extra={
"properties": {
"codegen_run_id": run.id,
"parent_run_id": parent_run.id
}
})
except Exception:
pass
except Exception:
pass
return logger
logger = setup_instrumentation()
def split_dataset(X, y, weights, split_ratio, should_stratify):
from sklearn.model_selection import train_test_split
random_state = 42
if should_stratify:
stratify = y
else:
stratify = None
if weights is not None:
X_train, X_test, y_train, y_test, weights_train, weights_test = train_test_split(
X, y, weights, stratify=stratify, test_size=split_ratio, random_state=random_state
)
else:
X_train, X_test, y_train, y_test = train_test_split(
X, y, stratify=stratify, test_size=split_ratio, random_state=random_state
)
weights_train, weights_test = None, None
return (X_train, y_train, weights_train), (X_test, y_test, weights_test)
def get_training_dataset(dataset_id):
from azureml.core.dataset import Dataset
from azureml.core.run import Run
logger.info("Running get_training_dataset")
ws = Run.get_context().experiment.workspace
dataset = Dataset.get_by_id(workspace=ws, id=dataset_id)
return dataset.to_pandas_dataframe()
def prepare_data(dataframe):
from azureml.training.tabular.preprocessing import data_cleaning
logger.info("Running prepare_data")
label_column_name = 'rentals'
# extract the features, target and sample weight arrays
y = dataframe[label_column_name].values
X = dataframe.drop([label_column_name], axis=1)
sample_weights = None
X, y, sample_weights = data_cleaning._remove_nan_rows_in_X_y(X, y, sample_weights,
is_timeseries=False, target_column=label_column_name)
return X, y, sample_weights
def get_mapper_49c852(column_names):
from azureml.training.tabular.featurization.text.stringcast_transformer import StringCastTransformer
from azureml.training.tabular.featurization.utilities import wrap_in_list
from numpy import uint8
from sklearn.feature_extraction.text import CountVectorizer
from sklearn_pandas.dataframe_mapper import DataFrameMapper
from sklearn_pandas.features_generator import gen_features
definition = gen_features(
columns=column_names,
classes=[
{
'class': StringCastTransformer,
},
{
'class': CountVectorizer,
'analyzer': 'word',
'binary': True,
'decode_error': 'strict',
'dtype': numpy.uint8,
'encoding': 'utf-8',
'input': 'content',
'lowercase': True,
'max_df': 1.0,
'max_features': None,
'min_df': 1,
'ngram_range': (1, 1),
'preprocessor': None,
'stop_words': None,
'strip_accents': None,
'token_pattern': '(?u)\\b\\w\\w+\\b',
'tokenizer': wrap_in_list,
'vocabulary': None,
},
]
)
mapper = DataFrameMapper(features=definition, input_df=True, sparse=True)
return mapper
def get_mapper_9133f9(column_names):
from azureml.training.tabular.featurization.categorical.cat_imputer import CatImputer
from azureml.training.tabular.featurization.categorical.labelencoder_transformer import LabelEncoderTransformer
from azureml.training.tabular.featurization.text.stringcast_transformer import StringCastTransformer
from sklearn_pandas.dataframe_mapper import DataFrameMapper
from sklearn_pandas.features_generator import gen_features
definition = gen_features(
columns=column_names,
classes=[
{
'class': CatImputer,
'copy': True,
},
{
'class': StringCastTransformer,
},
{
'class': LabelEncoderTransformer,
'hashing_seed_val': 314489979,
},
]
)
mapper = DataFrameMapper(features=definition, input_df=True, sparse=True)
return mapper
def get_mapper_ab1045(column_names):
from sklearn.impute import SimpleImputer
from sklearn_pandas.dataframe_mapper import DataFrameMapper
from sklearn_pandas.features_generator import gen_features
definition = gen_features(
columns=column_names,
classes=[
{
'class': SimpleImputer,
'add_indicator': False,
'copy': True,
'fill_value': None,
'missing_values': numpy.nan,
'strategy': 'mean',
'verbose': 0,
},
]
)
mapper = DataFrameMapper(features=definition, input_df=True, sparse=True)
return mapper
def generate_data_transformation_config():
from sklearn.pipeline import FeatureUnion
column_group_1 = [['temp'], ['atemp'], ['hum'], ['windspeed']]
column_group_2 = ['year', 'holiday', 'workingday']
column_group_3 = ['day', 'mnth', 'season', 'weekday', 'weathersit']
feature_union = FeatureUnion([
('mapper_49c852', get_mapper_49c852(column_group_3)),
('mapper_9133f9', get_mapper_9133f9(column_group_2)),
('mapper_ab1045', get_mapper_ab1045(column_group_1)),
])
return feature_union
def generate_preprocessor_config_0():
from sklearn.preprocessing import MaxAbsScaler
preproc = MaxAbsScaler(
copy=True
)
return preproc
def generate_algorithm_config_0():
from xgboost.sklearn import XGBRegressor
algorithm = XGBRegressor(
base_score=0.5,
booster='gbtree',
colsample_bylevel=1,
colsample_bynode=1,
colsample_bytree=1,
gamma=0,
gpu_id=-1,
importance_type='gain',
interaction_constraints='',
learning_rate=0.300000012,
max_delta_step=0,
max_depth=6,
min_child_weight=1,
missing=numpy.nan,
monotone_constraints='()',
n_estimators=100,
n_jobs=0,
num_parallel_tree=1,
objective='reg:squarederror',
random_state=0,
reg_alpha=0,
reg_lambda=1,
scale_pos_weight=1,
subsample=1,
tree_method='auto',
validate_parameters=1,
verbose=-10,
verbosity=0
)
return algorithm
def generate_preprocessor_config_1():
from sklearn.preprocessing import MaxAbsScaler
preproc = MaxAbsScaler(
copy=True
)
return preproc
def generate_algorithm_config_1():
from lightgbm.sklearn import LGBMRegressor
algorithm = LGBMRegressor(
boosting_type='gbdt',
class_weight=None,
colsample_bytree=1.0,
importance_type='split',
learning_rate=0.1,
max_depth=-1,
min_child_samples=20,
min_child_weight=0.001,
min_split_gain=0.0,
n_estimators=100,
n_jobs=-1,
num_leaves=31,
objective=None,
random_state=None,
reg_alpha=0.0,
reg_lambda=0.0,
silent=True,
subsample=1.0,
subsample_for_bin=200000,
subsample_freq=0,
verbose=-1
)
return algorithm
def generate_preprocessor_config_2():
from sklearn.preprocessing import MaxAbsScaler
preproc = MaxAbsScaler(
copy=True
)
return preproc
def generate_algorithm_config_2():
from sklearn.ensemble import RandomForestRegressor
algorithm = RandomForestRegressor(
bootstrap=False,
ccp_alpha=0.0,
criterion='mse',
max_depth=None,
max_features=0.4,
max_leaf_nodes=None,
max_samples=None,
min_impurity_decrease=0.0,
min_impurity_split=None,
min_samples_leaf=0.006151578686713196,
min_samples_split=0.012814223889440833,
min_weight_fraction_leaf=0.0,
n_estimators=200,
n_jobs=-1,
oob_score=False,
random_state=None,
verbose=0,
warm_start=False
)
return algorithm
def generate_preprocessor_config_3():
from sklearn.preprocessing import StandardScaler
preproc = StandardScaler(
copy=True,
with_mean=False,
with_std=True
)
return preproc
def generate_algorithm_config_3():
from lightgbm.sklearn import LGBMRegressor
algorithm = LGBMRegressor(
boosting_type='gbdt',
class_weight=None,
colsample_bytree=0.6,
importance_type='split',
learning_rate=0.16842263157894738,
max_bin=1023,
max_depth=9,
min_child_samples=3,
min_child_weight=0.001,
min_split_gain=0.7368421052631579,
n_estimators=100,
n_jobs=-1,
num_leaves=255,
objective=None,
random_state=None,
reg_alpha=0,
reg_lambda=0.75,
silent=True,
subsample=0.9,
subsample_for_bin=200000,
subsample_freq=6,
verbose=-1
)
return algorithm
def generate_preprocessor_config_4():
from sklearn.preprocessing import StandardScaler
preproc = StandardScaler(
copy=True,
with_mean=False,
with_std=False
)
return preproc
def generate_algorithm_config_4():
from xgboost.sklearn import XGBRegressor
algorithm = XGBRegressor(
base_score=0.5,
booster='gbtree',
colsample_bylevel=1,
colsample_bynode=1,
colsample_bytree=0.9,
eta=0.5,
gamma=0.01,
gpu_id=-1,
importance_type='gain',
interaction_constraints='',
learning_rate=0.5,
max_delta_step=0,
max_depth=8,
max_leaves=0,
min_child_weight=1,
missing=numpy.nan,
monotone_constraints='()',
n_estimators=50,
n_jobs=0,
num_parallel_tree=1,
objective='reg:squarederror',
random_state=0,
reg_alpha=1.3541666666666667,
reg_lambda=1.6666666666666667,
scale_pos_weight=1,
subsample=1,
tree_method='auto',
validate_parameters=1,
verbose=-10,
verbosity=0
)
return algorithm
def generate_preprocessor_config_5():
from sklearn.preprocessing import MaxAbsScaler
preproc = MaxAbsScaler(
copy=True
)
return preproc
def generate_algorithm_config_5():
from sklearn.ensemble import ExtraTreesRegressor
algorithm = ExtraTreesRegressor(
bootstrap=False,
ccp_alpha=0.0,
criterion='mse',
max_depth=None,
max_features=0.8,
max_leaf_nodes=None,
max_samples=None,
min_impurity_decrease=0.0,
min_impurity_split=None,
min_samples_leaf=0.0023646822772690063,
min_samples_split=0.005285388593079247,
min_weight_fraction_leaf=0.0,
n_estimators=10,
n_jobs=-1,
oob_score=False,
random_state=None,
verbose=0,
warm_start=False
)
return algorithm
def generate_preprocessor_config_6():
from sklearn.preprocessing import Normalizer
preproc = Normalizer(
copy=True,
norm='l2'
)
return preproc
def generate_algorithm_config_6():
from xgboost.sklearn import XGBRegressor
algorithm = XGBRegressor(
base_score=0.5,
booster='gbtree',
colsample_bylevel=1,
colsample_bynode=1,
colsample_bytree=0.7,
eta=0.5,
gamma=0,
gpu_id=-1,
grow_policy='lossguide',
importance_type='gain',
interaction_constraints='',
learning_rate=0.5,
max_bin=255,
max_delta_step=0,
max_depth=2,
max_leaves=3,
min_child_weight=1,
missing=numpy.nan,
monotone_constraints='()',
n_estimators=25,
n_jobs=0,
num_parallel_tree=1,
objective='reg:squarederror',
random_state=0,
reg_alpha=2.1875,
reg_lambda=0.8333333333333334,
scale_pos_weight=1,
subsample=0.8,
tree_method='hist',
validate_parameters=1,
verbose=-10,
verbosity=0
)
return algorithm
def generate_preprocessor_config_7():
from sklearn.preprocessing import MaxAbsScaler
preproc = MaxAbsScaler(
copy=True
)
return preproc
def generate_algorithm_config_7():
from sklearn.linear_model import ElasticNet
algorithm = ElasticNet(
alpha=0.001,
copy_X=True,
fit_intercept=True,
l1_ratio=0.6873684210526316,
max_iter=1000,
normalize=False,
positive=False,
precompute=False,
random_state=None,
selection='cyclic',
tol=0.0001,
warm_start=False
)
return algorithm
def generate_algorithm_config():
from azureml.training.tabular.models.forecasting_pipeline_wrapper import PreFittedSoftVotingRegressor
from sklearn.pipeline import Pipeline
pipeline_0 = Pipeline(steps=[('preproc', generate_preprocessor_config_0()), ('model', generate_algorithm_config_0())])
pipeline_1 = Pipeline(steps=[('preproc', generate_preprocessor_config_1()), ('model', generate_algorithm_config_1())])
pipeline_2 = Pipeline(steps=[('preproc', generate_preprocessor_config_2()), ('model', generate_algorithm_config_2())])
pipeline_3 = Pipeline(steps=[('preproc', generate_preprocessor_config_3()), ('model', generate_algorithm_config_3())])
pipeline_4 = Pipeline(steps=[('preproc', generate_preprocessor_config_4()), ('model', generate_algorithm_config_4())])
pipeline_5 = Pipeline(steps=[('preproc', generate_preprocessor_config_5()), ('model', generate_algorithm_config_5())])
pipeline_6 = Pipeline(steps=[('preproc', generate_preprocessor_config_6()), ('model', generate_algorithm_config_6())])
pipeline_7 = Pipeline(steps=[('preproc', generate_preprocessor_config_7()), ('model', generate_algorithm_config_7())])
algorithm = PreFittedSoftVotingRegressor(
estimators=[
('model_0', pipeline_0),
('model_1', pipeline_1),
('model_2', pipeline_2),
('model_3', pipeline_3),
('model_4', pipeline_4),
('model_5', pipeline_5),
('model_6', pipeline_6),
('model_7', pipeline_7),
],
weights=[0.3333333333333333, 0.2, 0.06666666666666667, 0.06666666666666667, 0.13333333333333333, 0.06666666666666667, 0.06666666666666667, 0.06666666666666667]
)
return algorithm
def build_model_pipeline():
from sklearn.pipeline import Pipeline
logger.info("Running build_model_pipeline")
pipeline = Pipeline(
steps=[
('featurization', generate_data_transformation_config()),
('ensemble', generate_algorithm_config()),
]
)
return pipeline
def train_model(X, y, sample_weights=None, transformer=None):
logger.info("Running train_model")
model_pipeline = build_model_pipeline()
model = model_pipeline.fit(X, y)
return model
def calculate_metrics(model, X, y, sample_weights, X_test, y_test, cv_splits=None):
from azureml.training.tabular.preprocessing.binning import make_dataset_bins
from azureml.training.tabular.score.scoring import score_regression
y_pred = model.predict(X_test)
y_min = np.min(y)
y_max = np.max(y)
y_std = np.std(y)
bin_info = make_dataset_bins(X_test.shape[0], y_test)
metrics = score_regression(
y_test, y_pred, get_metrics_names(), y_max, y_min, y_std, sample_weights, bin_info)
return metrics
def get_metrics_names():
metrics_names = [
'mean_absolute_percentage_error',
'normalized_median_absolute_error',
'normalized_root_mean_squared_error',
'predicted_true',
'r2_score',
'spearman_correlation',
'residuals',
'root_mean_squared_log_error',
'median_absolute_error',
'normalized_mean_absolute_error',
'explained_variance',
'root_mean_squared_error',
'normalized_root_mean_squared_log_error',
'mean_absolute_error',
]
return metrics_names
def main(training_dataset_id=None):
from azureml.core.run import Run
# The following code is for when running this code as part of an AzureML script run.
run = Run.get_context()
df = get_training_dataset(training_dataset_id)
X, y, sample_weights = prepare_data(df)
split_ratio = 0.25
(X_train, y_train, sample_weights_train), (X_valid, y_valid, sample_weights_valid) = split_dataset(X, y, sample_weights, split_ratio, should_stratify=False)
model = train_model(X_train, y_train, sample_weights_train)
metrics = calculate_metrics(model, X, y, sample_weights, X_test=X_valid, y_test=y_valid)
print(metrics)
for metric in metrics:
run.log(metric, metrics[metric])
with open('model.pkl', 'wb') as f:
pickle.dump(model, f)
run.upload_file('outputs/model.pkl', 'model.pkl')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--training_dataset_id', type=str, default='c40daae3-2c7d-4e22-a231-4c2706efe0df', help='Default training dataset id is populated from the parent run')
args = parser.parse_args()
main(args.training_dataset_id)