-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathcoresk.py
More file actions
1144 lines (994 loc) · 51.5 KB
/
coresk.py
File metadata and controls
1144 lines (994 loc) · 51.5 KB
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
"""Scikit-learn compatible API for stacking.
Find out how to use:
>>> from vecstack import StackingTransformer
>>> help(StackingTransformer)
MIT License
Copyright (c) 2016-2025 Igor Ivanov
Email: vecxoz@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
from __future__ import print_function
from __future__ import division
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
import warnings
from contextlib import suppress
import numpy as np
import scipy.stats as st
from sklearn.base import BaseEstimator
from sklearn.base import TransformerMixin
from sklearn.base import clone
from sklearn.dummy import DummyClassifier
from sklearn.dummy import DummyRegressor
from sklearn.utils.validation import check_X_y
from sklearn.utils.validation import check_array
from sklearn.utils.validation import check_is_fitted
from sklearn.utils.validation import has_fit_parameter
from sklearn.utils.validation import validate_data
from sklearn.model_selection import KFold
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import accuracy_score
from sklearn.metrics import log_loss
from sklearn.metrics import mean_squared_error
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
class StackingTransformer(TransformerMixin, BaseEstimator):
"""StackingTransformer. Scikit-learn compatible API for stacking.
Parameters
----------
estimators : list of tuples, default None
Base level estimators.
If None then by default:
DummyRegressor (predicts constant 5.5) - for regression task
DummyClassifier (predicts constant 1) - for classification task
You can use any sklearn-like estimators.
Each tuple in the list contains arbitrary
unique name and estimator object, e.g.:
estimators = [('lr', LinearRegression()),
('ridge', Ridge(random_state=0))]
Note. Dropping individual estimators using 'drop' is not supported.
Note. According to sklearn convention for binary classification
task with probabilities estimator must return probabilities
for each class (i.e. two columns).
regression : boolean, default True
If True - perform stacking for regression task,
if False - perform stacking for classification task
transform_target : callable, default None
Function to transform target variable.
If None - transformation is not used.
For example, for regression task (if target variable is skewed)
you can use transformation like ``numpy.log1p``.
Set ``transform_target=numpy.log1p``
Usually you want to use respective backward transformation
for prediction like ``numpy.expm1``.
Set ``transform_pred=numpy.expm1``
Caution! Some transformations may give inapplicable results.
For example, if target variable contains zeros, numpy.log
gives you -inf. In such case you can use appropriate
transformation like ``numpy.log1p`` and respective
backward transformation like ``numpy.expm1``
transform_pred : callable, default None
Function to transform prediction.
If None - transformation is not used.
If you use transformation for target variable (``transform_target``)
like ``numpy.log1p``, then using ``transform_pred`` you can specify
respective backward transformation like ``numpy.expm1``.
Look at description of parameter ``transform_target``
variant: str, default 'A'
Possible values: 'A', 'B'.
This parameter influences test set predictions only.
Variant 'A' - predict test set in each fold and find mean (mode)
Variant 'B' - fit on full train set and predict test set once
See tutorial for details:
https://github.com/vecxoz/vecstack/blob/master/ \
examples/00_stacking_concept_pictures_code.ipynb
needs_proba: boolean, default False, meaningful only for classification
Whether to predict probabilities (instead of class labels)
in classification task.
Ignored if ``regression=True``
metric : callable, default None
Evaluation metric (score function) which is used to calculate
cross-validation scores.
If None, then by default:
sklearn.metrics.mean_absolute_error - for regression
sklearn.metrics.accuracy_score - for classification
with class labels
sklearn.metrics.log_loss - for classification with probabilities
You can use any appropriate sklearn metric or
define your own metric like shown below:
def your_metric(y_true, y_pred):
# calculate
return result
n_folds : int, default 4
Number of folds in cross-validation
stratified : boolean, default False, meaningful only for classification
If True - use stratified folds in cross-validation
Ignored if ``regression=True``
shuffle : boolean, default False
Whether to perform a shuffle before cross-validation split
random_state : int, default 0
Random seed used to initiate fold split.
Same seed and correspondingly same split is used for all estimators.
Ignored if ``shuffle=False``
verbose : int, default 0
Level of verbosity.
0 - show no messages
1 - for each estimator show mean score
2 - for each estimator show score for each fold and mean score
Attributes
----------
estimators_ : list
List of base estimators (not fitted) passed by user (or default)
n_estimators_ : int
Number of base estimators passed by user (or default)
n_classes_ : int
Number of classes in classification task.
``None`` in regression task.
models_A_ : list of lists
List containing n_estimators lists. Each of which contains
n_folds models (fitted estimators).
In variant A this models are used to transform (predict)
both train set and test set
In variant B this models are used to transform (predict)
train set only
models_B_ : list or None
List containing n_estimators models (fitted estimators).
In variant A this attribute is None
In variant B this models are used to transform (predict) test set
metric_ : callable
Metric passed by user (or default) which was used
to compute cross-validation scores
kf_ : KFold or StratifiedKFold object
Initialized cross-validation object which was used to split train set
during fitting
scores_ : 2d numpy array of shape [n_estimators, n_folds]
Scores for each fold for each estimator.
mean_std_ : list of tuples
Each tuple contains name, mean and std for each estimator.
train_shape_ : tuple of ints
Shape of training data
n_train_examples_ : int
Number of training examples
n_features_: int
Number of features
train_footprint_ : list of tuples
Train set footprint which is used to identify train set
during transform (predict) phase.
Each tuple containes 3 values: row index, column index, data value
Examples
--------
>>> from sklearn.datasets import fetch_california_housing
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.metrics import mean_absolute_error
>>> from sklearn.ensemble import ExtraTreesRegressor, RandomForestRegressor
>>> from xgboost import XGBRegressor
>>> from vecstack import StackingTransformer
>>>
>>> # Load demo data
>>> X, y = fetch_california_housing(return_X_y=True)
>>>
>>> # Make train/test split
>>> X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2,
random_state=0)
>>>
>>> # Init 1st level estimators
>>> estimators_L1 = [('et', ExtraTreesRegressor(random_state=0,
n_jobs=-1,
n_estimators=100,
max_depth=3)),
('rf', RandomForestRegressor(random_state=0,
n_jobs=-1,
n_estimators=100,
max_depth=3)),
('xgb', XGBRegressor(random_state=0,
n_jobs=-1,
learning_rate=0.1,
n_estimators=100,
max_depth=3))]
>>> # Stacking
>>> stack = StackingTransformer(estimators=estimators_L1,
regression=True,
shuffle=True,
random_state=0,
verbose=2)
>>> stack = stack.fit(X_train, y_train)
>>> S_train = stack.transform(X_train)
>>> S_test = stack.transform(X_test)
>>>
>>> # Use 2nd level estimator to get final prediction
>>> estimator_L2 = XGBRegressor(random_state=0,
n_jobs=-1,
learning_rate=0.1,
n_estimators=100,
max_depth=3)
>>> estimator_L2 = estimator_L2.fit(S_train, y_train)
>>> y_pred = estimator_L2.predict(S_test)
>>>
>>> # Final prediction score
>>> print('Final score: [%.8f]' % mean_absolute_error(y_test, y_pred))
"""
def __init__(self,
estimators=None,
regression=True,
transform_target=None,
transform_pred=None,
variant='A',
needs_proba=False,
metric=None,
n_folds=4,
stratified=False,
shuffle=False,
random_state=0,
verbose=0):
self.estimators = estimators
self.regression = regression
self.transform_target = transform_target
self.transform_pred = transform_pred
self.variant = variant
self.needs_proba = needs_proba
self.metric = metric
self.n_folds = n_folds
self.stratified = stratified
self.shuffle = shuffle
self.random_state = random_state
self.verbose = verbose
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
def __sklearn_tags__(self):
tags = super().__sklearn_tags__()
tags.estimator_type = 'transformer'
tags.transformer_tags.preserves_dtype = []
tags.target_tags.required = True
tags.input_tags.sparse = True
return tags
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
def fit(self, X, y, sample_weight=None):
"""Fit all base estimators.
Parameters
----------
X : 2d numpy array or sparse matrix of shape [n_samples, n_features]
Training data
y : 1d numpy array of shape [n_samples]
Target values.
sample_weight : 1d numpy array of shape [n_samples]
Individual weights for each sample.
Passed to fit method of each estimator.
Note: will be split automatically for each fold.
Returns
-------
self : object
Fitted StackingTransformer instance.
"""
# ---------------------------------------------------------------------
# Validation
# ---------------------------------------------------------------------
# ---------------------------------------------------------------------
# Check input data
# ---------------------------------------------------------------------
# Check data and set `self.n_features_in_` and `self.feature_names_in_`
X, y = validate_data(self, X, y,
reset=True, # default: True, if True will set `self.n_features_in_` and `self.feature_names_in_`
validate_separately=False, # default: False, if False will use `check_X_y`
skip_check_array=False, # default: False, if False will NOT skip checks
accept_sparse=['csr'],
ensure_all_finite=True,
multi_output=False)
# Legacy check included in `validate_data`
# Check X and y
# ``check_estimator`` does not allow ``ensure_all_finite=False``
# X, y = check_X_y(X, y,
# accept_sparse=['csr'], # allow csr, cast all others to csr
# ensure_all_finite=True, # do not allow nan and inf
# multi_output=False) # allow only one column in y_train
# Check X and sample_weight
# X is alredy checked, but we need it to compare length of sample_weight
if sample_weight is not None:
X, sample_weight = check_X_y(X, sample_weight,
accept_sparse=['csr'],
ensure_all_finite=True,
multi_output=False)
# ---------------------------------------------------------------------
# Check ``estimators``
# ---------------------------------------------------------------------
if self.estimators is None:
if self.regression:
self.estimators_ = [('dumregr', DummyRegressor(strategy='constant', constant=5.5))]
else:
self.estimators_ = [('dumclf', DummyClassifier(strategy='constant', constant=1))]
# warnings.warn('No estimators were specified. '
# 'Using single dummy estimator as demo.', UserWarning)
else:
if 0 == len(self.estimators):
raise ValueError('List of estimators is empty')
else:
# Clone
self.estimators_ = [(name, clone(estim)) for name, estim in self.estimators]
# Check names of estimators
names, estims = zip(*self.estimators_)
self._validate_names(names)
# Check if all estimators support ``sample_weight``
if sample_weight is not None:
for name, estim in self.estimators_:
if not has_fit_parameter(estim, 'sample_weight'):
raise ValueError('Underlying estimator [%s] does not '
'support sample weights.' % name)
# ---------------------------------------------------------------------
# Check other StackingTransformer parameters
# ---------------------------------------------------------------------
# ``variant``
if self.variant not in ['A', 'B']:
raise ValueError('Parameter ``variant`` must be set properly')
# ``n_folds``
if not isinstance(self.n_folds, int):
raise ValueError('Parameter ``n_folds`` must be integer')
if not self.n_folds > 1:
raise ValueError('Parameter ``n_folds`` must be not less than 2')
# ``verbose``
if self.verbose not in [0, 1, 2]:
raise ValueError('Parameter ``verbose`` must be 0, 1, or 2')
# Additional check for inapplicable parameter combinations
# If ``regression=True`` we ignore classification-specific
# parameters and issue user warning
if self.regression and (self.needs_proba or self.stratified):
warn_str = ('This is regression task hence classification-specific'
'parameters set to ``True`` were ignored:')
if self.needs_proba:
self.needs_proba = False
warn_str += ' ``needs_proba``'
if self.stratified:
self.stratified = False
warn_str += ' ``stratified``'
warnings.warn(warn_str, UserWarning)
# To comply with sklearn requirement
if not self.shuffle:
random_state_internal = None
else:
random_state_internal = self.random_state
# ---------------------------------------------------------------------
# Compute attributes (basic properties of data, number of estimators, etc.)
# ---------------------------------------------------------------------
self.train_shape_ = X.shape
self.n_train_examples_ = X.shape[0]
self.n_features_ = X.shape[1]
if not self.regression:
self.n_classes_ = len(np.unique(y))
else:
self.n_classes_ = None
self.n_estimators_ = len(self.estimators_)
self.train_footprint_ = self._get_footprint(X)
# ---------------------------------------------------------------------
# Specify default metric
# ---------------------------------------------------------------------
if self.metric is None and self.regression:
self.metric_ = mean_absolute_error
elif self.metric is None and not self.regression:
if self.needs_proba:
self.metric_ = log_loss
else:
self.metric_ = accuracy_score
else:
self.metric_ = self.metric
# ---------------------------------------------------------------------
# Create report header strings and print report header
# ---------------------------------------------------------------------
if self.verbose > 0:
if self.regression:
task_str = 'task: [regression]'
else:
task_str = 'task: [classification]'
n_classes_str = 'n_classes: [%d]' % self.n_classes_
metric_str = 'metric: [%s]' % self.metric_.__name__
variant_str = 'variant: [%s]' % self.variant
n_estimators_str = 'n_estimators: [%d]' % self.n_estimators_
print(task_str)
if not self.regression:
print(n_classes_str)
print(metric_str)
print(variant_str)
print(n_estimators_str + '\n')
# ---------------------------------------------------------------------
# Initialize cross-validation split
# Stratified can be used only for classification
# ---------------------------------------------------------------------
if not self.regression and self.stratified:
self.kf_ = StratifiedKFold(n_splits=self.n_folds,
shuffle=self.shuffle,
random_state=random_state_internal)
# Save target to be able to create stratified split in ``transform`` method
# This is more efficient than to save split indices
self._y_ = y.copy()
else:
self.kf_ = KFold(n_splits=self.n_folds,
shuffle=self.shuffle,
random_state=random_state_internal)
self._y_ = None
# ---------------------------------------------------------------------
# Compute implicit number of classes to create appropriate empty arrays.
# !!! Important. In order to unify array creation
# variable ``n_classes_implicit_`` is always equal to 1, except the case
# when we performing classification task with ``needs_proba=True``
# ---------------------------------------------------------------------
if not self.regression and self.needs_proba:
self.n_classes_implicit_ = len(np.unique(y))
self.action_ = 'predict_proba'
else:
self.n_classes_implicit_ = 1
self.action_ = 'predict'
# ---------------------------------------------------------------------
# Create empty numpy array for train predictions (OOF)
# !!! Important. We have to implicitly predict during fit
# in order to compute CV scores, because
# the most reasonable place to print out CV scores is fit method
# ---------------------------------------------------------------------
S_train = np.zeros((X.shape[0], self.n_estimators_ * self.n_classes_implicit_))
# ---------------------------------------------------------------------
# Prepare (clone) estmators for fitting and storing
# We need models_A_ for both variant A and varian B
# We need models_B_ for varian B only (in variant A attribute models_B_ is None)
# ---------------------------------------------------------------------
self.models_A_ = []
self.models_B_ = None
for n, est in self.estimators_:
self.models_A_.append([clone(est) for _ in range(self.n_folds)])
if self.variant in ['B']:
self.models_B_ = [clone(est) for n, est in self.estimators_]
# ---------------------------------------------------------------------
# Create empty numpy array to store scores for each estimator and each fold
# ---------------------------------------------------------------------
self.scores_ = np.zeros((self.n_estimators_, self.n_folds))
# ---------------------------------------------------------------------
# Create empty list to store name, mean and std for each estimator
# ---------------------------------------------------------------------
self.mean_std_ = []
# ---------------------------------------------------------------------
# MAIN FIT PROCEDURE
# ---------------------------------------------------------------------
# Loop across estimators
# ---------------------------------------------------------------------
for estimator_counter, (name, estimator) in enumerate(self.estimators_):
if self.verbose > 0:
estimator_str = 'estimator %2d: [%s: %s]' % (estimator_counter, name, estimator.__class__.__name__)
print(estimator_str)
# -----------------------------------------------------------------
# Loop across folds
# -----------------------------------------------------------------
for fold_counter, (tr_index, te_index) in enumerate(self.kf_.split(X, y)):
# Split data and target
X_tr = X[tr_index]
y_tr = y[tr_index]
X_te = X[te_index]
y_te = y[te_index]
# Split sample weights accordingly (if passed)
if sample_weight is not None:
sample_weight_tr = sample_weight[tr_index]
# sample_weight_te = sample_weight[te_index]
else:
sample_weight_tr = None
# sample_weight_te = None
# Fit estimator
_ = self._estimator_action(self.models_A_[estimator_counter][fold_counter],
X_tr, y_tr, None,
sample_weight=sample_weight_tr,
action='fit',
transform=self.transform_target)
# Predict out-of-fold part of train set
if 'predict_proba' == self.action_:
col_slice_estimator = slice(estimator_counter * self.n_classes_implicit_,
estimator_counter * self.n_classes_implicit_ + self.n_classes_implicit_)
else:
col_slice_estimator = estimator_counter
S_train[te_index, col_slice_estimator] = self._estimator_action(self.models_A_[estimator_counter][fold_counter],
None, None,
X_te, action=self.action_,
transform=self.transform_pred)
# Compute score
score = self.metric_(y_te, S_train[te_index, col_slice_estimator])
self.scores_[estimator_counter, fold_counter] = score
# Print fold score
if self.verbose > 1:
fold_str = ' fold %2d: [%.8f]' % (fold_counter, score)
print(fold_str)
# Compute mean and std and save in dict
estim_name = self.estimators_[estimator_counter][0]
estim_mean = np.mean(self.scores_[estimator_counter])
estim_std = np.std(self.scores_[estimator_counter])
self.mean_std_.append((estim_name, estim_mean, estim_std))
if self.verbose > 1:
sep_str = ' ----'
print(sep_str)
# Compute mean + std (and full)
if self.verbose > 0:
mean_str = ' MEAN: [%.8f] + [%.8f]\n' % (estim_mean, estim_std)
print(mean_str)
# Fit estimator on full train set
if self.variant in ['B']:
if self.verbose > 0:
print(' Fitting on full train set...\n')
_ = self._estimator_action(self.models_B_[estimator_counter],
X, y, None,
sample_weight=sample_weight,
action='fit',
transform=self.transform_target)
# ---------------------------------------------------------------------
# ---------------------------------------------------------------------
# Return fitted StackingTransformer instance
return self
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
def fit_transform(self, X, y, sample_weight=None):
"""Fit all base estimators and transform (predict) train set.
Parameters
----------
See docs for ``fit`` and ``transform`` methods.
Returns
-------
X_transformed : 2d numpy array of shape [n_samples, n_estimators] or
[n_samples, n_estimators * n_classes]
Out-of-fold predictions (OOF) for train set.
This is stacked features for next level.
"""
# ---------------------------------------------------------------------
# All validation and procedures are done inside corresponding methods
# fit and transform
# ---------------------------------------------------------------------
return self.fit(X, y, sample_weight).transform(X)
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
def transform(self, X, is_train_set=None):
"""Transform (predict) given data set.
If ``X`` is train set:
for each estimator return out-of-fold predictions (OOF).
If ``X`` is any other set:
variant A: for each estimator return mean (mode) of predictions
made in each fold
variant B: for each estimator return single prediction
Parameters
----------
X : 2d numpy array or sparse matrix of shape [n_samples, n_features]
Input data
is_train_set : boolean, default None
Fallback parameter. In general case
should not be used (should be None).
Gives ability to explicitly specify that given dataset
is train set or other set.
Returns
-------
X_transformed : 2d numpy array of shape [n_samples, n_estimators] or
[n_samples, n_estimators * n_classes]
Out-of-fold predictions (OOF) for train set.
Regular or bagged predictions for any other set.
This is stacked features for next level.
"""
# Check if fitted
check_is_fitted(self, ['models_A_'])
# Check data without resetting `self.n_features_in_` and `self.feature_names_in_`
X = validate_data(self, X,
reset=False, # default: True, if True will set `self.n_features_in_` and `self.feature_names_in_`
validate_separately=False, # default: False, if False will use `check_X_y`
skip_check_array=False, # default: False, if False will NOT skip checks
accept_sparse=['csr'],
ensure_all_finite=True) # no need for `multi_output`, because no `y`
# Legacy check included in `validate_data`
# Input validation
# ``check_estimator`` does not allow ``ensure_all_finite=False``
# X = check_array(X, accept_sparse=['csr'], ensure_all_finite=True)
# *********************************************************************
# Fitted StackingTransformer instance is bound to train set used for fitting.
# So during transformation we have different actions for train set
# and all other sets
# *********************************************************************
if is_train_set is None:
is_train_set = self._check_identity(X)
# Print
if self.verbose > 0:
if is_train_set:
print('Train set was detected.')
print('Transforming...\n')
# *********************************************************************
# Transform train set
# *********************************************************************
if is_train_set:
# In case if user directly tells that it is train set but shape is different
if self.train_shape_ != X.shape:
raise ValueError('Train set must have the same shape '
'in order to be transformed.')
# Create empty numpy array for train predictions (OOF)
S_train = np.zeros((X.shape[0], self.n_estimators_ * self.n_classes_implicit_))
# -----------------------------------------------------------------
# MAIN TRANSFORM (PREDICT) PROCEDURE for train set
# -----------------------------------------------------------------
# Loop across estimators
# -----------------------------------------------------------------
for estimator_counter, (name, estimator) in enumerate(self.estimators_):
if self.verbose > 0:
estimator_str = 'estimator %2d: [%s: %s]' % (estimator_counter, name, estimator.__class__.__name__)
print(estimator_str)
# -------------------------------------------------------------
# Loop across folds
# -------------------------------------------------------------
for fold_counter, (tr_index, te_index) in enumerate(self.kf_.split(X, self._y_)):
# Split data
# X_tr = X[tr_index]
X_te = X[te_index]
# Predict out-of-fold part of train set
if 'predict_proba' == self.action_:
col_slice_estimator = slice(estimator_counter * self.n_classes_implicit_,
estimator_counter * self.n_classes_implicit_ + self.n_classes_implicit_)
else:
col_slice_estimator = estimator_counter
S_train[te_index, col_slice_estimator] = self._estimator_action(self.models_A_[estimator_counter][fold_counter],
None, None,
X_te, action=self.action_,
transform=self.transform_pred)
# Print
if self.verbose > 1:
fold_str = ' model from fold %2d: done' % fold_counter
print(fold_str)
if self.verbose > 1:
sep_str = ' ----'
print(sep_str)
if self.verbose > 0:
done_str = ' DONE\n'
print(done_str)
# -----------------------------------------------------------------
# Cast class labels to int
# -----------------------------------------------------------------
if not self.regression and not self.needs_proba:
S_train = S_train.astype(int)
# Return transformed data (OOF)
return S_train # X_transformed
# *********************************************************************
# Transform any other set
# *********************************************************************
else:
# Legacy check included in `validate_data`
# Check n_features
# if X.shape[1] != self.n_features_:
# raise ValueError('Inconsistent number of features.')
# Create empty numpy array for test predictions
S_test = np.zeros((X.shape[0], self.n_estimators_ * self.n_classes_implicit_))
# ---------------------------------------------------------------------
# MAIN TRANSFORM (PREDICT) PROCEDURE for any other set
# -----------------------------------------------------------------
# Loop across estimators
# -----------------------------------------------------------------
for estimator_counter, (name, estimator) in enumerate(self.estimators_):
if self.verbose > 0:
estimator_str = 'estimator %2d: [%s: %s]' % (estimator_counter, name, estimator.__class__.__name__)
print(estimator_str)
# -------------------------------------------------------------
# Variant A
# -------------------------------------------------------------
if self.variant in ['A']:
# Create empty numpy array, which will contain temporary predictions
# for test set made in each fold
S_test_temp = np.zeros((X.shape[0], self.n_folds * self.n_classes_implicit_))
# ---------------------------------------------------------
# Loop across fitted models (it is the same as loop across folds)
# ---------------------------------------------------------
for fold_counter, model in enumerate(self.models_A_[estimator_counter]):
# Predict test set in each fold
if 'predict_proba' == self.action_:
col_slice_fold = slice(fold_counter * self.n_classes_implicit_,
fold_counter * self.n_classes_implicit_ + self.n_classes_implicit_)
else:
col_slice_fold = fold_counter
S_test_temp[:, col_slice_fold] = self._estimator_action(model, None, None, X,
action=self.action_,
transform=self.transform_pred)
# Print
if self.verbose > 1:
fold_str = ' model from fold %2d: done' % fold_counter
print(fold_str)
if self.verbose > 1:
sep_str = ' ----'
print(sep_str)
# ---------------------------------------------------------
# Compute mean or mode (majority voting) of predictions for test set
# ---------------------------------------------------------
if 'predict_proba' == self.action_:
# Here we copute means of probabilirties for each class
for class_id in range(self.n_classes_implicit_):
S_test[:, estimator_counter * self.n_classes_implicit_ + class_id] = np.mean(S_test_temp[:, class_id::self.n_classes_implicit_], axis=1)
else:
if self.regression:
S_test[:, estimator_counter] = np.mean(S_test_temp, axis=1)
else:
S_test[:, estimator_counter] = st.mode(S_test_temp, axis=1)[0].ravel()
if self.verbose > 0:
done_str = ' DONE\n'
print(done_str)
# -------------------------------------------------------------
# Variant B
# -------------------------------------------------------------
else:
if 'predict_proba' == self.action_:
col_slice_estimator = slice(estimator_counter * self.n_classes_implicit_,
estimator_counter * self.n_classes_implicit_ + self.n_classes_implicit_)
else:
col_slice_estimator = estimator_counter
S_test[:, col_slice_estimator] = self._estimator_action(self.models_B_[estimator_counter],
None, None, X,
action=self.action_,
transform=self.transform_pred)
if self.verbose > 0:
done_str = ' DONE\n'
print(done_str)
# ---------------------------------------------------------------------
# Cast class labels to int
# ---------------------------------------------------------------------
if not self.regression and not self.needs_proba:
S_test = S_test.astype(int)
return S_test # X_transformed
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
def _transformer(self, y, func=None):
"""Transforms target variable and prediction
"""
if func is None:
return y
else:
return func(y)
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
def _estimator_action(self, estimator, X_train, y_train, X_test,
sample_weight=None, action=None,
transform=None):
"""Performs estimator action.
This wrapper gives us ability to choose action dynamically
(e.g. ``predict`` or ``predict_proba``).
Note. Through ``_estimator_action`` and then through ``_transformer``
we apply ``transform_target`` and ``transform_pred`` functions if
given by user on the target and prediction in each fold separately
to be able to calculate proper scores.
"""
if 'fit' == action:
# We use following condition, because some estimators (e.g. Lars)
# may not have ``sample_weight`` parameter of ``fit`` method
if sample_weight is not None:
return estimator.fit(X_train, self._transformer(y_train, func=transform),
sample_weight=sample_weight)
else:
return estimator.fit(X_train, self._transformer(y_train, func=transform))
elif 'predict' == action:
return self._transformer(estimator.predict(X_test), func=transform)
elif 'predict_proba' == action:
return self._transformer(estimator.predict_proba(X_test), func=transform)
else:
raise ValueError('Parameter action must be set properly')
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
def _random_choice(self, n, size, bound=2**30):
"""
Memory efficient substitute for np.random.choice without replacement
Parameters:
===========
n : int
Upper value for range to chose from: [0, n).
This parameter is bounded (see bound).
size: int
Number of values to chose
bound : int
Upper random int for backward compatibility
with some older numpy versions
Returns:
========
ids : 1d numpy array of shape (size, ) dtype=np.int32
"""
try:
if n < size:
raise ValueError('Drawing without replacement: '
'``n`` cannot be less than ``size``')
ids = []
while len(ids) < size:
rnd = np.random.randint(min(bound, n))
if rnd not in ids:
ids.append(rnd)
return np.array(ids, dtype=np.int32)
except Exception:
raise ValueError('Internal error. '
'Please save traceback and inform developers.')
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
def _get_footprint(self, X, n_items=1000):
"""Selects ``n_items`` random elements from 2d numpy array or
sparse matrix (or all elements if their number is less or equal
to ``n_items``).
"""
try:
footprint = []
r, c = X.shape
n = r * c
# np.random.seed(0) # for development
# OOM with large arrays (see #29)
# ids = np.random.choice(n, min(n_items, n), replace=False)
ids = self._random_choice(n, min(n_items, n))
for i in ids:
row = i // c
col = i - row * c
footprint.append((row, col, X[row, col]))
return footprint
except Exception:
raise ValueError('Internal error. '
'Please save traceback and inform developers.')
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
def _check_identity(self, X,
rtol=1e-05, atol=1e-08,
equal_nan=False):
"""Checks 2d numpy array or sparse matrix identity
by its shape and footprint.
"""
try:
# Check shape
if X.shape != self.train_shape_:
return False
# Check footprint
try:
for coo in self.train_footprint_:
assert np.isclose(X[coo[0], coo[1]], coo[2], rtol=rtol, atol=atol, equal_nan=equal_nan)
return True
except AssertionError:
return False
except Exception:
raise ValueError('Internal error. '
'Please save traceback and inform developers.')
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
def _get_params(self, attr, deep=True):
"""
Gives ability to get parameters of nested estimators
"""
out = super().get_params(deep=deep)
if not deep:
return out
estimators = getattr(self, attr)
try:
out.update(estimators)
except (TypeError, ValueError):
# Ignore TypeError for cases where estimators is not a list of