-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelRun.py
1075 lines (865 loc) · 40.8 KB
/
ModelRun.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
991
992
993
994
995
996
997
998
999
1000
import pandas as pd
import numpy as np
from sklearn.model_selection import StratifiedKFold, KFold, RepeatedKFold, GroupKFold, GridSearchCV, train_test_split, TimeSeriesSplit, RepeatedStratifiedKFold
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.base import BaseEstimator, TransformerMixin
import lightgbm as lgb
from category_encoders.ordinal import OrdinalEncoder
import copy
import time
import xgboost as xgb
import matplotlib.pyplot as plt
import seaborn as sns
class LGBWrapper(object):
"""
A wrapper for lightgbm model so that we will have a single api for various models.
"""
def __init__(self):
self.model = lgb.LGBMClassifier()
def fit(self, X_train, y_train, X_valid=None, y_valid=None, X_holdout=None, y_holdout=None, params=None):
eval_set = [(X_train, y_train)]
eval_names = ['train']
self.model = self.model.set_params(**params)
if X_valid is not None:
eval_set.append((X_valid, y_valid))
eval_names.append('valid')
if X_holdout is not None:
eval_set.append((X_holdout, y_holdout))
eval_names.append('holdout')
if 'cat_cols' in params.keys():
cat_cols = [col for col in params['cat_cols'] if col in X_train.columns]
if len(cat_cols) > 0:
categorical_columns = params['cat_cols']
else:
categorical_columns = 'auto'
else:
categorical_columns = 'auto'
self.model.fit(X=X_train, y=y_train,
eval_set=eval_set, eval_names=eval_names, eval_metric=eval_qwk_lgb,
verbose=params['verbose'], early_stopping_rounds=params['early_stopping_rounds'],
categorical_feature=categorical_columns)
self.best_score_ = self.model.best_score_
self.feature_importances_ = self.model.feature_importances_
def predict_proba(self, X_test):
if self.model.objective == 'binary':
return self.model.predict_proba(X_test, num_iteration=self.model.best_iteration_)[:, 1]
else:
return self.model.predict_proba(X_test, num_iteration=self.model.best_iteration_)
class ClassifierModel(object):
"""
A wrapper class for classification models.
It can be used for training and prediction.
Can plot feature importance and training progress (if relevant for model).
"""
def __init__(self, columns: list = None, model_wrapper=None):
"""
:param original_columns:
:param model_wrapper:
"""
self.columns = columns
self.model_wrapper = model_wrapper
self.result_dict = {}
self.train_one_fold = False
self.preprocesser = None
def fit(self, X: pd.DataFrame, y,
X_holdout: pd.DataFrame = None, y_holdout=None,
folds=None,
params: dict = None,
eval_metric='auc',
cols_to_drop: list = None,
preprocesser=None,
transformers: dict = None,
adversarial: bool = False,
plot: bool = True):
"""
Training the model.
:param X: training data
:param y: training target
:param X_holdout: holdout data
:param y_holdout: holdout target
:param folds: folds to split the data. If not defined, then model will be trained on the whole X
:param params: training parameters
:param eval_metric: metric for validataion
:param cols_to_drop: list of columns to drop (for example ID)
:param preprocesser: preprocesser class
:param transformers: transformer to use on folds
:param adversarial
:return:
"""
if folds is None:
folds = KFold(n_splits=3, random_state=42)
self.train_one_fold = True
self.columns = X.columns if self.columns is None else self.columns
self.feature_importances = pd.DataFrame(columns=['feature', 'importance'])
self.trained_transformers = {k: [] for k in transformers}
self.transformers = transformers
self.models = []
self.folds_dict = {}
self.eval_metric = eval_metric
n_target = 4# 1 if len(set(y.values)) == 2 else len(set(y.values))
self.oof = np.zeros((len(X), n_target))
self.n_target = n_target
X = X[self.columns]
if X_holdout is not None:
X_holdout = X_holdout[self.columns]
if preprocesser is not None:
self.preprocesser = preprocesser
self.preprocesser.fit(X, y)
X = self.preprocesser.transform(X, y)
self.columns = X.columns.tolist()
if X_holdout is not None:
X_holdout = self.preprocesser.transform(X_holdout)
# y = X['accuracy_group']
for fold_n, (train_index, valid_index) in enumerate(folds.split(X, y, X['game_session'])):
if X_holdout is not None:
X_hold = X_holdout.copy()
else:
X_hold = None
self.folds_dict[fold_n] = {}
if params['verbose']:
print(f'Fold {fold_n + 1} started at {time.ctime()}')
self.folds_dict[fold_n] = {}
X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
if self.train_one_fold:
X_train = X[self.columns]
y_train = y
X_valid = None
y_valid = None
datasets = {'X_train': X_train, 'X_valid': X_valid, 'X_holdout': X_hold, 'y_train': y_train}
X_train, X_valid, X_hold = self.transform_(datasets, cols_to_drop)
self.folds_dict[fold_n]['columns'] = X_train.columns.tolist()
model = copy.deepcopy(self.model_wrapper)
if adversarial:
X_new1 = X_train.copy()
if X_valid is not None:
X_new2 = X_valid.copy()
elif X_holdout is not None:
X_new2 = X_holdout.copy()
X_new = pd.concat([X_new1, X_new2], axis=0)
y_new = np.hstack((np.zeros((X_new1.shape[0])), np.ones((X_new2.shape[0]))))
X_train, X_valid, y_train, y_valid = train_test_split(X_new, y_new)
model.fit(X_train, y_train, X_valid, y_valid, X_hold, y_holdout, params=params)
self.folds_dict[fold_n]['scores'] = model.best_score_
if self.oof.shape[0] != len(X):
self.oof = np.zeros((X.shape[0], self.oof.shape[1]))
if not adversarial:
self.oof[valid_index] = model.predict_proba(X_valid).reshape(-1, n_target)
fold_importance = pd.DataFrame(list(zip(X_train.columns, model.feature_importances_)),
columns=['feature', 'importance'])
self.feature_importances = self.feature_importances.append(fold_importance)
self.models.append(model)
self.feature_importances['importance'] = self.feature_importances['importance'].astype(float)
# if params['verbose']:
self.calc_scores_()
if plot:
print(classification_report(y, self.oof.argmax(1)))
fig, ax = plt.subplots(figsize=(16, 12))
plt.subplot(2, 2, 1)
self.plot_feature_importance(top_n=25)
plt.subplot(2, 2, 2)
self.plot_metric()
plt.subplot(2, 2, 3)
g = sns.heatmap(confusion_matrix(y, self.oof.argmax(1)), annot=True, cmap=plt.cm.Blues,fmt="d")
g.set(ylim=(-0.5, 4), xlim=(-0.5, 4), title='Confusion matrix')
plt.subplot(2, 2, 4)
plt.hist(self.oof.argmax(1))
plt.xticks(range(self.n_target), range(self.n_target))
plt.title('Distribution of oof predictions');
def transform_(self, datasets, cols_to_drop):
for name, transformer in self.transformers.items():
transformer.fit(datasets['X_train'], datasets['y_train'])
datasets['X_train'] = transformer.transform(datasets['X_train'])
if datasets['X_valid'] is not None:
datasets['X_valid'] = transformer.transform(datasets['X_valid'])
if datasets['X_holdout'] is not None:
datasets['X_holdout'] = transformer.transform(datasets['X_holdout'])
self.trained_transformers[name].append(transformer)
if cols_to_drop is not None:
cols_to_drop = [col for col in cols_to_drop if col in datasets['X_train'].columns]
self.cols_to_drop = cols_to_drop
datasets['X_train'] = datasets['X_train'].drop(cols_to_drop, axis=1)
if datasets['X_valid'] is not None:
datasets['X_valid'] = datasets['X_valid'].drop(cols_to_drop, axis=1)
if datasets['X_holdout'] is not None:
datasets['X_holdout'] = datasets['X_holdout'].drop(cols_to_drop, axis=1)
return datasets['X_train'], datasets['X_valid'], datasets['X_holdout']
def calc_scores_(self):
datasets = [k for k, v in [v['scores'] for k, v in self.folds_dict.items()][0].items() if len(v) > 0]
self.scores = {}
for d in datasets:
scores = [v['scores'][d][self.eval_metric] for k, v in self.folds_dict.items()]
print(f"CV mean score on {d}: {np.mean(scores):.4f} +/- {np.std(scores):.4f} std.")
self.scores[d] = np.mean(scores)
def predict(self, X_test, averaging: str = 'usual'):
"""
Make prediction
:param X_test:
:param averaging: method of averaging
:return:
"""
full_prediction = np.zeros((X_test.shape[0], self.oof.shape[1]))
if self.preprocesser is not None:
X_test = self.preprocesser.transform(X_test)
for i in range(len(self.models)):
X_t = X_test.copy()
for name, transformers in self.trained_transformers.items():
X_t = transformers[i].transform(X_t)
cols_to_drop = [col for col in self.cols_to_drop if col in X_t.columns]
X_t = X_t.drop(cols_to_drop, axis=1)
y_pred = self.models[i].predict_proba(X_t[self.folds_dict[i]['columns']]).reshape(-1, full_prediction.shape[1])
# if case transformation changes the number of the rows
if full_prediction.shape[0] != len(y_pred):
full_prediction = np.zeros((y_pred.shape[0], self.oof.shape[1]))
if averaging == 'usual':
full_prediction += y_pred
elif averaging == 'rank':
full_prediction += pd.Series(y_pred).rank().values
return full_prediction / len(self.models)
def plot_feature_importance(self, drop_null_importance: bool = True, top_n: int = 10):
"""
Plot default feature importance.
:param drop_null_importance: drop columns with null feature importance
:param top_n: show top n columns
:return:
"""
top_feats = self.get_top_features(drop_null_importance, top_n)
feature_importances = self.feature_importances.loc[self.feature_importances['feature'].isin(top_feats)]
feature_importances['feature'] = feature_importances['feature'].astype(str)
top_feats = [str(i) for i in top_feats]
sns.barplot(data=feature_importances, x='importance', y='feature', orient='h', order=top_feats)
plt.title('Feature importances')
def get_top_features(self, drop_null_importance: bool = True, top_n: int = 10):
"""
Get top features by importance.
:param drop_null_importance:
:param top_n:
:return:
"""
grouped_feats = self.feature_importances.groupby(['feature'])['importance'].mean()
if drop_null_importance:
grouped_feats = grouped_feats[grouped_feats != 0]
return list(grouped_feats.sort_values(ascending=False).index)[:top_n]
def plot_metric(self):
"""
Plot training progress.
Inspired by `plot_metric` from https://lightgbm.readthedocs.io/en/latest/_modules/lightgbm/plotting.html
:return:
"""
full_evals_results = pd.DataFrame()
for model in self.models:
evals_result = pd.DataFrame()
for k in model.model.evals_result_.keys():
evals_result[k] = model.model.evals_result_[k][self.eval_metric]
evals_result = evals_result.reset_index().rename(columns={'index': 'iteration'})
full_evals_results = full_evals_results.append(evals_result)
full_evals_results = full_evals_results.melt(id_vars=['iteration']).rename(columns={'value': self.eval_metric,
'variable': 'dataset'})
full_evals_results[self.eval_metric] = np.abs(full_evals_results[self.eval_metric])
sns.lineplot(data=full_evals_results, x='iteration', y=self.eval_metric, hue='dataset')
plt.title('Training progress')
class MainTransformer(BaseEstimator, TransformerMixin):
def __init__(self, convert_cyclical: bool = False, create_interactions: bool = False, n_interactions: int = 20):
"""
Main transformer for the data. Can be used for processing on the whole data.
:param convert_cyclical: convert cyclical features into continuous
:param create_interactions: create interactions between features
"""
self.convert_cyclical = convert_cyclical
self.create_interactions = create_interactions
self.feats_for_interaction = None
self.n_interactions = n_interactions
def fit(self, X, y=None):
if self.create_interactions:
self.feats_for_interaction = [col for col in X.columns if 'sum' in col
or 'mean' in col or 'max' in col or 'std' in col
or 'attempt' in col]
self.feats_for_interaction1 = np.random.choice(self.feats_for_interaction, self.n_interactions)
self.feats_for_interaction2 = np.random.choice(self.feats_for_interaction, self.n_interactions)
return self
def transform(self, X, y=None):
data = copy.deepcopy(X)
if self.create_interactions:
for col1 in self.feats_for_interaction1:
for col2 in self.feats_for_interaction2:
data[f'{col1}_int_{col2}'] = data[col1] * data[col2]
if self.convert_cyclical:
data['timestampHour'] = np.sin(2 * np.pi * data['timestampHour'] / 23.0)
data['timestampMonth'] = np.sin(2 * np.pi * data['timestampMonth'] / 23.0)
data['timestampWeek'] = np.sin(2 * np.pi * data['timestampWeek'] / 23.0)
data['timestampMinute'] = np.sin(2 * np.pi * data['timestampMinute'] / 23.0)
return data
def fit_transform(self, X, y=None, **fit_params):
data = copy.deepcopy(X)
self.fit(data)
return self.transform(data)
class FeatureTransformer(BaseEstimator, TransformerMixin):
def __init__(self, main_cat_features: list = None, num_cols: list = None):
"""
:param main_cat_features:
:param num_cols:
"""
self.main_cat_features = main_cat_features
self.num_cols = num_cols
def fit(self, X, y=None):
self.num_cols = [col for col in X.columns if 'sum' in col or 'mean' in col or 'max' in col or 'std' in col
or 'attempt' in col]
return self
def transform(self, X, y=None):
data = copy.deepcopy(X)
return data
def fit_transform(self, X, y=None, **fit_params):
data = copy.deepcopy(X)
self.fit(data)
return self.transform(data)
class CategoricalTransformer(BaseEstimator, TransformerMixin):
def __init__(self, cat_cols=None, drop_original: bool = False, encoder=OrdinalEncoder()):
"""
Categorical transformer. This is a wrapper for categorical encoders.
:param cat_cols:
:param drop_original:
:param encoder:
"""
self.cat_cols = cat_cols
self.drop_original = drop_original
self.encoder = encoder
self.default_encoder = OrdinalEncoder()
def fit(self, X, y=None):
if self.cat_cols is None:
kinds = np.array([dt.kind for dt in X.dtypes])
is_cat = kinds == 'O'
self.cat_cols = list(X.columns[is_cat])
self.encoder.set_params(cols=self.cat_cols)
self.default_encoder.set_params(cols=self.cat_cols)
self.encoder.fit(X[self.cat_cols], y)
self.default_encoder.fit(X[self.cat_cols], y)
return self
def transform(self, X, y=None):
data = copy.deepcopy(X)
new_cat_names = [f'{col}_encoded' for col in self.cat_cols]
encoded_data = self.encoder.transform(data[self.cat_cols])
if encoded_data.shape[1] == len(self.cat_cols):
data[new_cat_names] = encoded_data
else:
pass
if self.drop_original:
data = data.drop(self.cat_cols, axis=1)
else:
data[self.cat_cols] = self.default_encoder.transform(data[self.cat_cols])
return data
def fit_transform(self, X, y=None, **fit_params):
data = copy.deepcopy(X)
self.fit(data)
return self.transform(data)
class RegressorModel(object):
"""
A wrapper class for classification models.
It can be used for training and prediction.
Can plot feature importance and training progress (if relevant for model).
"""
def __init__(self, columns: list = None, model_wrapper=None):
"""
:param original_columns:
:param model_wrapper:
"""
self.columns = columns
self.model_wrapper = model_wrapper
self.result_dict = {}
self.train_one_fold = False
self.preprocesser = None
def fit(self, X: pd.DataFrame, y,
X_holdout: pd.DataFrame = None, y_holdout=None,
folds=None,
params: dict = None,
eval_metric='rmse',
cols_to_drop: list = None,
preprocesser=None,
transformers: dict = None,
adversarial: bool = False,
plot: bool = True):
"""
Training the model.
:param X: training data
:param y: training target
:param X_holdout: holdout data
:param y_holdout: holdout target
:param folds: folds to split the data. If not defined, then model will be trained on the whole X
:param params: training parameters
:param eval_metric: metric for validataion
:param cols_to_drop: list of columns to drop (for example ID)
:param preprocesser: preprocesser class
:param transformers: transformer to use on folds
:param adversarial
:return:
"""
if folds is None:
folds = KFold(n_splits=3, random_state=42)
self.train_one_fold = True
self.columns = X.columns if self.columns is None else self.columns
self.feature_importances = pd.DataFrame(columns=['feature', 'importance'])
self.trained_transformers = {k: [] for k in transformers}
self.transformers = transformers
self.models = []
self.folds_dict = {}
self.eval_metric = eval_metric
n_target = 1
self.oof = np.zeros((len(X), n_target))
self.n_target = n_target
X = X[self.columns]
if X_holdout is not None:
X_holdout = X_holdout[self.columns]
if preprocesser is not None:
self.preprocesser = preprocesser
self.preprocesser.fit(X, y)
X = self.preprocesser.transform(X, y)
self.columns = X.columns.tolist()
if X_holdout is not None:
X_holdout = self.preprocesser.transform(X_holdout)
for fold_n, (train_index, valid_index) in enumerate(folds.split(X, y, X['game_session'])):
if X_holdout is not None:
X_hold = X_holdout.copy()
else:
X_hold = None
self.folds_dict[fold_n] = {}
if params['verbose']:
print(f'Fold {fold_n + 1} started at {time.ctime()}')
self.folds_dict[fold_n] = {}
X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
if self.train_one_fold:
X_train = X[self.columns]
y_train = y
X_valid = None
y_valid = None
datasets = {'X_train': X_train, 'X_valid': X_valid, 'X_holdout': X_hold, 'y_train': y_train}
X_train, X_valid, X_hold = self.transform_(datasets, cols_to_drop)
self.folds_dict[fold_n]['columns'] = X_train.columns.tolist()
model = copy.deepcopy(self.model_wrapper)
if adversarial:
X_new1 = X_train.copy()
if X_valid is not None:
X_new2 = X_valid.copy()
elif X_holdout is not None:
X_new2 = X_holdout.copy()
X_new = pd.concat([X_new1, X_new2], axis=0)
y_new = np.hstack((np.zeros((X_new1.shape[0])), np.ones((X_new2.shape[0]))))
X_train, X_valid, y_train, y_valid = train_test_split(X_new, y_new)
model.fit(X_train, y_train, X_valid, y_valid, X_hold, y_holdout, params=params)
self.folds_dict[fold_n]['scores'] = model.best_score_
if self.oof.shape[0] != len(X):
self.oof = np.zeros((X.shape[0], self.oof.shape[1]))
if not adversarial:
self.oof[valid_index] = model.predict(X_valid).reshape(-1, n_target)
fold_importance = pd.DataFrame(list(zip(X_train.columns, model.feature_importances_)),
columns=['feature', 'importance'])
self.feature_importances = self.feature_importances.append(fold_importance)
self.models.append(model)
self.feature_importances['importance'] = self.feature_importances['importance'].astype(int)
# if params['verbose']:
self.calc_scores_()
if plot:
# print(classification_report(y, self.oof.argmax(1)))
fig, ax = plt.subplots(figsize=(16, 12))
plt.subplot(2, 2, 1)
self.plot_feature_importance(top_n=20)
plt.subplot(2, 2, 2)
self.plot_metric()
plt.subplot(2, 2, 3)
plt.hist(y.values.reshape(-1, 1) - self.oof)
plt.title('Distribution of errors')
plt.subplot(2, 2, 4)
plt.hist(self.oof)
plt.title('Distribution of oof predictions');
def transform_(self, datasets, cols_to_drop):
for name, transformer in self.transformers.items():
transformer.fit(datasets['X_train'], datasets['y_train'])
datasets['X_train'] = transformer.transform(datasets['X_train'])
if datasets['X_valid'] is not None:
datasets['X_valid'] = transformer.transform(datasets['X_valid'])
if datasets['X_holdout'] is not None:
datasets['X_holdout'] = transformer.transform(datasets['X_holdout'])
self.trained_transformers[name].append(transformer)
if cols_to_drop is not None:
cols_to_drop = [col for col in cols_to_drop if col in datasets['X_train'].columns]
datasets['X_train'] = datasets['X_train'].drop(cols_to_drop, axis=1)
if datasets['X_valid'] is not None:
datasets['X_valid'] = datasets['X_valid'].drop(cols_to_drop, axis=1)
if datasets['X_holdout'] is not None:
datasets['X_holdout'] = datasets['X_holdout'].drop(cols_to_drop, axis=1)
self.cols_to_drop = cols_to_drop
return datasets['X_train'], datasets['X_valid'], datasets['X_holdout']
def calc_scores_(self):
datasets = [k for k, v in [v['scores'] for k, v in self.folds_dict.items()][0].items() if len(v) > 0]
self.scores = {}
for d in datasets:
scores = [v['scores'][d][self.eval_metric] for k, v in self.folds_dict.items()]
print(f"CV mean score on {d}: {np.mean(scores):.4f} +/- {np.std(scores):.4f} std.")
self.scores[d] = np.mean(scores)
def predict(self, X_test, averaging: str = 'usual'):
"""
Make prediction
:param X_test:
:param averaging: method of averaging
:return:
"""
full_prediction = np.zeros((X_test.shape[0], self.oof.shape[1]))
if self.preprocesser is not None:
X_test = self.preprocesser.transform(X_test)
for i in range(len(self.models)):
X_t = X_test.copy()
for name, transformers in self.trained_transformers.items():
X_t = transformers[i].transform(X_t)
if self.cols_to_drop is not None:
cols_to_drop = [col for col in self.cols_to_drop if col in X_t.columns]
X_t = X_t.drop(cols_to_drop, axis=1)
y_pred = self.models[i].predict(X_t[self.folds_dict[i]['columns']]).reshape(-1, full_prediction.shape[1])
# if case transformation changes the number of the rows
if full_prediction.shape[0] != len(y_pred):
full_prediction = np.zeros((y_pred.shape[0], self.oof.shape[1]))
if averaging == 'usual':
full_prediction += y_pred
elif averaging == 'rank':
full_prediction += pd.Series(y_pred).rank().values
return full_prediction / len(self.models)
def plot_feature_importance(self, drop_null_importance: bool = True, top_n: int = 10):
"""
Plot default feature importance.
:param drop_null_importance: drop columns with null feature importance
:param top_n: show top n columns
:return:
"""
top_feats = self.get_top_features(drop_null_importance, top_n)
feature_importances = self.feature_importances.loc[self.feature_importances['feature'].isin(top_feats)]
feature_importances['feature'] = feature_importances['feature'].astype(str)
top_feats = [str(i) for i in top_feats]
sns.barplot(data=feature_importances, x='importance', y='feature', orient='h', order=top_feats)
plt.title('Feature importances')
def get_top_features(self, drop_null_importance: bool = True, top_n: int = 10):
"""
Get top features by importance.
:param drop_null_importance:
:param top_n:
:return:
"""
grouped_feats = self.feature_importances.groupby(['feature'])['importance'].mean()
if drop_null_importance:
grouped_feats = grouped_feats[grouped_feats != 0]
return list(grouped_feats.sort_values(ascending=False).index)[:top_n]
def plot_metric(self):
"""
Plot training progress.
Inspired by `plot_metric` from https://lightgbm.readthedocs.io/en/latest/_modules/lightgbm/plotting.html
:return:
"""
full_evals_results = pd.DataFrame()
for model in self.models:
evals_result = pd.DataFrame()
for k in model.model.evals_result_.keys():
evals_result[k] = model.model.evals_result_[k][self.eval_metric]
evals_result = evals_result.reset_index().rename(columns={'index': 'iteration'})
full_evals_results = full_evals_results.append(evals_result)
full_evals_results = full_evals_results.melt(id_vars=['iteration']).rename(columns={'value': self.eval_metric,
'variable': 'dataset'})
sns.lineplot(data=full_evals_results, x='iteration', y=self.eval_metric, hue='dataset')
plt.title('Training progress')
def qwk(a1, a2):
"""
Source: https://www.kaggle.com/c/data-science-bowl-2019/discussion/114133#latest-660168
:param a1:
:param a2:
:param max_rat:
:return:
"""
max_rat = 3
a1 = np.asarray(a1, dtype=int)
a2 = np.asarray(a2, dtype=int)
hist1 = np.zeros((max_rat + 1, ))
hist2 = np.zeros((max_rat + 1, ))
o = 0
for k in range(a1.shape[0]):
i, j = a1[k], a2[k]
hist1[i] += 1
hist2[j] += 1
o += (i - j) * (i - j)
e = 0
for i in range(max_rat + 1):
for j in range(max_rat + 1):
e += hist1[i] * hist2[j] * (i - j) * (i - j)
e = e / a1.shape[0]
return 1 - o / e
def eval_qwk_lgb(y_true, y_pred):
"""
Fast cappa eval function for lgb.
"""
y_pred = y_pred.reshape(len(np.unique(y_true)), -1).argmax(axis=0)
return 'cappa', qwk(y_true, y_pred), True
def eval_qwk_lgb_regr(y_true, y_pred):
"""
Fast cappa eval function for lgb.
"""
y_pred[y_pred <= 1.12232214] = 0
y_pred[np.where(np.logical_and(y_pred > 1.12232214, y_pred <= 1.73925866))] = 1
y_pred[np.where(np.logical_and(y_pred > 1.73925866, y_pred <= 2.22506454))] = 2
y_pred[y_pred > 2.22506454] = 3
# y_pred = y_pred.reshape(len(np.unique(y_true)), -1).argmax(axis=0)
return 'cappa', qwk(y_true, y_pred), True
class LGBWrapper_regr(object):
"""
A wrapper for lightgbm model so that we will have a single api for various models.
"""
def __init__(self):
self.model = lgb.LGBMRegressor()
def fit(self, X_train, y_train, X_valid=None, y_valid=None, X_holdout=None, y_holdout=None, params=None):
if params['objective'] == 'regression':
eval_metric = eval_qwk_lgb_regr
else:
eval_metric = 'auc'
eval_set = [(X_train, y_train)]
eval_names = ['train']
self.model = self.model.set_params(**params)
if X_valid is not None:
eval_set.append((X_valid, y_valid))
eval_names.append('valid')
if X_holdout is not None:
eval_set.append((X_holdout, y_holdout))
eval_names.append('holdout')
if 'cat_cols' in params.keys():
cat_cols = [col for col in params['cat_cols'] if col in X_train.columns]
if len(cat_cols) > 0:
categorical_columns = params['cat_cols']
else:
categorical_columns = 'auto'
else:
categorical_columns = 'auto'
self.model.fit(X=X_train, y=y_train,
eval_set=eval_set, eval_names=eval_names, eval_metric=eval_metric,
verbose=params['verbose'], early_stopping_rounds=params['early_stopping_rounds'],
categorical_feature=categorical_columns)
self.best_score_ = self.model.best_score_
self.feature_importances_ = self.model.feature_importances_
def predict(self, X_test):
return self.model.predict(X_test, num_iteration=self.model.best_iteration_)
def eval_qwk_xgb(y_pred, y_true):
"""
Fast cappa eval function for xgb.
"""
y_true = y_true.get_label()
y_pred = y_pred.argmax(axis=1)
return 'cappa', -qwk(y_true, y_pred)
class LGBWrapper(object):
"""
A wrapper for lightgbm model so that we will have a single api for various models.
"""
def __init__(self):
self.model = lgb.LGBMClassifier()
def fit(self, X_train, y_train, X_valid=None, y_valid=None, X_holdout=None, y_holdout=None, params=None):
eval_set = [(X_train, y_train)]
eval_names = ['train']
self.model = self.model.set_params(**params)
if X_valid is not None:
eval_set.append((X_valid, y_valid))
eval_names.append('valid')
if X_holdout is not None:
eval_set.append((X_holdout, y_holdout))
eval_names.append('holdout')
if 'cat_cols' in params.keys():
cat_cols = [col for col in params['cat_cols'] if col in X_train.columns]
if len(cat_cols) > 0:
categorical_columns = params['cat_cols']
else:
categorical_columns = 'auto'
else:
categorical_columns = 'auto'
self.model.fit(X=X_train, y=y_train,
eval_set=eval_set, eval_names=eval_names, eval_metric=eval_qwk_lgb,
verbose=params['verbose'], early_stopping_rounds=params['early_stopping_rounds'],
categorical_feature=categorical_columns)
self.best_score_ = self.model.best_score_
self.feature_importances_ = self.model.feature_importances_
def predict_proba(self, X_test):
if self.model.objective == 'binary':
return self.model.predict_proba(X_test, num_iteration=self.model.best_iteration_)[:, 1]
else:
return self.model.predict_proba(X_test, num_iteration=self.model.best_iteration_)
class CatWrapper(object):
"""
A wrapper for catboost model so that we will have a single api for various models.
"""
def __init__(self):
self.model = cat.CatBoostClassifier()
def fit(self, X_train, y_train, X_valid=None, y_valid=None, X_holdout=None, y_holdout=None, params=None):
eval_set = [(X_train, y_train)]
self.model = self.model.set_params(**{k: v for k, v in params.items() if k != 'cat_cols'})
if X_valid is not None:
eval_set.append((X_valid, y_valid))
if X_holdout is not None:
eval_set.append((X_holdout, y_holdout))
if 'cat_cols' in params.keys():
cat_cols = [col for col in params['cat_cols'] if col in X_train.columns]
if len(cat_cols) > 0:
categorical_columns = params['cat_cols']
else:
categorical_columns = None
else:
categorical_columns = None
self.model.fit(X=X_train, y=y_train,
eval_set=eval_set,
verbose=params['verbose'], early_stopping_rounds=params['early_stopping_rounds'],
cat_features=categorical_columns)
self.best_score_ = self.model.best_score_
self.feature_importances_ = self.model.feature_importances_
def predict_proba(self, X_test):
if 'MultiClass' not in self.model.get_param('loss_function'):
return self.model.predict_proba(X_test, ntree_end=self.model.best_iteration_)[:, 1]
else:
return self.model.predict_proba(X_test, ntree_end=self.model.best_iteration_)
class XGBWrapper(object):
"""
A wrapper for xgboost model so that we will have a single api for various models.
"""
def __init__(self):
self.model = xgb.XGBClassifier()
def fit(self, X_train, y_train, X_valid=None, y_valid=None, X_holdout=None, y_holdout=None, params=None):
eval_set = [(X_train, y_train)]
self.model = self.model.set_params(**params)
if X_valid is not None:
eval_set.append((X_valid, y_valid))
if X_holdout is not None:
eval_set.append((X_holdout, y_holdout))
self.model.fit(X=X_train, y=y_train,
eval_set=eval_set, eval_metric=eval_qwk_xgb,
verbose=params['verbose'], early_stopping_rounds=params['early_stopping_rounds'])
scores = self.model.evals_result()
self.best_score_ = {k: {m: m_v[-1] for m, m_v in v.items()} for k, v in scores.items()}
self.best_score_ = {k: {m: n if m != 'cappa' else -n for m, n in v.items()} for k, v in self.best_score_.items()}
self.feature_importances_ = self.model.feature_importances_
def predict_proba(self, X_test):
if self.model.objective == 'binary':
return self.model.predict_proba(X_test, ntree_limit=self.model.best_iteration)[:, 1]
else:
return self.model.predict_proba(X_test, ntree_limit=self.model.best_iteration)
class MainTransformer(BaseEstimator, TransformerMixin):
def __init__(self, convert_cyclical: bool = False, create_interactions: bool = False, n_interactions: int = 20):
"""
Main transformer for the data. Can be used for processing on the whole data.
:param convert_cyclical: convert cyclical features into continuous
:param create_interactions: create interactions between features
"""
self.convert_cyclical = convert_cyclical
self.create_interactions = create_interactions
self.feats_for_interaction = None
self.n_interactions = n_interactions
def fit(self, X, y=None):
if self.create_interactions:
self.feats_for_interaction = [col for col in X.columns if 'sum' in col
or 'mean' in col or 'max' in col or 'std' in col
or 'attempt' in col]
self.feats_for_interaction1 = np.random.choice(self.feats_for_interaction, self.n_interactions)
self.feats_for_interaction2 = np.random.choice(self.feats_for_interaction, self.n_interactions)
return self
def transform(self, X, y=None):
data = copy.deepcopy(X)
if self.create_interactions:
for col1 in self.feats_for_interaction1:
for col2 in self.feats_for_interaction2:
data[f'{col1}_int_{col2}'] = data[col1] * data[col2]
if self.convert_cyclical:
data['timestampHour'] = np.sin(2 * np.pi * data['hour'] / 23.0)
return data
def fit_transform(self, X, y=None, **fit_params):
data = copy.deepcopy(X)
self.fit(data)
return self.transform(data)
class FeatureTransformer(BaseEstimator, TransformerMixin):
def __init__(self, main_cat_features: list = None, num_cols: list = None):
"""
:param main_cat_features:
:param num_cols:
"""
self.main_cat_features = main_cat_features
self.num_cols = num_cols
def fit(self, X, y=None):
# self.num_cols = [col for col in X.columns if 'sum' in col or 'mean' in col or 'max' in col or 'std' in col
# or 'attempt' in col]
return self
def transform(self, X, y=None):
data = copy.deepcopy(X)
return data
def fit_transform(self, X, y=None, **fit_params):
data = copy.deepcopy(X)
self.fit(data)
return self.transform(data)
reduced = pd.read_csv('data/reduced_trainFull.csv')
Target = reduced['Target']
reduced = reduced.drop(['Target'], axis=1)