-
-
Notifications
You must be signed in to change notification settings - Fork 25.4k
/
_gb.py
1846 lines (1550 loc) · 70.4 KB
/
_gb.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
"""Gradient Boosted Regression Trees.
This module contains methods for fitting gradient boosted regression trees for
both classification and regression.
The module structure is the following:
- The ``BaseGradientBoosting`` base class implements a common ``fit`` method
for all the estimators in the module. Regression and classification
only differ in the concrete ``LossFunction`` used.
- ``GradientBoostingClassifier`` implements gradient boosting for
classification problems.
- ``GradientBoostingRegressor`` implements gradient boosting for
regression problems.
"""
# Authors: Peter Prettenhofer, Scott White, Gilles Louppe, Emanuele Olivetti,
# Arnaud Joly, Jacob Schreiber
# License: BSD 3 clause
from abc import ABCMeta
from abc import abstractmethod
from numbers import Integral, Real
import warnings
from ._base import BaseEnsemble
from ..base import ClassifierMixin, RegressorMixin
from ..base import is_classifier
from ..utils import deprecated
from ._gradient_boosting import predict_stages
from ._gradient_boosting import predict_stage
from ._gradient_boosting import _random_sample_mask
import numpy as np
from scipy.sparse import csc_matrix
from scipy.sparse import csr_matrix
from scipy.sparse import issparse
from time import time
from ..model_selection import train_test_split
from ..tree import DecisionTreeRegressor
from ..tree._tree import DTYPE, DOUBLE
from . import _gb_losses
from ..utils import check_array, check_random_state, column_or_1d
from ..utils._param_validation import HasMethods, Interval, StrOptions
from ..utils.validation import check_is_fitted, _check_sample_weight
from ..utils.multiclass import check_classification_targets
from ..exceptions import NotFittedError
class VerboseReporter:
"""Reports verbose output to stdout.
Parameters
----------
verbose : int
Verbosity level. If ``verbose==1`` output is printed once in a while
(when iteration mod verbose_mod is zero).; if larger than 1 then output
is printed for each update.
"""
def __init__(self, verbose):
self.verbose = verbose
def init(self, est, begin_at_stage=0):
"""Initialize reporter
Parameters
----------
est : Estimator
The estimator
begin_at_stage : int, default=0
stage at which to begin reporting
"""
# header fields and line format str
header_fields = ["Iter", "Train Loss"]
verbose_fmt = ["{iter:>10d}", "{train_score:>16.4f}"]
# do oob?
if est.subsample < 1:
header_fields.append("OOB Improve")
verbose_fmt.append("{oob_impr:>16.4f}")
header_fields.append("Remaining Time")
verbose_fmt.append("{remaining_time:>16s}")
# print the header line
print(("%10s " + "%16s " * (len(header_fields) - 1)) % tuple(header_fields))
self.verbose_fmt = " ".join(verbose_fmt)
# plot verbose info each time i % verbose_mod == 0
self.verbose_mod = 1
self.start_time = time()
self.begin_at_stage = begin_at_stage
def update(self, j, est):
"""Update reporter with new iteration.
Parameters
----------
j : int
The new iteration.
est : Estimator
The estimator.
"""
do_oob = est.subsample < 1
# we need to take into account if we fit additional estimators.
i = j - self.begin_at_stage # iteration relative to the start iter
if (i + 1) % self.verbose_mod == 0:
oob_impr = est.oob_improvement_[j] if do_oob else 0
remaining_time = (
(est.n_estimators - (j + 1)) * (time() - self.start_time) / float(i + 1)
)
if remaining_time > 60:
remaining_time = "{0:.2f}m".format(remaining_time / 60.0)
else:
remaining_time = "{0:.2f}s".format(remaining_time)
print(
self.verbose_fmt.format(
iter=j + 1,
train_score=est.train_score_[j],
oob_impr=oob_impr,
remaining_time=remaining_time,
)
)
if self.verbose == 1 and ((i + 1) // (self.verbose_mod * 10) > 0):
# adjust verbose frequency (powers of 10)
self.verbose_mod *= 10
class BaseGradientBoosting(BaseEnsemble, metaclass=ABCMeta):
"""Abstract base class for Gradient Boosting."""
_parameter_constraints: dict = {
**DecisionTreeRegressor._parameter_constraints,
"learning_rate": [Interval(Real, 0.0, None, closed="left")],
"n_estimators": [Interval(Integral, 1, None, closed="left")],
"criterion": [StrOptions({"friedman_mse", "squared_error"})],
"subsample": [Interval(Real, 0.0, 1.0, closed="right")],
"verbose": ["verbose"],
"warm_start": ["boolean"],
"validation_fraction": [Interval(Real, 0.0, 1.0, closed="neither")],
"n_iter_no_change": [Interval(Integral, 1, None, closed="left"), None],
"tol": [Interval(Real, 0.0, None, closed="left")],
}
_parameter_constraints.pop("splitter")
@abstractmethod
def __init__(
self,
*,
loss,
learning_rate,
n_estimators,
criterion,
min_samples_split,
min_samples_leaf,
min_weight_fraction_leaf,
max_depth,
min_impurity_decrease,
init,
subsample,
max_features,
ccp_alpha,
random_state,
alpha=0.9,
verbose=0,
max_leaf_nodes=None,
warm_start=False,
validation_fraction=0.1,
n_iter_no_change=None,
tol=1e-4,
):
self.n_estimators = n_estimators
self.learning_rate = learning_rate
self.loss = loss
self.criterion = criterion
self.min_samples_split = min_samples_split
self.min_samples_leaf = min_samples_leaf
self.min_weight_fraction_leaf = min_weight_fraction_leaf
self.subsample = subsample
self.max_features = max_features
self.max_depth = max_depth
self.min_impurity_decrease = min_impurity_decrease
self.ccp_alpha = ccp_alpha
self.init = init
self.random_state = random_state
self.alpha = alpha
self.verbose = verbose
self.max_leaf_nodes = max_leaf_nodes
self.warm_start = warm_start
self.validation_fraction = validation_fraction
self.n_iter_no_change = n_iter_no_change
self.tol = tol
@abstractmethod
def _validate_y(self, y, sample_weight=None):
"""Called by fit to validate y."""
def _fit_stage(
self,
i,
X,
y,
raw_predictions,
sample_weight,
sample_mask,
random_state,
X_csc=None,
X_csr=None,
):
"""Fit another stage of ``_n_classes`` trees to the boosting model."""
assert sample_mask.dtype == bool
loss = self._loss
original_y = y
# Need to pass a copy of raw_predictions to negative_gradient()
# because raw_predictions is partially updated at the end of the loop
# in update_terminal_regions(), and gradients need to be evaluated at
# iteration i - 1.
raw_predictions_copy = raw_predictions.copy()
for k in range(loss.K):
if loss.is_multi_class:
y = np.array(original_y == k, dtype=np.float64)
residual = loss.negative_gradient(
y, raw_predictions_copy, k=k, sample_weight=sample_weight
)
# induce regression tree on residuals
tree = DecisionTreeRegressor(
criterion=self.criterion,
splitter="best",
max_depth=self.max_depth,
min_samples_split=self.min_samples_split,
min_samples_leaf=self.min_samples_leaf,
min_weight_fraction_leaf=self.min_weight_fraction_leaf,
min_impurity_decrease=self.min_impurity_decrease,
max_features=self.max_features,
max_leaf_nodes=self.max_leaf_nodes,
random_state=random_state,
ccp_alpha=self.ccp_alpha,
)
if self.subsample < 1.0:
# no inplace multiplication!
sample_weight = sample_weight * sample_mask.astype(np.float64)
X = X_csr if X_csr is not None else X
tree.fit(X, residual, sample_weight=sample_weight, check_input=False)
# update tree leaves
loss.update_terminal_regions(
tree.tree_,
X,
y,
residual,
raw_predictions,
sample_weight,
sample_mask,
learning_rate=self.learning_rate,
k=k,
)
# add tree to ensemble
self.estimators_[i, k] = tree
return raw_predictions
def _check_params(self):
# TODO(1.3): Remove
if self.loss == "deviance":
warnings.warn(
"The loss parameter name 'deviance' was deprecated in v1.1 and will be "
"removed in version 1.3. Use the new parameter name 'log_loss' which "
"is equivalent.",
FutureWarning,
)
loss_class = (
_gb_losses.MultinomialDeviance
if len(self.classes_) > 2
else _gb_losses.BinomialDeviance
)
elif self.loss == "log_loss":
loss_class = (
_gb_losses.MultinomialDeviance
if len(self.classes_) > 2
else _gb_losses.BinomialDeviance
)
else:
loss_class = _gb_losses.LOSS_FUNCTIONS[self.loss]
if is_classifier(self):
self._loss = loss_class(self.n_classes_)
elif self.loss in ("huber", "quantile"):
self._loss = loss_class(self.alpha)
else:
self._loss = loss_class()
if isinstance(self.max_features, str):
if self.max_features == "auto":
if is_classifier(self):
max_features = max(1, int(np.sqrt(self.n_features_in_)))
else:
max_features = self.n_features_in_
elif self.max_features == "sqrt":
max_features = max(1, int(np.sqrt(self.n_features_in_)))
else: # self.max_features == "log2"
max_features = max(1, int(np.log2(self.n_features_in_)))
elif self.max_features is None:
max_features = self.n_features_in_
elif isinstance(self.max_features, Integral):
max_features = self.max_features
else: # float
max_features = max(1, int(self.max_features * self.n_features_in_))
self.max_features_ = max_features
def _init_state(self):
"""Initialize model state and allocate model state data structures."""
self.init_ = self.init
if self.init_ is None:
self.init_ = self._loss.init_estimator()
self.estimators_ = np.empty((self.n_estimators, self._loss.K), dtype=object)
self.train_score_ = np.zeros((self.n_estimators,), dtype=np.float64)
# do oob?
if self.subsample < 1.0:
self.oob_improvement_ = np.zeros((self.n_estimators), dtype=np.float64)
def _clear_state(self):
"""Clear the state of the gradient boosting model."""
if hasattr(self, "estimators_"):
self.estimators_ = np.empty((0, 0), dtype=object)
if hasattr(self, "train_score_"):
del self.train_score_
if hasattr(self, "oob_improvement_"):
del self.oob_improvement_
if hasattr(self, "init_"):
del self.init_
if hasattr(self, "_rng"):
del self._rng
def _resize_state(self):
"""Add additional ``n_estimators`` entries to all attributes."""
# self.n_estimators is the number of additional est to fit
total_n_estimators = self.n_estimators
if total_n_estimators < self.estimators_.shape[0]:
raise ValueError(
"resize with smaller n_estimators %d < %d"
% (total_n_estimators, self.estimators_[0])
)
self.estimators_ = np.resize(
self.estimators_, (total_n_estimators, self._loss.K)
)
self.train_score_ = np.resize(self.train_score_, total_n_estimators)
if self.subsample < 1 or hasattr(self, "oob_improvement_"):
# if do oob resize arrays or create new if not available
if hasattr(self, "oob_improvement_"):
self.oob_improvement_ = np.resize(
self.oob_improvement_, total_n_estimators
)
else:
self.oob_improvement_ = np.zeros(
(total_n_estimators,), dtype=np.float64
)
def _is_initialized(self):
return len(getattr(self, "estimators_", [])) > 0
def _check_initialized(self):
"""Check that the estimator is initialized, raising an error if not."""
check_is_fitted(self)
def fit(self, X, y, sample_weight=None, monitor=None):
"""Fit the gradient boosting model.
Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
The input samples. Internally, it will be converted to
``dtype=np.float32`` and if a sparse matrix is provided
to a sparse ``csr_matrix``.
y : array-like of shape (n_samples,)
Target values (strings or integers in classification, real numbers
in regression)
For classification, labels must correspond to classes.
sample_weight : array-like of shape (n_samples,), default=None
Sample weights. If None, then samples are equally weighted. Splits
that would create child nodes with net zero or negative weight are
ignored while searching for a split in each node. In the case of
classification, splits are also ignored if they would result in any
single class carrying a negative weight in either child node.
monitor : callable, default=None
The monitor is called after each iteration with the current
iteration, a reference to the estimator and the local variables of
``_fit_stages`` as keyword arguments ``callable(i, self,
locals())``. If the callable returns ``True`` the fitting procedure
is stopped. The monitor can be used for various things such as
computing held-out estimates, early stopping, model introspect, and
snapshoting.
Returns
-------
self : object
Fitted estimator.
"""
self._validate_params()
if not self.warm_start:
self._clear_state()
# Check input
# Since check_array converts both X and y to the same dtype, but the
# trees use different types for X and y, checking them separately.
X, y = self._validate_data(
X, y, accept_sparse=["csr", "csc", "coo"], dtype=DTYPE, multi_output=True
)
sample_weight_is_none = sample_weight is None
sample_weight = _check_sample_weight(sample_weight, X)
y = column_or_1d(y, warn=True)
if is_classifier(self):
y = self._validate_y(y, sample_weight)
else:
y = self._validate_y(y)
self._check_params()
if self.n_iter_no_change is not None:
stratify = y if is_classifier(self) else None
X, X_val, y, y_val, sample_weight, sample_weight_val = train_test_split(
X,
y,
sample_weight,
random_state=self.random_state,
test_size=self.validation_fraction,
stratify=stratify,
)
if is_classifier(self):
if self._n_classes != np.unique(y).shape[0]:
# We choose to error here. The problem is that the init
# estimator would be trained on y, which has some missing
# classes now, so its predictions would not have the
# correct shape.
raise ValueError(
"The training data after the early stopping split "
"is missing some classes. Try using another random "
"seed."
)
else:
X_val = y_val = sample_weight_val = None
if not self._is_initialized():
# init state
self._init_state()
# fit initial model and initialize raw predictions
if self.init_ == "zero":
raw_predictions = np.zeros(
shape=(X.shape[0], self._loss.K), dtype=np.float64
)
else:
# XXX clean this once we have a support_sample_weight tag
if sample_weight_is_none:
self.init_.fit(X, y)
else:
msg = (
"The initial estimator {} does not support sample "
"weights.".format(self.init_.__class__.__name__)
)
try:
self.init_.fit(X, y, sample_weight=sample_weight)
except TypeError as e:
if "unexpected keyword argument 'sample_weight'" in str(e):
# regular estimator without SW support
raise ValueError(msg) from e
else: # regular estimator whose input checking failed
raise
except ValueError as e:
if (
"pass parameters to specific steps of "
"your pipeline using the "
"stepname__parameter"
in str(e)
): # pipeline
raise ValueError(msg) from e
else: # regular estimator whose input checking failed
raise
raw_predictions = self._loss.get_init_raw_predictions(X, self.init_)
begin_at_stage = 0
# The rng state must be preserved if warm_start is True
self._rng = check_random_state(self.random_state)
else:
# add more estimators to fitted model
# invariant: warm_start = True
if self.n_estimators < self.estimators_.shape[0]:
raise ValueError(
"n_estimators=%d must be larger or equal to "
"estimators_.shape[0]=%d when "
"warm_start==True" % (self.n_estimators, self.estimators_.shape[0])
)
begin_at_stage = self.estimators_.shape[0]
# The requirements of _raw_predict
# are more constrained than fit. It accepts only CSR
# matrices. Finite values have already been checked in _validate_data.
X = check_array(
X,
dtype=DTYPE,
order="C",
accept_sparse="csr",
force_all_finite=False,
)
raw_predictions = self._raw_predict(X)
self._resize_state()
# fit the boosting stages
n_stages = self._fit_stages(
X,
y,
raw_predictions,
sample_weight,
self._rng,
X_val,
y_val,
sample_weight_val,
begin_at_stage,
monitor,
)
# change shape of arrays after fit (early-stopping or additional ests)
if n_stages != self.estimators_.shape[0]:
self.estimators_ = self.estimators_[:n_stages]
self.train_score_ = self.train_score_[:n_stages]
if hasattr(self, "oob_improvement_"):
self.oob_improvement_ = self.oob_improvement_[:n_stages]
self.n_estimators_ = n_stages
return self
def _fit_stages(
self,
X,
y,
raw_predictions,
sample_weight,
random_state,
X_val,
y_val,
sample_weight_val,
begin_at_stage=0,
monitor=None,
):
"""Iteratively fits the stages.
For each stage it computes the progress (OOB, train score)
and delegates to ``_fit_stage``.
Returns the number of stages fit; might differ from ``n_estimators``
due to early stopping.
"""
n_samples = X.shape[0]
do_oob = self.subsample < 1.0
sample_mask = np.ones((n_samples,), dtype=bool)
n_inbag = max(1, int(self.subsample * n_samples))
loss_ = self._loss
if self.verbose:
verbose_reporter = VerboseReporter(verbose=self.verbose)
verbose_reporter.init(self, begin_at_stage)
X_csc = csc_matrix(X) if issparse(X) else None
X_csr = csr_matrix(X) if issparse(X) else None
if self.n_iter_no_change is not None:
loss_history = np.full(self.n_iter_no_change, np.inf)
# We create a generator to get the predictions for X_val after
# the addition of each successive stage
y_val_pred_iter = self._staged_raw_predict(X_val, check_input=False)
# perform boosting iterations
i = begin_at_stage
for i in range(begin_at_stage, self.n_estimators):
# subsampling
if do_oob:
sample_mask = _random_sample_mask(n_samples, n_inbag, random_state)
# OOB score before adding this stage
old_oob_score = loss_(
y[~sample_mask],
raw_predictions[~sample_mask],
sample_weight[~sample_mask],
)
# fit next stage of trees
raw_predictions = self._fit_stage(
i,
X,
y,
raw_predictions,
sample_weight,
sample_mask,
random_state,
X_csc,
X_csr,
)
# track deviance (= loss)
if do_oob:
self.train_score_[i] = loss_(
y[sample_mask],
raw_predictions[sample_mask],
sample_weight[sample_mask],
)
self.oob_improvement_[i] = old_oob_score - loss_(
y[~sample_mask],
raw_predictions[~sample_mask],
sample_weight[~sample_mask],
)
else:
# no need to fancy index w/ no subsampling
self.train_score_[i] = loss_(y, raw_predictions, sample_weight)
if self.verbose > 0:
verbose_reporter.update(i, self)
if monitor is not None:
early_stopping = monitor(i, self, locals())
if early_stopping:
break
# We also provide an early stopping based on the score from
# validation set (X_val, y_val), if n_iter_no_change is set
if self.n_iter_no_change is not None:
# By calling next(y_val_pred_iter), we get the predictions
# for X_val after the addition of the current stage
validation_loss = loss_(y_val, next(y_val_pred_iter), sample_weight_val)
# Require validation_score to be better (less) than at least
# one of the last n_iter_no_change evaluations
if np.any(validation_loss + self.tol < loss_history):
loss_history[i % len(loss_history)] = validation_loss
else:
break
return i + 1
def _make_estimator(self, append=True):
# we don't need _make_estimator
raise NotImplementedError()
def _raw_predict_init(self, X):
"""Check input and compute raw predictions of the init estimator."""
self._check_initialized()
X = self.estimators_[0, 0]._validate_X_predict(X, check_input=True)
if self.init_ == "zero":
raw_predictions = np.zeros(
shape=(X.shape[0], self._loss.K), dtype=np.float64
)
else:
raw_predictions = self._loss.get_init_raw_predictions(X, self.init_).astype(
np.float64
)
return raw_predictions
def _raw_predict(self, X):
"""Return the sum of the trees raw predictions (+ init estimator)."""
raw_predictions = self._raw_predict_init(X)
predict_stages(self.estimators_, X, self.learning_rate, raw_predictions)
return raw_predictions
def _staged_raw_predict(self, X, check_input=True):
"""Compute raw predictions of ``X`` for each iteration.
This method allows monitoring (i.e. determine error on testing set)
after each stage.
Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
The input samples. Internally, it will be converted to
``dtype=np.float32`` and if a sparse matrix is provided
to a sparse ``csr_matrix``.
check_input : bool, default=True
If False, the input arrays X will not be checked.
Returns
-------
raw_predictions : generator of ndarray of shape (n_samples, k)
The raw predictions of the input samples. The order of the
classes corresponds to that in the attribute :term:`classes_`.
Regression and binary classification are special cases with
``k == 1``, otherwise ``k==n_classes``.
"""
if check_input:
X = self._validate_data(
X, dtype=DTYPE, order="C", accept_sparse="csr", reset=False
)
raw_predictions = self._raw_predict_init(X)
for i in range(self.estimators_.shape[0]):
predict_stage(self.estimators_, i, X, self.learning_rate, raw_predictions)
yield raw_predictions.copy()
@property
def feature_importances_(self):
"""The impurity-based feature importances.
The higher, the more important the feature.
The importance of a feature is computed as the (normalized)
total reduction of the criterion brought by that feature. It is also
known as the Gini importance.
Warning: impurity-based feature importances can be misleading for
high cardinality features (many unique values). See
:func:`sklearn.inspection.permutation_importance` as an alternative.
Returns
-------
feature_importances_ : ndarray of shape (n_features,)
The values of this array sum to 1, unless all trees are single node
trees consisting of only the root node, in which case it will be an
array of zeros.
"""
self._check_initialized()
relevant_trees = [
tree
for stage in self.estimators_
for tree in stage
if tree.tree_.node_count > 1
]
if not relevant_trees:
# degenerate case where all trees have only one node
return np.zeros(shape=self.n_features_in_, dtype=np.float64)
relevant_feature_importances = [
tree.tree_.compute_feature_importances(normalize=False)
for tree in relevant_trees
]
avg_feature_importances = np.mean(
relevant_feature_importances, axis=0, dtype=np.float64
)
return avg_feature_importances / np.sum(avg_feature_importances)
def _compute_partial_dependence_recursion(self, grid, target_features):
"""Fast partial dependence computation.
Parameters
----------
grid : ndarray of shape (n_samples, n_target_features)
The grid points on which the partial dependence should be
evaluated.
target_features : ndarray of shape (n_target_features,)
The set of target features for which the partial dependence
should be evaluated.
Returns
-------
averaged_predictions : ndarray of shape \
(n_trees_per_iteration, n_samples)
The value of the partial dependence function on each grid point.
"""
if self.init is not None:
warnings.warn(
"Using recursion method with a non-constant init predictor "
"will lead to incorrect partial dependence values. "
"Got init=%s."
% self.init,
UserWarning,
)
grid = np.asarray(grid, dtype=DTYPE, order="C")
n_estimators, n_trees_per_stage = self.estimators_.shape
averaged_predictions = np.zeros(
(n_trees_per_stage, grid.shape[0]), dtype=np.float64, order="C"
)
for stage in range(n_estimators):
for k in range(n_trees_per_stage):
tree = self.estimators_[stage, k].tree_
tree.compute_partial_dependence(
grid, target_features, averaged_predictions[k]
)
averaged_predictions *= self.learning_rate
return averaged_predictions
def apply(self, X):
"""Apply trees in the ensemble to X, return leaf indices.
.. versionadded:: 0.17
Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
The input samples. Internally, its dtype will be converted to
``dtype=np.float32``. If a sparse matrix is provided, it will
be converted to a sparse ``csr_matrix``.
Returns
-------
X_leaves : array-like of shape (n_samples, n_estimators, n_classes)
For each datapoint x in X and for each tree in the ensemble,
return the index of the leaf x ends up in each estimator.
In the case of binary classification n_classes is 1.
"""
self._check_initialized()
X = self.estimators_[0, 0]._validate_X_predict(X, check_input=True)
# n_classes will be equal to 1 in the binary classification or the
# regression case.
n_estimators, n_classes = self.estimators_.shape
leaves = np.zeros((X.shape[0], n_estimators, n_classes))
for i in range(n_estimators):
for j in range(n_classes):
estimator = self.estimators_[i, j]
leaves[:, i, j] = estimator.apply(X, check_input=False)
return leaves
# TODO(1.3): Remove
# mypy error: Decorated property not supported
@deprecated( # type: ignore
"Attribute `loss_` was deprecated in version 1.1 and will be removed in 1.3."
)
@property
def loss_(self):
return self._loss
class GradientBoostingClassifier(ClassifierMixin, BaseGradientBoosting):
"""Gradient Boosting for classification.
This algorithm builds an additive model in a forward stage-wise fashion; it
allows for the optimization of arbitrary differentiable loss functions. In
each stage ``n_classes_`` regression trees are fit on the negative gradient
of the loss function, e.g. binary or multiclass log loss. Binary
classification is a special case where only a single regression tree is
induced.
:class:`sklearn.ensemble.HistGradientBoostingClassifier` is a much faster
variant of this algorithm for intermediate datasets (`n_samples >= 10_000`).
Read more in the :ref:`User Guide <gradient_boosting>`.
Parameters
----------
loss : {'log_loss', 'deviance', 'exponential'}, default='log_loss'
The loss function to be optimized. 'log_loss' refers to binomial and
multinomial deviance, the same as used in logistic regression.
It is a good choice for classification with probabilistic outputs.
For loss 'exponential', gradient boosting recovers the AdaBoost algorithm.
.. deprecated:: 1.1
The loss 'deviance' was deprecated in v1.1 and will be removed in
version 1.3. Use `loss='log_loss'` which is equivalent.
learning_rate : float, default=0.1
Learning rate shrinks the contribution of each tree by `learning_rate`.
There is a trade-off between learning_rate and n_estimators.
Values must be in the range `[0.0, inf)`.
n_estimators : int, default=100
The number of boosting stages to perform. Gradient boosting
is fairly robust to over-fitting so a large number usually
results in better performance.
Values must be in the range `[1, inf)`.
subsample : float, default=1.0
The fraction of samples to be used for fitting the individual base
learners. If smaller than 1.0 this results in Stochastic Gradient
Boosting. `subsample` interacts with the parameter `n_estimators`.
Choosing `subsample < 1.0` leads to a reduction of variance
and an increase in bias.
Values must be in the range `(0.0, 1.0]`.
criterion : {'friedman_mse', 'squared_error'}, default='friedman_mse'
The function to measure the quality of a split. Supported criteria are
'friedman_mse' for the mean squared error with improvement score by
Friedman, 'squared_error' for mean squared error. The default value of
'friedman_mse' is generally the best as it can provide a better
approximation in some cases.
.. versionadded:: 0.18
min_samples_split : int or float, default=2
The minimum number of samples required to split an internal node:
- If int, values must be in the range `[2, inf)`.
- If float, values must be in the range `(0.0, 1.0]` and `min_samples_split`
will be `ceil(min_samples_split * n_samples)`.
.. versionchanged:: 0.18
Added float values for fractions.
min_samples_leaf : int or float, default=1
The minimum number of samples required to be at a leaf node.
A split point at any depth will only be considered if it leaves at
least ``min_samples_leaf`` training samples in each of the left and
right branches. This may have the effect of smoothing the model,
especially in regression.
- If int, values must be in the range `[1, inf)`.
- If float, values must be in the range `(0.0, 1.0)` and `min_samples_leaf`
will be `ceil(min_samples_leaf * n_samples)`.
.. versionchanged:: 0.18
Added float values for fractions.
min_weight_fraction_leaf : float, default=0.0
The minimum weighted fraction of the sum total of weights (of all
the input samples) required to be at a leaf node. Samples have
equal weight when sample_weight is not provided.
Values must be in the range `[0.0, 0.5]`.
max_depth : int or None, default=3
Maximum depth of the individual regression estimators. The maximum
depth limits the number of nodes in the tree. Tune this parameter
for best performance; the best value depends on the interaction
of the input variables. If None, then nodes are expanded until
all leaves are pure or until all leaves contain less than
min_samples_split samples.
If int, values must be in the range `[1, inf)`.
min_impurity_decrease : float, default=0.0
A node will be split if this split induces a decrease of the impurity
greater than or equal to this value.
Values must be in the range `[0.0, inf)`.
The weighted impurity decrease equation is the following::
N_t / N * (impurity - N_t_R / N_t * right_impurity
- N_t_L / N_t * left_impurity)
where ``N`` is the total number of samples, ``N_t`` is the number of
samples at the current node, ``N_t_L`` is the number of samples in the
left child, and ``N_t_R`` is the number of samples in the right child.
``N``, ``N_t``, ``N_t_R`` and ``N_t_L`` all refer to the weighted sum,
if ``sample_weight`` is passed.
.. versionadded:: 0.19
init : estimator or 'zero', default=None
An estimator object that is used to compute the initial predictions.
``init`` has to provide :meth:`fit` and :meth:`predict_proba`. If
'zero', the initial raw predictions are set to zero. By default, a
``DummyEstimator`` predicting the classes priors is used.
random_state : int, RandomState instance or None, default=None
Controls the random seed given to each Tree estimator at each
boosting iteration.
In addition, it controls the random permutation of the features at
each split (see Notes for more details).
It also controls the random splitting of the training data to obtain a
validation set if `n_iter_no_change` is not None.
Pass an int for reproducible output across multiple function calls.
See :term:`Glossary <random_state>`.
max_features : {'auto', 'sqrt', 'log2'}, int or float, default=None
The number of features to consider when looking for the best split:
- If int, values must be in the range `[1, inf)`.
- If float, values must be in the range `(0.0, 1.0]` and the features
considered at each split will be `max(1, int(max_features * n_features_in_))`.
- If 'auto', then `max_features=sqrt(n_features)`.
- If 'sqrt', then `max_features=sqrt(n_features)`.
- If 'log2', then `max_features=log2(n_features)`.
- If None, then `max_features=n_features`.
Choosing `max_features < n_features` leads to a reduction of variance
and an increase in bias.
Note: the search for a split does not stop until at least one
valid partition of the node samples is found, even if it requires to
effectively inspect more than ``max_features`` features.
verbose : int, default=0
Enable verbose output. If 1 then it prints progress and performance
once in a while (the more trees the lower the frequency). If greater