@@ -169,11 +169,6 @@ def validate_or_none(meta: Optional[List], name: str) -> List:
169
169
return train_dmatrix , evals
170
170
171
171
172
- try :
173
- from xgboost .sklearn import _convert_ntree_limit
174
- except ImportError :
175
- _convert_ntree_limit = None
176
-
177
172
try :
178
173
from xgboost .sklearn import _cls_predict_proba
179
174
except ImportError :
@@ -195,10 +190,6 @@ def _cls_predict_proba(n_classes: int, prediction, vstack: Callable):
195
190
_is_cudf_ser = None
196
191
_is_cupy_array = None
197
192
198
- try :
199
- from xgboost .compat import XGBoostLabelEncoder
200
- except ImportError :
201
- from sklearn .preprocessing import LabelEncoder as XGBoostLabelEncoder
202
193
203
194
_RAY_PARAMS_DOC = """ray_params : None or RayParams or Dict
204
195
Parameters to configure Ray-specific behavior.
@@ -367,27 +358,15 @@ def _ray_predict(
367
358
self : "XGBModel" ,
368
359
X ,
369
360
output_margin = False ,
370
- ntree_limit = None ,
371
361
validate_features = True ,
372
362
base_margin = None ,
373
363
iteration_range = None ,
374
364
ray_params : Union [None , RayParams , Dict ] = None ,
375
365
_remote : Optional [bool ] = None ,
376
366
ray_dmatrix_params : Optional [Dict ] = None ,
367
+ ** kwargs ,
377
368
):
378
369
"""Distributed predict via Ray"""
379
- compat_predict_kwargs = {}
380
- if _convert_ntree_limit is not None :
381
- iteration_range = _convert_ntree_limit (
382
- self .get_booster (), ntree_limit , iteration_range
383
- )
384
- iteration_range = self ._get_iteration_range (iteration_range )
385
- compat_predict_kwargs ["iteration_range" ] = iteration_range
386
- else :
387
- if ntree_limit is None :
388
- ntree_limit = getattr (self , "best_ntree_limit" , 0 )
389
- compat_predict_kwargs ["ntree_limit" ] = ntree_limit
390
-
391
370
ray_params = self ._ray_set_ray_params_n_jobs (ray_params , self .n_jobs )
392
371
ray_dmatrix_params = ray_dmatrix_params or {}
393
372
@@ -407,7 +386,7 @@ def _ray_predict(
407
386
validate_features = validate_features ,
408
387
ray_params = ray_params ,
409
388
_remote = _remote ,
410
- ** compat_predict_kwargs ,
389
+ ** kwargs ,
411
390
)
412
391
413
392
def _ray_get_wrap_evaluation_matrices_compat_kwargs (
@@ -589,24 +568,24 @@ def predict(
589
568
self ,
590
569
X ,
591
570
output_margin = False ,
592
- ntree_limit = None ,
593
571
validate_features = True ,
594
572
base_margin = None ,
595
573
iteration_range = None ,
596
574
ray_params : Union [None , RayParams , Dict ] = None ,
597
575
_remote : Optional [bool ] = None ,
598
576
ray_dmatrix_params : Optional [Dict ] = None ,
577
+ ** kwargs ,
599
578
):
600
579
return self ._ray_predict (
601
580
X ,
602
581
output_margin = output_margin ,
603
- ntree_limit = ntree_limit ,
604
582
validate_features = validate_features ,
605
583
base_margin = base_margin ,
606
584
iteration_range = iteration_range ,
607
585
ray_params = ray_params ,
608
586
_remote = _remote ,
609
587
ray_dmatrix_params = ray_dmatrix_params ,
588
+ ** kwargs ,
610
589
)
611
590
612
591
predict .__doc__ = _treat_X_doc (_get_doc (XGBRegressor .predict )) + _RAY_PARAMS_DOC
@@ -702,22 +681,13 @@ def fit(
702
681
)
703
682
704
683
if train_dmatrix is not None :
705
- if not hasattr (self , "use_label_encoder" ):
706
- warnings .warn (
707
- "If X is a RayDMatrix, no label encoding"
708
- " will be performed. Ensure the labels are"
709
- " encoded."
710
- )
711
- elif self .use_label_encoder :
712
- raise ValueError (
713
- "X cannot be a RayDMatrix if `use_label_encoder` " "is set to True"
714
- )
715
684
if "num_class" not in params :
716
685
raise ValueError (
717
686
"`num_class` must be set during initalization if X"
718
687
" is a RayDMatrix"
719
688
)
720
- self .classes_ = list (range (0 , params ["num_class" ]))
689
+ if XGBOOST_VERSION < Version ("2.0.0" ):
690
+ self .classes_ = list (range (0 , params ["num_class" ]))
721
691
self .n_classes_ = params ["num_class" ]
722
692
if self .n_classes_ <= 2 :
723
693
params .pop ("num_class" )
@@ -730,7 +700,10 @@ def fit(
730
700
"Please reshape the input data X into 2-dimensional " "matrix."
731
701
)
732
702
733
- label_transform = self ._ray_fit_preprocess (y )
703
+ label_transform = lambda x : x # noqa: E731
704
+ if XGBOOST_VERSION < Version ("2.0.0" ):
705
+ self .classes_ = np .unique (y )
706
+ self .n_classes_ = len (np .unique (y ))
734
707
735
708
if callable (self .objective ):
736
709
obj = _objective_decorator (self .objective )
@@ -819,99 +792,31 @@ def fit(
819
792
820
793
fit .__doc__ = _treat_X_doc (_get_doc (XGBClassifier .fit )) + _RAY_PARAMS_DOC
821
794
822
- def _ray_fit_preprocess (self , y ) -> Callable :
823
- """This has been separated out so that it can be easily overwritten
824
- should a future xgboost version remove label encoding"""
825
- # pylint: disable = attribute-defined-outside-init,too-many-statements
826
- can_use_label_encoder = True
827
- use_label_encoder = getattr (self , "use_label_encoder" , True )
828
- label_encoding_check_error = (
829
- "The label must consist of integer "
830
- "labels of form 0, 1, 2, ..., [num_class - 1]."
831
- )
832
- label_encoder_deprecation_msg = (
833
- "The use of label encoder in XGBClassifier is deprecated and will "
834
- "be removed in a future release. To remove this warning, do the "
835
- "following: 1) Pass option use_label_encoder=False when "
836
- "constructing XGBClassifier object; and 2) Encode your labels (y) "
837
- "as integers starting with 0, i.e. 0, 1, 2, ..., [num_class - 1]."
838
- )
839
-
840
- # ray: modified this to allow for compatibility with legacy xgboost
841
- if (_is_cudf_df and _is_cudf_df (y )) or (_is_cudf_ser and _is_cudf_ser (y )):
842
- import cupy as cp # pylint: disable=E0401
843
-
844
- self .classes_ = cp .unique (y .values )
845
- self .n_classes_ = len (self .classes_ )
846
- can_use_label_encoder = False
847
- expected_classes = cp .arange (self .n_classes_ )
848
- if (
849
- self .classes_ .shape != expected_classes .shape
850
- or not (self .classes_ == expected_classes ).all ()
851
- ):
852
- raise ValueError (label_encoding_check_error )
853
- elif _is_cupy_array and _is_cupy_array (y ):
854
- import cupy as cp # pylint: disable=E0401
855
-
856
- self .classes_ = cp .unique (y )
857
- self .n_classes_ = len (self .classes_ )
858
- can_use_label_encoder = False
859
- expected_classes = cp .arange (self .n_classes_ )
860
- if (
861
- self .classes_ .shape != expected_classes .shape
862
- or not (self .classes_ == expected_classes ).all ()
863
- ):
864
- raise ValueError (label_encoding_check_error )
865
- else :
866
- self .classes_ = np .unique (y )
867
- self .n_classes_ = len (self .classes_ )
868
- if not use_label_encoder and (
869
- not np .array_equal (self .classes_ , np .arange (self .n_classes_ ))
870
- ):
871
- raise ValueError (label_encoding_check_error )
872
-
873
- if use_label_encoder :
874
- if not can_use_label_encoder :
875
- raise ValueError (
876
- "The option use_label_encoder=True is incompatible with "
877
- "inputs of type cuDF or cuPy. Please set "
878
- "use_label_encoder=False when constructing XGBClassifier "
879
- "object. NOTE:" + label_encoder_deprecation_msg
880
- )
881
- if hasattr (self , "use_label_encoder" ):
882
- warnings .warn (label_encoder_deprecation_msg , UserWarning )
883
- self ._le = XGBoostLabelEncoder ().fit (y )
884
- label_transform = self ._le .transform
885
- else :
886
- label_transform = lambda x : x # noqa: E731
887
-
888
- return label_transform
889
-
890
795
def _can_use_inplace_predict (self ) -> bool :
891
796
return False
892
797
893
798
def predict (
894
799
self ,
895
800
X ,
896
801
output_margin = False ,
897
- ntree_limit = None ,
898
802
validate_features = True ,
899
803
base_margin = None ,
900
804
iteration_range : Optional [Tuple [int , int ]] = None ,
901
805
ray_params : Union [None , RayParams , Dict ] = None ,
902
806
_remote : Optional [bool ] = None ,
903
807
ray_dmatrix_params : Optional [Dict ] = None ,
808
+ ** kwargs ,
904
809
):
905
810
class_probs = self ._ray_predict (
906
811
X = X ,
907
812
output_margin = output_margin ,
908
- ntree_limit = ntree_limit ,
909
813
validate_features = validate_features ,
910
814
base_margin = base_margin ,
911
815
iteration_range = iteration_range ,
912
816
ray_params = ray_params ,
913
817
_remote = _remote ,
914
818
ray_dmatrix_params = ray_dmatrix_params ,
819
+ ** kwargs ,
915
820
)
916
821
if output_margin :
917
822
# If output_margin is active, simply return the scores
@@ -934,25 +839,25 @@ def predict(
934
839
def predict_proba (
935
840
self ,
936
841
X ,
937
- ntree_limit = None ,
938
842
validate_features = False ,
939
843
base_margin = None ,
940
844
iteration_range : Optional [Tuple [int , int ]] = None ,
941
845
ray_params : Union [None , RayParams , Dict ] = None ,
942
846
_remote : Optional [bool ] = None ,
943
847
ray_dmatrix_params : Optional [Dict ] = None ,
848
+ ** kwargs ,
944
849
) -> np .ndarray :
945
850
946
851
class_probs = self ._ray_predict (
947
852
X = X ,
948
853
output_margin = self .objective == "multi:softmax" ,
949
- ntree_limit = ntree_limit ,
950
854
validate_features = validate_features ,
951
855
base_margin = base_margin ,
952
856
iteration_range = iteration_range ,
953
857
ray_params = ray_params ,
954
858
_remote = _remote ,
955
859
ray_dmatrix_params = ray_dmatrix_params ,
860
+ ** kwargs ,
956
861
)
957
862
# If model is loaded from a raw booster there's no `n_classes_`
958
863
return _cls_predict_proba (
@@ -979,31 +884,6 @@ class RayXGBRFClassifier(RayXGBClassifier):
979
884
def __init__ (self , * args , ** kwargs ):
980
885
raise ValueError ("RayXGBRFClassifier not available with xgboost<1.0.0" )
981
886
982
- # use_label_encoder added in xgboost commit
983
- # c8ec62103a36f1717d032b1ddff2bf9e0642508a (1.3.0)
984
- elif "use_label_encoder" in inspect .signature (XGBRFClassifier .__init__ ).parameters :
985
-
986
- @_deprecate_positional_args
987
- @_xgboost_version_warn
988
- def __init__ (
989
- self ,
990
- * ,
991
- learning_rate = 1 ,
992
- subsample = 0.8 ,
993
- colsample_bynode = 0.8 ,
994
- reg_lambda = 1e-5 ,
995
- use_label_encoder = True ,
996
- ** kwargs ,
997
- ):
998
- super ().__init__ (
999
- learning_rate = learning_rate ,
1000
- subsample = subsample ,
1001
- colsample_bynode = colsample_bynode ,
1002
- reg_lambda = reg_lambda ,
1003
- use_label_encoder = use_label_encoder ,
1004
- ** kwargs ,
1005
- )
1006
-
1007
887
else :
1008
888
1009
889
@_deprecate_positional_args
@@ -1172,24 +1052,24 @@ def predict(
1172
1052
self ,
1173
1053
X ,
1174
1054
output_margin = False ,
1175
- ntree_limit = None ,
1176
1055
validate_features = True ,
1177
1056
base_margin = None ,
1178
1057
iteration_range = None ,
1179
1058
ray_params : Union [None , RayParams , Dict ] = None ,
1180
1059
_remote : Optional [bool ] = None ,
1181
1060
ray_dmatrix_params : Optional [Dict ] = None ,
1061
+ ** kwargs ,
1182
1062
):
1183
1063
return self ._ray_predict (
1184
1064
X ,
1185
1065
output_margin = output_margin ,
1186
- ntree_limit = ntree_limit ,
1187
1066
validate_features = validate_features ,
1188
1067
base_margin = base_margin ,
1189
1068
iteration_range = iteration_range ,
1190
1069
ray_params = ray_params ,
1191
1070
_remote = _remote ,
1192
1071
ray_dmatrix_params = ray_dmatrix_params ,
1072
+ ** kwargs ,
1193
1073
)
1194
1074
1195
1075
predict .__doc__ = _treat_X_doc (_get_doc (XGBRanker .predict )) + _RAY_PARAMS_DOC
0 commit comments