@@ -29,8 +29,9 @@ class BaseHistGradientBoosting(BaseEstimator, ABC):
29
29
@abstractmethod
30
30
def __init__ (self , loss , learning_rate , max_iter , max_leaf_nodes ,
31
31
max_depth , min_samples_leaf , l2_regularization , max_bins ,
32
- warm_start , early_stopping , scoring , validation_fraction ,
33
- n_iter_no_change , tol , verbose , random_state ):
32
+ monotonic_cst , warm_start , early_stopping , scoring ,
33
+ validation_fraction , n_iter_no_change , tol , verbose ,
34
+ random_state ):
34
35
self .loss = loss
35
36
self .learning_rate = learning_rate
36
37
self .max_iter = max_iter
@@ -39,6 +40,7 @@ def __init__(self, loss, learning_rate, max_iter, max_leaf_nodes,
39
40
self .min_samples_leaf = min_samples_leaf
40
41
self .l2_regularization = l2_regularization
41
42
self .max_bins = max_bins
43
+ self .monotonic_cst = monotonic_cst
42
44
self .warm_start = warm_start
43
45
self .early_stopping = early_stopping
44
46
self .scoring = scoring
@@ -82,6 +84,12 @@ def _validate_parameters(self):
82
84
raise ValueError ('max_bins={} should be no smaller than 2 '
83
85
'and no larger than 255.' .format (self .max_bins ))
84
86
87
+ if self .monotonic_cst is not None and self .n_trees_per_iteration_ != 1 :
88
+ raise ValueError (
89
+ 'monotonic constraints are not supported for '
90
+ 'multiclass classification.'
91
+ )
92
+
85
93
def fit (self , X , y , sample_weight = None ):
86
94
"""Fit the gradient boosting model.
87
95
@@ -352,12 +360,12 @@ def fit(self, X, y, sample_weight=None):
352
360
353
361
# Build `n_trees_per_iteration` trees.
354
362
for k in range (self .n_trees_per_iteration_ ):
355
-
356
363
grower = TreeGrower (
357
364
X_binned_train , gradients [k , :], hessians [k , :],
358
365
n_bins = n_bins ,
359
366
n_bins_non_missing = self .bin_mapper_ .n_bins_non_missing_ ,
360
367
has_missing_values = has_missing_values ,
368
+ monotonic_cst = self .monotonic_cst ,
361
369
max_leaf_nodes = self .max_leaf_nodes ,
362
370
max_depth = self .max_depth ,
363
371
min_samples_leaf = self .min_samples_leaf ,
@@ -790,6 +798,11 @@ class HistGradientBoostingRegressor(RegressorMixin, BaseHistGradientBoosting):
790
798
Features with a small number of unique values may use less than
791
799
``max_bins`` bins. In addition to the ``max_bins`` bins, one more bin
792
800
is always reserved for missing values. Must be no larger than 255.
801
+ monotonic_cst : array-like of int of shape (n_features), default=None
802
+ Indicates the monotonic constraint to enforce on each feature. -1, 1
803
+ and 0 respectively correspond to a positive constraint, negative
804
+ constraint and no constraint. Read more in the :ref:`User Guide
805
+ <monotonic_cst_gbdt>`.
793
806
warm_start : bool, optional (default=False)
794
807
When set to ``True``, reuse the solution of the previous call to fit
795
808
and add more estimators to the ensemble. For results to be valid, the
@@ -867,16 +880,18 @@ class HistGradientBoostingRegressor(RegressorMixin, BaseHistGradientBoosting):
867
880
def __init__ (self , loss = 'least_squares' , learning_rate = 0.1 ,
868
881
max_iter = 100 , max_leaf_nodes = 31 , max_depth = None ,
869
882
min_samples_leaf = 20 , l2_regularization = 0. , max_bins = 255 ,
870
- warm_start = False , early_stopping = 'auto' , scoring = 'loss' ,
871
- validation_fraction = 0.1 , n_iter_no_change = 10 , tol = 1e-7 ,
883
+ monotonic_cst = None , warm_start = False , early_stopping = 'auto' ,
884
+ scoring = 'loss' , validation_fraction = 0.1 ,
885
+ n_iter_no_change = 10 , tol = 1e-7 ,
872
886
verbose = 0 , random_state = None ):
873
887
super (HistGradientBoostingRegressor , self ).__init__ (
874
888
loss = loss , learning_rate = learning_rate , max_iter = max_iter ,
875
889
max_leaf_nodes = max_leaf_nodes , max_depth = max_depth ,
876
890
min_samples_leaf = min_samples_leaf ,
877
891
l2_regularization = l2_regularization , max_bins = max_bins ,
878
- warm_start = warm_start , early_stopping = early_stopping ,
879
- scoring = scoring , validation_fraction = validation_fraction ,
892
+ monotonic_cst = monotonic_cst , early_stopping = early_stopping ,
893
+ warm_start = warm_start , scoring = scoring ,
894
+ validation_fraction = validation_fraction ,
880
895
n_iter_no_change = n_iter_no_change , tol = tol , verbose = verbose ,
881
896
random_state = random_state )
882
897
@@ -978,6 +993,11 @@ class HistGradientBoostingClassifier(BaseHistGradientBoosting,
978
993
Features with a small number of unique values may use less than
979
994
``max_bins`` bins. In addition to the ``max_bins`` bins, one more bin
980
995
is always reserved for missing values. Must be no larger than 255.
996
+ monotonic_cst : array-like of int of shape (n_features), default=None
997
+ Indicates the monotonic constraint to enforce on each feature. -1, 1
998
+ and 0 respectively correspond to a positive constraint, negative
999
+ constraint and no constraint. Read more in the :ref:`User Guide
1000
+ <monotonic_cst_gbdt>`.
981
1001
warm_start : bool, optional (default=False)
982
1002
When set to ``True``, reuse the solution of the previous call to fit
983
1003
and add more estimators to the ensemble. For results to be valid, the
@@ -1058,17 +1078,18 @@ class HistGradientBoostingClassifier(BaseHistGradientBoosting,
1058
1078
1059
1079
def __init__ (self , loss = 'auto' , learning_rate = 0.1 , max_iter = 100 ,
1060
1080
max_leaf_nodes = 31 , max_depth = None , min_samples_leaf = 20 ,
1061
- l2_regularization = 0. , max_bins = 255 , warm_start = False ,
1062
- early_stopping = 'auto' , scoring = 'loss' ,
1081
+ l2_regularization = 0. , max_bins = 255 , monotonic_cst = None ,
1082
+ warm_start = False , early_stopping = 'auto' , scoring = 'loss' ,
1063
1083
validation_fraction = 0.1 , n_iter_no_change = 10 , tol = 1e-7 ,
1064
1084
verbose = 0 , random_state = None ):
1065
1085
super (HistGradientBoostingClassifier , self ).__init__ (
1066
1086
loss = loss , learning_rate = learning_rate , max_iter = max_iter ,
1067
1087
max_leaf_nodes = max_leaf_nodes , max_depth = max_depth ,
1068
1088
min_samples_leaf = min_samples_leaf ,
1069
1089
l2_regularization = l2_regularization , max_bins = max_bins ,
1070
- warm_start = warm_start , early_stopping = early_stopping ,
1071
- scoring = scoring , validation_fraction = validation_fraction ,
1090
+ monotonic_cst = monotonic_cst , warm_start = warm_start ,
1091
+ early_stopping = early_stopping , scoring = scoring ,
1092
+ validation_fraction = validation_fraction ,
1072
1093
n_iter_no_change = n_iter_no_change , tol = tol , verbose = verbose ,
1073
1094
random_state = random_state )
1074
1095
0 commit comments