xgboost API Reference
Full documentation for xgboost (3.3.0)
xgboost.Booster
Python Library Documentation: class Booster in module xgboost.core
class Booster(builtins.object)
| Booster(params: Union[List, Dict[str, Any], NoneType] = None, cache: Optional[Sequence[xgboost.core.DMatrix]] = None, model_file: Union[ForwardRef('Booster'), bytearray, os.PathLike, str, NoneType] = None) -> None
|
| A Booster of XGBoost.
|
| Booster is the model of xgboost, that contains low level routines for
| training, prediction and evaluation.
|
| Methods defined here:
|
| __copy__(self) -> 'Booster'
|
| __deepcopy__(self, _: Any) -> 'Booster'
| Return a copy of booster.
|
| __del__(self) -> None
|
| __getitem__(self, val: Union[int, numpy.integer, tuple, slice, ellipsis]) -> 'Booster'
| Get a slice of the tree-based model. Attributes like `best_iteration` and
| `best_score` are removed in the resulting booster.
|
| .. versionadded:: 1.3.0
|
| __getstate__(self) -> Dict
| Helper for pickle.
|
| __init__(self, params: Union[List, Dict[str, Any], NoneType] = None, cache: Optional[Sequence[xgboost.core.DMatrix]] = None, model_file: Union[ForwardRef('Booster'), bytearray, os.PathLike, str, NoneType] = None) -> None
| Parameters
| ----------
| params :
| Parameters for boosters.
| cache :
| List of cache items.
| model_file :
| Path to the model file if it's string or PathLike.
|
| __iter__(self) -> Generator[ForwardRef('Booster'), NoneType, NoneType]
| Iterator method for getting individual trees.
|
| .. versionadded:: 2.0.0
|
| __setstate__(self, state: Dict) -> None
|
| attr(self, key: str) -> Optional[str]
| Get attribute string from the Booster.
|
| Parameters
| ----------
| key :
| The key to get attribute from.
|
| Returns
| -------
| value :
| The attribute value of the key, returns None if attribute do not exist.
|
| attributes(self) -> Dict[str, Optional[str]]
| Get attributes stored in the Booster as a dictionary.
|
| Returns
| -------
| result : dictionary of attribute_name: attribute_value pairs of strings.
| Returns an empty dict if there's no attributes.
|
| boost(self, dtrain: xgboost.core.DMatrix, iteration: int, *, grad: Union[numpy.ndarray, Any, NoneType] = None, hess: Union[numpy.ndarray, Any, NoneType] = None, fobj: Optional[Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[numpy.ndarray, numpy.ndarray]]] = None) -> None
| Boost the booster for one iteration with customized gradient statistics.
|
| .. warning::
|
| Like :py:meth:`.update`, this function should not be called directly by
| users.
|
| Parameters
| ----------
| dtrain :
| The training DMatrix.
| iteration :
| The current training iteration.
| grad :
| The first order of gradient.
| hess :
| The second order of gradient.
| fobj :
| A custom objective function. If gradient is None, then an objective function
| is required.
|
| copy(self) -> 'Booster'
| Copy the booster object.
|
| Returns
| -------
| booster :
| A copied booster model
|
| dump_model(self, fout: Union[str, os.PathLike], fmap: Union[str, os.PathLike] = '', with_stats: bool = False, dump_format: str = 'text') -> None
| Dump model into a text or JSON file. Unlike :py:meth:`save_model`, the
| output format is primarily used for visualization or interpretation,
| hence it's more human readable but cannot be loaded back to XGBoost.
|
| Parameters
| ----------
| fout :
| Output file name.
| fmap :
| Name of the file containing feature map names.
| with_stats :
| Controls whether the split statistics are output.
| dump_format :
| Format of model dump file. Can be 'text' or 'json'.
|
| eval(self, data: xgboost.core.DMatrix, name: str = 'eval', iteration: int = 0) -> str
| Evaluate the model on mat.
|
| Parameters
| ----------
| data :
| The dmatrix storing the input.
|
| name :
| The name of the dataset.
|
| iteration :
| The current iteration number.
|
| Returns
| -------
| result: str
| Evaluation result string.
|
| eval_set(self, evals: Sequence[Tuple[xgboost.core.DMatrix, str]], iteration: int = 0, feval: Optional[Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[str, float]]] = None, output_margin: bool = True) -> str
| Evaluate a set of data.
|
| Parameters
| ----------
| evals :
| List of items to be evaluated.
| iteration :
| Current iteration.
| feval :
| Custom evaluation function.
|
| Returns
| -------
| result: str
| Evaluation result string.
|
| get_categories(self, export_to_arrow: bool = False) -> xgboost._data_utils.Categories
| Same method as :py:meth:`DMatrix.get_categories`.
|
| get_dump(self, fmap: Union[str, os.PathLike] = '', with_stats: bool = False, dump_format: str = 'text') -> List[str]
| Returns the model dump as a list of strings. Unlike :py:meth:`save_model`,
| the output format is primarily used for visualization or interpretation, hence
| it's more human readable but cannot be loaded back to XGBoost.
|
| Parameters
| ----------
| fmap :
| Name of the file containing feature map names.
| with_stats :
| Controls whether the split statistics should be included.
| dump_format :
| Format of model dump. Can be 'text', 'json' or 'dot'.
|
| get_fscore(self, fmap: Union[str, os.PathLike] = '') -> Dict[str, Union[float, List[float]]]
| Get feature importance of each feature.
|
| .. note:: Zero-importance features will not be included
|
| Keep in mind that this function does not include zero-importance feature,
| i.e. those features that have not been used in any split conditions.
|
| Parameters
| ----------
| fmap :
| The name of feature map file
|
| get_score(self, fmap: Union[str, os.PathLike] = '', importance_type: str = 'weight') -> Dict[str, Union[float, List[float]]]
| Get feature importance of each feature.
| For tree model Importance type can be defined as:
|
| * 'weight': the number of times a feature is used to split the data across all
| trees.
| * 'gain': the average gain across all splits the feature is used in.
| * 'cover': the average coverage across all splits the feature is used in.
| * 'total_gain': the total gain across all splits the feature is used in.
| * 'total_cover': the total coverage across all splits the feature is used in.
|
| .. note::
|
| For linear model, only "weight" is defined and it's the normalized
| coefficients without bias.
|
| .. note:: Zero-importance features will not be included
|
| Keep in mind that this function does not include zero-importance feature,
| i.e. those features that have not been used in any split conditions.
|
| Parameters
| ----------
| fmap :
| The name of feature map file.
| importance_type :
| One of the importance types defined above.
|
| Returns
| -------
| A map between feature names and their scores. When `gblinear` is used for
| multi-class classification the scores for each feature is a list with length
| `n_classes`, otherwise they're scalars.
|
| get_split_value_histogram(self, feature: str, fmap: Union[str, os.PathLike] = '', bins: Optional[int] = None, as_pandas: bool = True) -> Union[numpy.ndarray, ForwardRef('PdDataFrame')]
| Get split value histogram of a feature
|
| Parameters
| ----------
| feature :
| The name of the feature.
| fmap:
| The name of feature map file.
| bin :
| The maximum number of bins.
| Number of bins equals number of unique split values n_unique,
| if bins == None or bins > n_unique.
| as_pandas :
| Return pd.DataFrame when pandas is installed.
| If False or pandas is not installed, return numpy ndarray.
|
| Returns
| -------
| a histogram of used splitting values for the specified feature
| either as numpy array or pandas DataFrame.
|
| inplace_predict(self, data: Any, *, iteration_range: Tuple[Union[int, numpy.integer], Union[int, numpy.integer]] = (0, 0), predict_type: str = 'value', missing: float = nan, validate_features: bool = True, base_margin: Any = None, strict_shape: bool = False) -> Union[numpy.ndarray, Any]
| Run prediction in-place when possible, Unlike :py:meth:`predict` method,
| inplace prediction does not cache the prediction result.
|
| Calling only ``inplace_predict`` in multiple threads is safe and lock
| free. But the safety does not hold when used in conjunction with other
| methods. E.g. you can't train the booster in one thread and perform
| prediction in the other.
|
| .. note::
|
| If the device ordinal of the input data doesn't match the one configured for
| the booster, data will be copied to the booster device.
|
| .. code-block:: python
|
| booster.set_param({"device": "cuda:0"})
| booster.inplace_predict(cupy_array)
|
| booster.set_param({"device": "cpu"})
| booster.inplace_predict(numpy_array)
|
| .. versionadded:: 1.1.0
|
| Parameters
| ----------
| data :
| The input data.
| iteration_range :
| See :py:meth:`predict` for details.
| predict_type :
| * `value` Output model prediction values.
| * `margin` Output the raw untransformed margin value.
| missing :
| See :py:obj:`xgboost.DMatrix` for details.
| validate_features:
| See :py:meth:`xgboost.Booster.predict` for details.
| base_margin:
| See :py:obj:`xgboost.DMatrix` for details.
|
| .. versionadded:: 1.4.0
|
| strict_shape:
| See :py:meth:`xgboost.Booster.predict` for details.
|
| .. versionadded:: 1.4.0
|
| Returns
| -------
| prediction : numpy.ndarray/cupy.ndarray
| The prediction result. When input data is on GPU, prediction result is
| stored in a cupy array.
|
| load_config(self, config: str) -> None
| Load configuration returned by `save_config`.
|
| .. versionadded:: 1.0.0
|
| load_model(self, fname: Union[os.PathLike[~AnyStr], bytearray, str]) -> None
| Load the model from a file or a bytearray.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| model.load_model("model.json")
|
| # or
| model.save_model("model.ubj")
| model.load_model("model.ubj")
|
| # or
| buf = model.save_raw()
| model.load_model(buf)
|
| Parameters
| ----------
| fname :
| Input file name or memory buffer(see also save_raw)
|
| num_boosted_rounds(self) -> int
| Get number of boosted rounds. For gblinear this is reset to 0 after
| serializing the model.
|
| num_features(self) -> int
| Number of features in booster.
|
| predict(self, data: xgboost.core.DMatrix, *, output_margin: bool = False, pred_leaf: bool = False, pred_contribs: bool = False, approx_contribs: bool = False, pred_interactions: bool = False, validate_features: bool = True, training: bool = False, iteration_range: Tuple[Union[int, numpy.integer], Union[int, numpy.integer]] = (0, 0), strict_shape: bool = False) -> numpy.ndarray
| Predict with data. The full model will be used unless `iteration_range` is
| specified, meaning users have to either slice the model or use the
| ``best_iteration`` attribute to get prediction from best model returned from
| early stopping.
|
| .. note::
|
| See :doc:`Prediction </prediction>` for issues like thread safety and a
| summary of outputs from this function.
|
| Parameters
| ----------
| data :
| The dmatrix storing the input.
|
| output_margin :
| Whether to output the raw untransformed margin value.
|
| pred_leaf :
| When this option is on, the output will be a matrix of (nsample,
| ntrees) with each record indicating the predicted leaf index of
| each sample in each tree. Note that the leaf index of a tree is
| unique per tree, so you may find leaf 1 in both tree 1 and tree 0.
|
| pred_contribs :
| When this is True the output will be a matrix of size (nsample,
| nfeats + 1) with each record indicating the feature contributions
| (SHAP values) for that prediction. The sum of all feature
| contributions is equal to the raw untransformed margin value of the
| prediction. Note the final column is the bias term.
|
| approx_contribs :
| Approximate the contributions of each feature. Used when ``pred_contribs``
| or ``pred_interactions`` is set to True. Changing the default of this
| parameter (False) is not recommended.
|
| pred_interactions :
| When this is True the output will be a matrix of size (nsample,
| nfeats + 1, nfeats + 1) indicating the SHAP interaction values for
| each pair of features. The sum of each row (or column) of the
| interaction values equals the corresponding SHAP value (from
| pred_contribs), and the sum of the entire matrix equals the raw
| untransformed margin value of the prediction. Note the last row and
| column correspond to the bias term.
|
| validate_features :
| When this is True, validate that the Booster's and data's
| feature_names are identical. Otherwise, it is assumed that the
| feature_names are the same.
|
| training :
| Whether the prediction value is used for training. This can effect `dart`
| booster, which performs dropouts during training iterations but use all
| trees for inference. If you want to obtain result with dropouts, set this
| parameter to `True`. Also, the parameter is set to true when obtaining
| prediction for custom objective function.
|
| .. versionadded:: 1.0.0
|
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying `iteration_range=(10,
| 20)`, then only the forests built during [10, 20) (half open set) rounds are
| used in this prediction.
|
| .. versionadded:: 1.4.0
|
| strict_shape :
| When set to True, output shape is invariant to whether classification is
| used. For both value and margin prediction, the output shape is (n_samples,
| n_groups), n_groups == 1 when multi-class is not used. Default to False, in
| which case the output shape can be (n_samples, ) if multi-class is not used.
|
| .. versionadded:: 1.4.0
|
| Returns
| -------
| prediction : numpy array
|
| reset(self) -> 'Booster'
| Reset the booster object to release data caches used for training.
|
| .. versionadded:: 3.0.0
|
| save_config(self) -> str
| Output internal parameter configuration of Booster as a JSON
| string.
|
| .. versionadded:: 1.0.0
|
| save_model(self, fname: Union[str, os.PathLike]) -> None
| Save the model to a file.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| # or
| model.save_model("model.ubj")
|
| Parameters
| ----------
| fname :
| Output file name
|
| save_raw(self, raw_format: str = 'ubj') -> bytearray
| Save the model to a in memory buffer representation instead of file.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| Parameters
| ----------
| raw_format :
| Format of output buffer. Can be `json` or `ubj`.
|
| Returns
| -------
| An in memory buffer representation of the model
|
| set_attr(self, **kwargs: Optional[Any]) -> None
| Set the attribute of the Booster.
|
| Parameters
| ----------
| **kwargs
| The attributes to set. Setting a value to None deletes an attribute.
|
| set_param(self, params: Union[Dict, Iterable[Tuple[str, Any]], str], value: Optional[str] = None) -> None
| Set parameters into the Booster.
|
| Parameters
| ----------
| params :
| list of key,value pairs, dict of key to value or simply str key
| value :
| value of the specified parameter, when params is str key
|
| trees_to_dataframe(self, fmap: Union[str, os.PathLike] = '') -> 'PdDataFrame'
| Parse a boosted tree model text dump into a pandas DataFrame structure.
|
| This feature is only defined when the decision tree model is chosen as base
| learner (`booster in {gbtree, dart}`). It is not defined for other base learner
| types, such as linear learners (`booster=gblinear`).
|
| Parameters
| ----------
| fmap :
| The name of feature map file.
|
| update(self, dtrain: xgboost.core.DMatrix, iteration: int, fobj: Optional[Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[numpy.ndarray, numpy.ndarray]]] = None) -> None
| Update for one iteration, with objective function calculated
| internally.
|
| .. warning::
|
| This function should not be called directly by users.
|
| Parameters
| ----------
| dtrain :
| Training data.
| iteration :
| The current training iteration.
| fobj :
| Custom objective function.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| best_iteration
| The best iteration during training.
|
| best_score
| The best evaluation score during training.
|
| feature_names
| Feature names for this booster. Can be directly set by input data or by
| assignment.
|
| feature_types
| Feature types for this booster. Can be directly set by input data or by
| assignment. See :py:class:`DMatrix` for details.
xgboost.DMatrix
Python Library Documentation: class DMatrix in module xgboost.core class DMatrix(builtins.object) | DMatrix(data: Any, label: Optional[Any] = None, *, weight: Optional[Any] = None, base_margin: Optional[Any] = None, missing: Optional[float] = None, silent: bool = False, feature_names: Optional[Sequence[str]] = None, feature_types: Union[Sequence[str], xgboost._data_utils.Categories, NoneType] = None, nthread: Optional[int] = None, group: Optional[Any] = None, qid: Optional[Any] = None, label_lower_bound: Optional[Any] = None, label_upper_bound: Optional[Any] = None, feature_weights: Optional[Any] = None, enable_categorical: bool = True, data_split_mode: xgboost._typing.DataSplitMode = <DataSplitMode.ROW: 0>) -> None | | Data Matrix used in XGBoost. | | DMatrix is an internal data structure that is used by XGBoost, which is optimized | for both memory efficiency and training speed. You can construct DMatrix from | multiple different sources of data. | | Methods defined here: | | __del__(self) -> None | | __init__(self, data: Any, label: Optional[Any] = None, *, weight: Optional[Any] = None, base_margin: Optional[Any] = None, missing: Optional[float] = None, silent: bool = False, feature_names: Optional[Sequence[str]] = None, feature_types: Union[Sequence[str], xgboost._data_utils.Categories, NoneType] = None, nthread: Optional[int] = None, group: Optional[Any] = None, qid: Optional[Any] = None, label_lower_bound: Optional[Any] = None, label_upper_bound: Optional[Any] = None, feature_weights: Optional[Any] = None, enable_categorical: bool = True, data_split_mode: xgboost._typing.DataSplitMode = <DataSplitMode.ROW: 0>) -> None | Parameters | ---------- | data : | Data source of DMatrix. See :ref:`py-data` for a list of supported input | types. | | Note that, if passing an iterator, it **will cache data on disk**, and note | that fields like ``label`` will be concatenated in-memory from multiple | calls to the iterator. | label : | Label of the training data. | weight : | Weight for each instance. | | .. note:: | | For ranking task, weights are per-group. In ranking task, one weight | is assigned to each group (not each data point). This is because we | only care about the relative ordering of data points within each group, | so it doesn't make sense to assign weights to individual data points. | | base_margin : | Global bias for each instance. See :doc:`/tutorials/intercept` for details. | missing : | Value in the input data which needs to be present as a missing value. If | None, defaults to np.nan. | silent : | Whether print messages during construction | feature_names : | Set names for features. | feature_types : | | Set types for features. If `data` is a DataFrame type and passing | `enable_categorical=True`, the types will be deduced automatically from the | column types. | | Otherwise, one can pass a list-like input with the same length as number of | columns in `data`, with the following possible values: | | - "c", which represents categorical columns. | - "q", which represents numeric columns. | - "int", which represents integer columns. | - "i", which represents boolean columns. | | Note that, while categorical types are treated differently from the rest for | model fitting purposes, the other types do not influence the generated | model, but have effects in other functionalities such as feature | importances. | | For categorical features, the input is assumed to be preprocessed and | encoded by the users. The encoding can be done via | :py:class:`sklearn.preprocessing.OrdinalEncoder` or pandas dataframe | `.cat.codes` method. This is useful when users want to specify categorical | features without having to construct a dataframe as input. | | .. versionadded:: 3.1.0 | | Alternatively, user can pass a :py:class:`~xgboost.core.Categories` object | returned from previous training as a reference for re-coding. One can obtain | the reference with the :py:meth:`.get_categories` from the previous training | DMatrix or the Booster. This feature is experimental. | | nthread : | Number of threads to use for loading data when parallelization is | applicable. If -1, uses maximum threads available on the system. | group : | Group size for all ranking group. | qid : | Query ID for data samples, used for ranking. | label_lower_bound : | Lower bound for survival training. | label_upper_bound : | Upper bound for survival training. | feature_weights : | Set feature weights for column sampling. | enable_categorical : | | .. versionadded:: 1.3.0 | | .. note:: This parameter is experimental | | Experimental support of specializing for categorical features. See | :doc:`/tutorials/categorical` for more info. | | If passing `True` and `data` is a data frame (from supported libraries such | as Pandas, Modin, polars, and cuDF), The DMatrix recognizes categorical | columns and automatically set the `feature_types` parameter. If `data` is | not a data frame, this argument is ignored. | | If passing `False` and `data` is a data frame with categorical columns, it | will result in an error. | | See notes in the :py:class:`DataIter` for consistency requirement when the | input is an iterator. | | .. versionchanged:: 3.1.0 | | XGBoost can remember the encoding of categories when the input is a | dataframe. | | data_split_mode(self) -> xgboost._typing.DataSplitMode | Get the data split mode of the DMatrix. | | .. versionadded:: 2.1.0 | | get_base_margin(self) -> Union[numpy.ndarray, Any] | Get the base margin of the DMatrix. | | get_categories(self, export_to_arrow: bool = False) -> xgboost._data_utils.Categories | Get the categories in the dataset. | | .. versionadded:: 3.1.0 | | .. warning:: | | This function is experimental. | | Parameters | ---------- | export_to_arrow : | The returned container will contain a list of ``pyarrow`` arrays for the | categories. See the :py:meth:`~Categories.to_arrow` for more info. | | get_data(self) -> scipy.sparse._csr.csr_matrix | Get the predictors from DMatrix as a CSR matrix. This getter is mostly for | testing purposes. If this is a quantized DMatrix then quantized values are | returned instead of input values. | | .. versionadded:: 1.7.0 | | get_float_info(self, field: str) -> Union[numpy.ndarray, Any] | Get float property from the DMatrix. | | Parameters | ---------- | field: str | The field name of the information. | | get_group(self) -> numpy.ndarray | Get the group of the DMatrix. | | Returns | ------- | group | | get_label(self) -> Union[numpy.ndarray, Any] | Get the label of the DMatrix. | | get_quantile_cut(self) -> Tuple[numpy.ndarray, numpy.ndarray] | Get quantile cuts for quantization. | | .. versionadded:: 2.0.0 | | get_uint_info(self, field: str) -> Union[numpy.ndarray, Any] | Get unsigned integer property from the DMatrix. | | Parameters | ---------- | field: str | The field name of the information. | | get_weight(self) -> Union[numpy.ndarray, Any] | Get the weight of the DMatrix. | | num_col(self) -> int | Get the number of columns (features) in the DMatrix. | | num_nonmissing(self) -> int | Get the number of non-missing values in the DMatrix. | | .. versionadded:: 1.7.0 | | num_row(self) -> int | Get the number of rows in the DMatrix. | | save_binary(self, fname: Union[str, os.PathLike], silent: bool = True) -> None | Save DMatrix to an XGBoost buffer. Saved binary can be later loaded | by providing the path to :py:func:`xgboost.DMatrix` as input. | | Parameters | ---------- | fname : string or os.PathLike | Name of the output buffer file. | silent : bool (optional; default: True) | If set, the output is suppressed. | | set_base_margin(self, margin: Any) -> None | Set base margin of booster to start from. | | This can be used to specify a prediction value of existing model to be | base_margin However, remember margin is needed, instead of transformed | prediction e.g. for logistic regression: need to put in value before | logistic transformation see also example/demo.py | | Parameters | ---------- | margin: array like | Prediction margin of each datapoint | | set_float_info(self, field: str, data: Any) -> None | Set float type property into the DMatrix. | | Parameters | ---------- | field: str | The field name of the information | | data: numpy array | The array of data to be set | | set_float_info_npy2d(self, field: str, data: Any) -> None | Set float type property into the DMatrix | for numpy 2d array input | | Parameters | ---------- | field: str | The field name of the information | | data: numpy array | The array of data to be set | | set_group(self, group: Any) -> None | Set group size of DMatrix (used for ranking). | | Parameters | ---------- | group : array like | Group size of each group | | set_info(self, *, label: Optional[Any] = None, weight: Optional[Any] = None, base_margin: Optional[Any] = None, group: Optional[Any] = None, qid: Optional[Any] = None, label_lower_bound: Optional[Any] = None, label_upper_bound: Optional[Any] = None, feature_names: Optional[Sequence[str]] = None, feature_types: Optional[Sequence[str]] = None, feature_weights: Optional[Any] = None) -> None | Set meta info for DMatrix. See doc string for :py:obj:`xgboost.DMatrix`. | | set_label(self, label: Any) -> None | Set label of dmatrix | | Parameters | ---------- | label: array like | The label information to be set into DMatrix | | set_uint_info(self, field: str, data: Any) -> None | Set uint type property into the DMatrix. | | Parameters | ---------- | field: str | The field name of the information | | data: numpy array | The array of data to be set | | set_weight(self, weight: Any) -> None | Set weight of each instance. | | Parameters | ---------- | weight : array like | Weight for each data point | | .. note:: For ranking task, weights are per-group. | | In ranking task, one weight is assigned to each group (not each | data point). This is because we only care about the relative | ordering of data points within each group, so it doesn't make | sense to assign weights to individual data points. | | slice(self, rindex: Union[List[int], numpy.ndarray], allow_groups: bool = False) -> 'DMatrix' | Slice the DMatrix and return a new DMatrix that only contains `rindex`. | | Parameters | ---------- | rindex | List of indices to be selected. | allow_groups | Allow slicing of a matrix with a groups attribute | | Returns | ------- | res | A new DMatrix containing only selected indices. | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables | | __weakref__ | list of weak references to the object | | feature_names | Labels for features (column labels). | | Setting it to ``None`` resets existing feature names. | | feature_types | Type of features (column types). | | This is for displaying the results and categorical data support. See | :py:class:`DMatrix` for details. | | Setting it to ``None`` resets existing feature types.
xgboost.DataIter
Python Library Documentation: class DataIter in module xgboost.core
class DataIter(abc.ABC)
| DataIter(cache_prefix: Optional[str] = None, release_data: bool = True, *, on_host: bool = True, min_cache_page_bytes: Optional[int] = None) -> None
|
| The interface for user defined data iterator. The iterator facilitates
| distributed training, :py:class:`QuantileDMatrix`, and external memory support using
| :py:class:`DMatrix` or :py:class:`ExtMemQuantileDMatrix`. Most of time, users don't
| need to interact with this class directly.
|
| .. note::
|
| The class caches some intermediate results using the `data` input (predictor
| `X`) as key. Don't repeat the `X` for multiple batches with different meta data
| (like `label`), make a copy if necessary.
|
| .. note::
|
| When the input for each batch is a DataFrame, we assume categories are
| consistently encoded for all batches. For example, given two dataframes for two
| batches, this is invalid:
|
| .. code-block::
|
| import pandas as pd
|
| x0 = pd.DataFrame({"a": [0, 1]}, dtype="category")
| x1 = pd.DataFrame({"a": [1, 2]}, dtype="category")
|
| This is invalid because the `x0` has `[0, 1]` as categories while `x2` has `[1,
| 2]`. They should share the same set of categories and encoding:
|
| .. code-block::
|
| import numpy as np
|
| categories = np.array([0, 1, 2])
| x0["a"] = pd.Categorical.from_codes(
| codes=np.array([0, 1]), categories=categories
| )
| x1["a"] = pd.Categorical.from_codes(
| codes=np.array([1, 2]), categories=categories
| )
|
| You can make sure the consistent encoding in your preprocessing step be careful
| that the data is stored in formats that preserve the encoding when chunking the
| data.
|
| Parameters
| ----------
| cache_prefix :
| Prefix to the cache files, only used in external memory.
|
| Note that using this class for external memory **will cache data
| on disk** under the path passed here.
|
| release_data :
| Whether the iterator should release the data during iteration. Set it to True if
| the data transformation (converting data to np.float32 type) is memory
| intensive. Otherwise, if the transformation is computation intensive then we can
| keep the cache.
|
| on_host :
| Whether the data should be cached on the host memory instead of the file system
| when using GPU with external memory. When set to true (the default), the
| "external memory" is the CPU (host) memory. See
| :doc:`/tutorials/external_memory` for more info.
|
| .. versionadded:: 3.0.0
|
| .. warning::
|
| This is an experimental parameter and subject to change.
|
| min_cache_page_bytes :
| The minimum number of bytes of each cached pages. Only used for on-host cache
| with GPU-based :py:class:`ExtMemQuantileDMatrix`. When using GPU-based external
| memory with the data cached in the host memory, XGBoost can concatenate the
| pages internally to increase the batch size for the GPU. The default page size
| is about 1/16 of the total device memory. Users can manually set the value based
| on the actual hardware and datasets. Set this to 0 to disable page
| concatenation.
|
| .. versionadded:: 3.0.0
|
| .. warning::
|
| This is an experimental parameter and subject to change.
|
| Method resolution order:
| DataIter
| abc.ABC
| builtins.object
|
| Methods defined here:
|
| __del__(self) -> None
|
| __init__(self, cache_prefix: Optional[str] = None, release_data: bool = True, *, on_host: bool = True, min_cache_page_bytes: Optional[int] = None) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| get_callbacks(self, enable_categorical: bool) -> Tuple[Callable, Callable]
| Get callback functions for iterating in C. This is an internal function.
|
| next(self, input_data: Callable) -> bool
| Set the next batch of data.
|
| Parameters
| ----------
|
| input_data:
| A function with same data fields like `data`, `label` with
| `xgboost.DMatrix`.
|
| Returns
| -------
| False if there's no more batch, otherwise True.
|
| reraise(self) -> None
| Reraise the exception thrown during iteration.
|
| reset(self) -> None
| Reset the data iterator. Prototype for user defined function.
|
| ----------------------------------------------------------------------
| Readonly properties defined here:
|
| proxy
| Handle of DMatrix proxy.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset({'next', 'reset'})
xgboost.ExtMemQuantileDMatrix
Python Library Documentation: class ExtMemQuantileDMatrix in module xgboost.core class ExtMemQuantileDMatrix(DMatrix, _RefMixIn) | ExtMemQuantileDMatrix(data: xgboost.core.DataIter, *, missing: Optional[float] = None, nthread: Optional[int] = None, max_bin: Optional[int] = None, ref: Optional[xgboost.core.DMatrix] = None, enable_categorical: bool = True, max_quantile_batches: Optional[int] = None, cache_host_ratio: Optional[float] = None) -> None | | The external memory version of the :py:class:`QuantileDMatrix`. | | See :doc:`/tutorials/external_memory` for explanation and usage examples, and | :py:class:`QuantileDMatrix` for parameter document. | | .. warning:: | | This is an experimental feature and subject to change. | | .. versionadded:: 3.0.0 | | Method resolution order: | ExtMemQuantileDMatrix | DMatrix | _RefMixIn | builtins.object | | Methods defined here: | | __init__(self, data: xgboost.core.DataIter, *, missing: Optional[float] = None, nthread: Optional[int] = None, max_bin: Optional[int] = None, ref: Optional[xgboost.core.DMatrix] = None, enable_categorical: bool = True, max_quantile_batches: Optional[int] = None, cache_host_ratio: Optional[float] = None) -> None | Parameters | ---------- | data : | A user-defined :py:class:`DataIter` for loading data. | | max_quantile_batches : | Deprecated. See :py:class:`QuantileDMatrix`. | | cache_host_ratio : | | .. versionadded:: 3.1.0 | | Used by the GPU implementation. For GPU-based inputs, XGBoost can split the | cache into host and device caches to reduce the data transfer overhead. This | parameter specifies the size of host cache compared to the size of the | entire cache: :math:`host / (host + device)`. | | See :ref:`extmem-adaptive-cache` for more info. | | ---------------------------------------------------------------------- | Methods inherited from DMatrix: | | __del__(self) -> None | | data_split_mode(self) -> xgboost._typing.DataSplitMode | Get the data split mode of the DMatrix. | | .. versionadded:: 2.1.0 | | get_base_margin(self) -> Union[numpy.ndarray, Any] | Get the base margin of the DMatrix. | | get_categories(self, export_to_arrow: bool = False) -> xgboost._data_utils.Categories | Get the categories in the dataset. | | .. versionadded:: 3.1.0 | | .. warning:: | | This function is experimental. | | Parameters | ---------- | export_to_arrow : | The returned container will contain a list of ``pyarrow`` arrays for the | categories. See the :py:meth:`~Categories.to_arrow` for more info. | | get_data(self) -> scipy.sparse._csr.csr_matrix | Get the predictors from DMatrix as a CSR matrix. This getter is mostly for | testing purposes. If this is a quantized DMatrix then quantized values are | returned instead of input values. | | .. versionadded:: 1.7.0 | | get_float_info(self, field: str) -> Union[numpy.ndarray, Any] | Get float property from the DMatrix. | | Parameters | ---------- | field: str | The field name of the information. | | get_group(self) -> numpy.ndarray | Get the group of the DMatrix. | | Returns | ------- | group | | get_label(self) -> Union[numpy.ndarray, Any] | Get the label of the DMatrix. | | get_quantile_cut(self) -> Tuple[numpy.ndarray, numpy.ndarray] | Get quantile cuts for quantization. | | .. versionadded:: 2.0.0 | | get_uint_info(self, field: str) -> Union[numpy.ndarray, Any] | Get unsigned integer property from the DMatrix. | | Parameters | ---------- | field: str | The field name of the information. | | get_weight(self) -> Union[numpy.ndarray, Any] | Get the weight of the DMatrix. | | num_col(self) -> int | Get the number of columns (features) in the DMatrix. | | num_nonmissing(self) -> int | Get the number of non-missing values in the DMatrix. | | .. versionadded:: 1.7.0 | | num_row(self) -> int | Get the number of rows in the DMatrix. | | save_binary(self, fname: Union[str, os.PathLike], silent: bool = True) -> None | Save DMatrix to an XGBoost buffer. Saved binary can be later loaded | by providing the path to :py:func:`xgboost.DMatrix` as input. | | Parameters | ---------- | fname : string or os.PathLike | Name of the output buffer file. | silent : bool (optional; default: True) | If set, the output is suppressed. | | set_base_margin(self, margin: Any) -> None | Set base margin of booster to start from. | | This can be used to specify a prediction value of existing model to be | base_margin However, remember margin is needed, instead of transformed | prediction e.g. for logistic regression: need to put in value before | logistic transformation see also example/demo.py | | Parameters | ---------- | margin: array like | Prediction margin of each datapoint | | set_float_info(self, field: str, data: Any) -> None | Set float type property into the DMatrix. | | Parameters | ---------- | field: str | The field name of the information | | data: numpy array | The array of data to be set | | set_float_info_npy2d(self, field: str, data: Any) -> None | Set float type property into the DMatrix | for numpy 2d array input | | Parameters | ---------- | field: str | The field name of the information | | data: numpy array | The array of data to be set | | set_group(self, group: Any) -> None | Set group size of DMatrix (used for ranking). | | Parameters | ---------- | group : array like | Group size of each group | | set_info(self, *, label: Optional[Any] = None, weight: Optional[Any] = None, base_margin: Optional[Any] = None, group: Optional[Any] = None, qid: Optional[Any] = None, label_lower_bound: Optional[Any] = None, label_upper_bound: Optional[Any] = None, feature_names: Optional[Sequence[str]] = None, feature_types: Optional[Sequence[str]] = None, feature_weights: Optional[Any] = None) -> None | Set meta info for DMatrix. See doc string for :py:obj:`xgboost.DMatrix`. | | set_label(self, label: Any) -> None | Set label of dmatrix | | Parameters | ---------- | label: array like | The label information to be set into DMatrix | | set_uint_info(self, field: str, data: Any) -> None | Set uint type property into the DMatrix. | | Parameters | ---------- | field: str | The field name of the information | | data: numpy array | The array of data to be set | | set_weight(self, weight: Any) -> None | Set weight of each instance. | | Parameters | ---------- | weight : array like | Weight for each data point | | .. note:: For ranking task, weights are per-group. | | In ranking task, one weight is assigned to each group (not each | data point). This is because we only care about the relative | ordering of data points within each group, so it doesn't make | sense to assign weights to individual data points. | | slice(self, rindex: Union[List[int], numpy.ndarray], allow_groups: bool = False) -> 'DMatrix' | Slice the DMatrix and return a new DMatrix that only contains `rindex`. | | Parameters | ---------- | rindex | List of indices to be selected. | allow_groups | Allow slicing of a matrix with a groups attribute | | Returns | ------- | res | A new DMatrix containing only selected indices. | | ---------------------------------------------------------------------- | Data descriptors inherited from DMatrix: | | __dict__ | dictionary for instance variables | | __weakref__ | list of weak references to the object | | feature_names | Labels for features (column labels). | | Setting it to ``None`` resets existing feature names. | | feature_types | Type of features (column types). | | This is for displaying the results and categorical data support. See | :py:class:`DMatrix` for details. | | Setting it to ``None`` resets existing feature types. | | ---------------------------------------------------------------------- | Data descriptors inherited from _RefMixIn: | | ref | Internal method for retrieving a reference to the training DMatrix.
xgboost.QuantileDMatrix
Python Library Documentation: class QuantileDMatrix in module xgboost.core class QuantileDMatrix(DMatrix, _RefMixIn) | QuantileDMatrix(data: Any, label: Optional[Any] = None, *, weight: Optional[Any] = None, base_margin: Optional[Any] = None, missing: Optional[float] = None, silent: bool = False, feature_names: Optional[Sequence[str]] = None, feature_types: Optional[Sequence[str]] = None, nthread: Optional[int] = None, max_bin: Optional[int] = None, ref: Optional[xgboost.core.DMatrix] = None, group: Optional[Any] = None, qid: Optional[Any] = None, label_lower_bound: Optional[Any] = None, label_upper_bound: Optional[Any] = None, feature_weights: Optional[Any] = None, enable_categorical: bool = True, max_quantile_batches: Optional[int] = None, data_split_mode: xgboost._typing.DataSplitMode = <DataSplitMode.ROW: 0>) -> None | | A DMatrix variant that generates quantilized data directly from input for the | ``hist`` tree method. This DMatrix is primarily designed to save memory in training | by avoiding intermediate storage. Set ``max_bin`` to control the number of bins | during quantisation, which should be consistent with the training parameter | ``max_bin``. When ``QuantileDMatrix`` is used for validation/test dataset, ``ref`` | should be another ``QuantileDMatrix`` or ``DMatrix``, but not recommended as it | defeats the purpose of saving memory) constructed from training dataset. See | :py:obj:`xgboost.DMatrix` for documents on meta info. | | .. note:: | | Do not use ``QuantileDMatrix`` as validation/test dataset without supplying a | reference (the training dataset) ``QuantileDMatrix`` using ``ref`` as some | information may be lost in quantisation. | | .. versionadded:: 1.7.0 | | Examples | -------- | | .. code-block:: | | from sklearn.datasets import make_regression | from sklearn.model_selection import train_test_split | | X, y = make_regression() | X_train, X_test, y_train, y_test = train_test_split(X, y) | Xy_train = xgb.QuantileDMatrix(X_train, y_train) | # It's necessary to have the training DMatrix as a reference for valid | # quantiles. | Xy_test = xgb.QuantileDMatrix(X_test, y_test, ref=Xy_train) | | Parameters | ---------- | max_bin : | The number of histogram bin, should be consistent with the training parameter | ``max_bin``. | | ref : | The training dataset that provides quantile information, needed when creating | validation/test dataset with ``QuantileDMatrix``. Supplying the training DMatrix | as a reference means that the same quantisation applied to the training data is | applied to the validation/test data | | max_quantile_batches : | Deprecated. This parameter no longer has any effect and will be removed in a | future release. | | .. versionadded:: 3.0.0 | | .. deprecated:: 3.3.0 | | Method resolution order: | QuantileDMatrix | DMatrix | _RefMixIn | builtins.object | | Methods defined here: | | __init__(self, data: Any, label: Optional[Any] = None, *, weight: Optional[Any] = None, base_margin: Optional[Any] = None, missing: Optional[float] = None, silent: bool = False, feature_names: Optional[Sequence[str]] = None, feature_types: Optional[Sequence[str]] = None, nthread: Optional[int] = None, max_bin: Optional[int] = None, ref: Optional[xgboost.core.DMatrix] = None, group: Optional[Any] = None, qid: Optional[Any] = None, label_lower_bound: Optional[Any] = None, label_upper_bound: Optional[Any] = None, feature_weights: Optional[Any] = None, enable_categorical: bool = True, max_quantile_batches: Optional[int] = None, data_split_mode: xgboost._typing.DataSplitMode = <DataSplitMode.ROW: 0>) -> None | Parameters | ---------- | data : | Data source of DMatrix. See :ref:`py-data` for a list of supported input | types. | | Note that, if passing an iterator, it **will cache data on disk**, and note | that fields like ``label`` will be concatenated in-memory from multiple | calls to the iterator. | label : | Label of the training data. | weight : | Weight for each instance. | | .. note:: | | For ranking task, weights are per-group. In ranking task, one weight | is assigned to each group (not each data point). This is because we | only care about the relative ordering of data points within each group, | so it doesn't make sense to assign weights to individual data points. | | base_margin : | Global bias for each instance. See :doc:`/tutorials/intercept` for details. | missing : | Value in the input data which needs to be present as a missing value. If | None, defaults to np.nan. | silent : | Whether print messages during construction | feature_names : | Set names for features. | feature_types : | | Set types for features. If `data` is a DataFrame type and passing | `enable_categorical=True`, the types will be deduced automatically from the | column types. | | Otherwise, one can pass a list-like input with the same length as number of | columns in `data`, with the following possible values: | | - "c", which represents categorical columns. | - "q", which represents numeric columns. | - "int", which represents integer columns. | - "i", which represents boolean columns. | | Note that, while categorical types are treated differently from the rest for | model fitting purposes, the other types do not influence the generated | model, but have effects in other functionalities such as feature | importances. | | For categorical features, the input is assumed to be preprocessed and | encoded by the users. The encoding can be done via | :py:class:`sklearn.preprocessing.OrdinalEncoder` or pandas dataframe | `.cat.codes` method. This is useful when users want to specify categorical | features without having to construct a dataframe as input. | | .. versionadded:: 3.1.0 | | Alternatively, user can pass a :py:class:`~xgboost.core.Categories` object | returned from previous training as a reference for re-coding. One can obtain | the reference with the :py:meth:`.get_categories` from the previous training | DMatrix or the Booster. This feature is experimental. | | nthread : | Number of threads to use for loading data when parallelization is | applicable. If -1, uses maximum threads available on the system. | group : | Group size for all ranking group. | qid : | Query ID for data samples, used for ranking. | label_lower_bound : | Lower bound for survival training. | label_upper_bound : | Upper bound for survival training. | feature_weights : | Set feature weights for column sampling. | enable_categorical : | | .. versionadded:: 1.3.0 | | .. note:: This parameter is experimental | | Experimental support of specializing for categorical features. See | :doc:`/tutorials/categorical` for more info. | | If passing `True` and `data` is a data frame (from supported libraries such | as Pandas, Modin, polars, and cuDF), The DMatrix recognizes categorical | columns and automatically set the `feature_types` parameter. If `data` is | not a data frame, this argument is ignored. | | If passing `False` and `data` is a data frame with categorical columns, it | will result in an error. | | See notes in the :py:class:`DataIter` for consistency requirement when the | input is an iterator. | | .. versionchanged:: 3.1.0 | | XGBoost can remember the encoding of categories when the input is a | dataframe. | | ---------------------------------------------------------------------- | Methods inherited from DMatrix: | | __del__(self) -> None | | data_split_mode(self) -> xgboost._typing.DataSplitMode | Get the data split mode of the DMatrix. | | .. versionadded:: 2.1.0 | | get_base_margin(self) -> Union[numpy.ndarray, Any] | Get the base margin of the DMatrix. | | get_categories(self, export_to_arrow: bool = False) -> xgboost._data_utils.Categories | Get the categories in the dataset. | | .. versionadded:: 3.1.0 | | .. warning:: | | This function is experimental. | | Parameters | ---------- | export_to_arrow : | The returned container will contain a list of ``pyarrow`` arrays for the | categories. See the :py:meth:`~Categories.to_arrow` for more info. | | get_data(self) -> scipy.sparse._csr.csr_matrix | Get the predictors from DMatrix as a CSR matrix. This getter is mostly for | testing purposes. If this is a quantized DMatrix then quantized values are | returned instead of input values. | | .. versionadded:: 1.7.0 | | get_float_info(self, field: str) -> Union[numpy.ndarray, Any] | Get float property from the DMatrix. | | Parameters | ---------- | field: str | The field name of the information. | | get_group(self) -> numpy.ndarray | Get the group of the DMatrix. | | Returns | ------- | group | | get_label(self) -> Union[numpy.ndarray, Any] | Get the label of the DMatrix. | | get_quantile_cut(self) -> Tuple[numpy.ndarray, numpy.ndarray] | Get quantile cuts for quantization. | | .. versionadded:: 2.0.0 | | get_uint_info(self, field: str) -> Union[numpy.ndarray, Any] | Get unsigned integer property from the DMatrix. | | Parameters | ---------- | field: str | The field name of the information. | | get_weight(self) -> Union[numpy.ndarray, Any] | Get the weight of the DMatrix. | | num_col(self) -> int | Get the number of columns (features) in the DMatrix. | | num_nonmissing(self) -> int | Get the number of non-missing values in the DMatrix. | | .. versionadded:: 1.7.0 | | num_row(self) -> int | Get the number of rows in the DMatrix. | | save_binary(self, fname: Union[str, os.PathLike], silent: bool = True) -> None | Save DMatrix to an XGBoost buffer. Saved binary can be later loaded | by providing the path to :py:func:`xgboost.DMatrix` as input. | | Parameters | ---------- | fname : string or os.PathLike | Name of the output buffer file. | silent : bool (optional; default: True) | If set, the output is suppressed. | | set_base_margin(self, margin: Any) -> None | Set base margin of booster to start from. | | This can be used to specify a prediction value of existing model to be | base_margin However, remember margin is needed, instead of transformed | prediction e.g. for logistic regression: need to put in value before | logistic transformation see also example/demo.py | | Parameters | ---------- | margin: array like | Prediction margin of each datapoint | | set_float_info(self, field: str, data: Any) -> None | Set float type property into the DMatrix. | | Parameters | ---------- | field: str | The field name of the information | | data: numpy array | The array of data to be set | | set_float_info_npy2d(self, field: str, data: Any) -> None | Set float type property into the DMatrix | for numpy 2d array input | | Parameters | ---------- | field: str | The field name of the information | | data: numpy array | The array of data to be set | | set_group(self, group: Any) -> None | Set group size of DMatrix (used for ranking). | | Parameters | ---------- | group : array like | Group size of each group | | set_info(self, *, label: Optional[Any] = None, weight: Optional[Any] = None, base_margin: Optional[Any] = None, group: Optional[Any] = None, qid: Optional[Any] = None, label_lower_bound: Optional[Any] = None, label_upper_bound: Optional[Any] = None, feature_names: Optional[Sequence[str]] = None, feature_types: Optional[Sequence[str]] = None, feature_weights: Optional[Any] = None) -> None | Set meta info for DMatrix. See doc string for :py:obj:`xgboost.DMatrix`. | | set_label(self, label: Any) -> None | Set label of dmatrix | | Parameters | ---------- | label: array like | The label information to be set into DMatrix | | set_uint_info(self, field: str, data: Any) -> None | Set uint type property into the DMatrix. | | Parameters | ---------- | field: str | The field name of the information | | data: numpy array | The array of data to be set | | set_weight(self, weight: Any) -> None | Set weight of each instance. | | Parameters | ---------- | weight : array like | Weight for each data point | | .. note:: For ranking task, weights are per-group. | | In ranking task, one weight is assigned to each group (not each | data point). This is because we only care about the relative | ordering of data points within each group, so it doesn't make | sense to assign weights to individual data points. | | slice(self, rindex: Union[List[int], numpy.ndarray], allow_groups: bool = False) -> 'DMatrix' | Slice the DMatrix and return a new DMatrix that only contains `rindex`. | | Parameters | ---------- | rindex | List of indices to be selected. | allow_groups | Allow slicing of a matrix with a groups attribute | | Returns | ------- | res | A new DMatrix containing only selected indices. | | ---------------------------------------------------------------------- | Data descriptors inherited from DMatrix: | | __dict__ | dictionary for instance variables | | __weakref__ | list of weak references to the object | | feature_names | Labels for features (column labels). | | Setting it to ``None`` resets existing feature names. | | feature_types | Type of features (column types). | | This is for displaying the results and categorical data support. See | :py:class:`DMatrix` for details. | | Setting it to ``None`` resets existing feature types. | | ---------------------------------------------------------------------- | Data descriptors inherited from _RefMixIn: | | ref | Internal method for retrieving a reference to the training DMatrix.
xgboost.RabitTracker
Python Library Documentation: class RabitTracker in module xgboost.tracker
class RabitTracker(builtins.object)
| RabitTracker(n_workers: int, host_ip: Optional[str], port: int = 0, *, sortby: str = 'host', timeout: int = 0) -> None
|
| Tracker for the collective used in XGBoost, acting as a coordinator between
| workers.
|
| Parameters
| ----------
|
| n_workers:
|
| The total number of workers in the communication group.
|
| host_ip:
|
| The IP address of the tracker node. XGBoost can try to guess one by probing with
| sockets. But it's best to explicitly pass an address.
|
| port:
|
| The port this tracker should listen to. XGBoost can query an available port from
| the OS, this configuration is useful for restricted network environments.
|
| sortby:
|
| How to sort the workers for rank assignment. The default is host, but users can
| set the `DMLC_TASK_ID` via arguments of :py:meth:`~xgboost.collective.init` and
| obtain deterministic rank assignment through sorting by task name. Available
| options are:
|
| - host
| - task
|
| timeout :
|
| Timeout for constructing (bootstrap) and shutting down the communication group,
| doesn't apply to communication when the group is up and running.
|
| The timeout value should take the time of data loading and pre-processing into
| account, due to potential lazy execution. By default the Tracker doesn't have
| any timeout to avoid pre-mature aborting.
|
| The :py:meth:`.wait_for` method has a different timeout parameter that can stop
| the tracker even if the tracker is still being used. A value error is raised
| when timeout is reached.
|
| Examples
| --------
|
| .. code-block:: python
|
| from xgboost.tracker import RabitTracker
| from xgboost import collective as coll
|
| tracker = RabitTracker(host_ip="127.0.0.1", n_workers=2)
| tracker.start()
|
| with coll.CommunicatorContext(**tracker.worker_args()):
| ret = coll.broadcast("msg", 0)
| assert str(ret) == "msg"
|
| Methods defined here:
|
| __del__(self) -> None
|
| __init__(self, n_workers: int, host_ip: Optional[str], port: int = 0, *, sortby: str = 'host', timeout: int = 0) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| free(self) -> None
| Internal function for testing.
|
| start(self) -> None
| Start the tracker. Once started, the client still need to call the
| :py:meth:`wait_for` method in order to wait for it to finish (think of it as a
| thread).
|
| wait_for(self, timeout: Optional[int] = None) -> None
| Wait for the tracker to finish all the work and shutdown. When timeout is
| reached, a value error is raised. By default we don't have timeout since we
| don't know how long it takes for the model to finish training.
|
| worker_args(self) -> Dict[str, Union[int, str]]
| Get arguments for workers.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
xgboost.XGBClassifier
Python Library Documentation: class XGBClassifier in module xgboost.sklearn
class XGBClassifier(sklearn.base.ClassifierMixin, XGBModel)
| XGBClassifier(*, objective: Union[str, xgboost.sklearn._SklObjWProto, Callable[[Any, Any], Tuple[numpy.ndarray, numpy.ndarray]], NoneType] = 'binary:logistic', **kwargs: Any) -> None
|
| Implementation of the scikit-learn API for XGBoost classification.
| See :doc:`/python/sklearn_estimator` for more information.
|
| Parameters
| ----------
|
| n_estimators : Optional[int]
| Number of boosting rounds.
|
| max_depth : typing.Optional[int]
|
| Maximum tree depth for base learners.
|
| max_leaves : typing.Optional[int]
|
| Maximum number of leaves; 0 indicates no limit.
|
| max_bin : typing.Optional[int]
|
| If using histogram-based algorithm, maximum number of bins per feature
|
| grow_policy : typing.Optional[str]
|
| Tree growing policy.
|
| - depthwise: Favors splitting at nodes closest to the node,
| - lossguide: Favors splitting at nodes with highest loss change.
|
| learning_rate : typing.Optional[float]
|
| Boosting learning rate (xgb's "eta")
|
| verbosity : typing.Optional[int]
|
| The degree of verbosity. Valid values are 0 (silent) - 3 (debug).
|
| objective : typing.Union[str, xgboost.sklearn._SklObjWProto, typing.Callable[[typing.Any, typing.Any], typing.Tuple[numpy.ndarray, numpy.ndarray]], NoneType]
|
| Specify the learning task and the corresponding learning objective or a custom
| objective function to be used.
|
| For custom objective, see :doc:`/tutorials/custom_metric_obj` and
| :ref:`custom-obj-metric` for more information, along with the end note for
| function signatures.
|
| booster: typing.Optional[str]
|
| Specify which booster to use: ``gbtree``, ``gblinear`` or ``dart``.
|
| .. deprecated:: 3.3.0
|
| ``gblinear`` is deprecated and support will be removed in a future release.
|
| tree_method : typing.Optional[str]
|
| Specify which tree method to use. Default to auto. If this parameter is set to
| default, XGBoost will choose the most conservative option available. It's
| recommended to study this option from the parameters document :doc:`tree method
| </treemethod>`
|
| n_jobs : typing.Optional[int]
|
| Number of parallel threads used to run xgboost. When used with other
| Scikit-Learn algorithms like grid search, you may choose which algorithm to
| parallelize and balance the threads. Creating thread contention will
| significantly slow down both algorithms.
|
| gamma : typing.Optional[float]
|
| (min_split_loss) Minimum loss reduction required to make a further partition on
| a leaf node of the tree.
|
| min_child_weight : typing.Optional[float]
|
| Minimum sum of instance weight(hessian) needed in a child.
|
| max_delta_step : typing.Optional[float]
|
| Maximum delta step we allow each tree's weight estimation to be.
|
| subsample : typing.Optional[float]
|
| Subsample ratio of the training instance.
|
| sampling_method : typing.Optional[str]
|
| Sampling method. Used only by the GPU version of ``hist`` tree method.
|
| - ``uniform``: Select random training instances uniformly.
| - ``gradient_based``: Select random training instances with higher probability
| when the gradient and hessian are larger. (cf. CatBoost)
|
| colsample_bytree : typing.Optional[float]
|
| Subsample ratio of columns when constructing each tree.
|
| colsample_bylevel : typing.Optional[float]
|
| Subsample ratio of columns for each level.
|
| colsample_bynode : typing.Optional[float]
|
| Subsample ratio of columns for each split.
|
| reg_alpha : typing.Optional[float]
|
| L1 regularization term on weights (xgb's alpha).
|
| reg_lambda : typing.Optional[float]
|
| L2 regularization term on weights (xgb's lambda).
|
| scale_pos_weight : typing.Optional[float]
| Balancing of positive and negative weights.
|
| base_score : typing.Union[float, typing.List[float], NoneType]
|
| The initial prediction score of all instances, global bias.
|
| random_state : typing.Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType]
|
| Random number seed.
|
| .. note::
|
| Using gblinear booster with shotgun updater is nondeterministic as
| it uses Hogwild algorithm.
|
| missing : float
|
| Value in the data which needs to be present as a missing value. Default to
| :py:data:`numpy.nan`.
|
| num_parallel_tree: typing.Optional[int]
|
| Used for boosting random forest.
|
| monotone_constraints : typing.Union[typing.Dict[str, int], str, NoneType]
|
| Constraint of variable monotonicity. See :doc:`tutorial </tutorials/monotonic>`
| for more information.
|
| interaction_constraints : typing.Union[str, typing.List[typing.Tuple[str]], NoneType]
|
| Constraints for interaction representing permitted interactions. The
| constraints must be specified in the form of a nested list, e.g. ``[[0, 1], [2,
| 3, 4]]``, where each inner list is a group of indices of features that are
| allowed to interact with each other. See :doc:`tutorial
| </tutorials/feature_interaction_constraint>` for more information
|
| importance_type: typing.Optional[str]
|
| The feature importance type for the feature_importances\_ property:
|
| * For tree model, it's either "gain", "weight", "cover", "total_gain" or
| "total_cover".
| * For linear model, only "weight" is defined and it's the normalized
| coefficients without bias.
|
| device : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| Device ordinal, available options are `cpu`, `cuda`, and `gpu`.
|
| validate_parameters : typing.Optional[bool]
|
| Give warnings for unknown parameter.
|
| enable_categorical : bool
|
| See the same parameter of :py:class:`DMatrix` for details.
|
| feature_types : typing.Optional[typing.Sequence[str]]
|
| .. versionadded:: 1.7.0
|
| Used for specifying feature types without constructing a dataframe. See
| the :py:class:`DMatrix` for details.
|
| feature_weights : Optional[ArrayLike]
|
| Weight for each feature, defines the probability of each feature being selected
| when colsample is being used. All values must be greater than 0, otherwise a
| `ValueError` is thrown.
|
| max_cat_to_onehot : Optional[int]
|
| .. versionadded:: 1.6.0
|
| .. note:: This parameter is experimental
|
| A threshold for deciding whether XGBoost should use one-hot encoding based split
| for categorical data. When number of categories is lesser than the threshold
| then one-hot encoding is chosen, otherwise the categories will be partitioned
| into children nodes. Also, `enable_categorical` needs to be set to have
| categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| max_cat_threshold : typing.Optional[int]
|
| .. versionadded:: 1.7.0
|
| .. note:: This parameter is experimental
|
| Maximum number of categories considered for each split. Used only by
| partition-based splits for preventing over-fitting. Also, `enable_categorical`
| needs to be set to have categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| multi_strategy : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| .. note:: This parameter is working-in-progress.
|
| The strategy used for training multi-target models, including multi-target
| regression and multi-class classification. See :doc:`/tutorials/multioutput` for
| more information.
|
| - ``one_output_per_tree``: One model for each target.
| - ``multi_output_tree``: Use multi-target trees.
|
| eval_metric : typing.Union[str, typing.List[typing.Union[str, typing.Callable]], typing.Callable, NoneType]
|
| .. versionadded:: 1.6.0
|
| Metric used for monitoring the training result and early stopping. It can be a
| string or list of strings as names of predefined metric in XGBoost (See
| :doc:`/parameter`), one of the metrics in :py:mod:`sklearn.metrics`, or any
| other user defined metric that looks like `sklearn.metrics`.
|
| If custom objective is also provided, then custom metric should implement the
| corresponding reverse link function.
|
| Unlike the `scoring` parameter commonly used in scikit-learn, when a callable
| object is provided, it's assumed to be a cost function and by default XGBoost
| will minimize the result during early stopping.
|
| For advanced usage on Early stopping like directly choosing to maximize instead
| of minimize, see :py:obj:`xgboost.callback.EarlyStopping`.
|
| See :doc:`/tutorials/custom_metric_obj` and :ref:`custom-obj-metric` for more
| information.
|
| .. code-block:: python
|
| from sklearn.datasets import load_diabetes
| from sklearn.metrics import mean_absolute_error
| X, y = load_diabetes(return_X_y=True)
| reg = xgb.XGBRegressor(
| tree_method="hist",
| eval_metric=mean_absolute_error,
| )
| reg.fit(X, y, eval_set=[(X, y)])
|
| early_stopping_rounds : typing.Optional[int]
|
| .. versionadded:: 1.6.0
|
| - Activates early stopping. Validation metric needs to improve at least once in
| every **early_stopping_rounds** round(s) to continue training. Requires at
| least one item in **eval_set** in :py:meth:`fit`.
|
| - If early stopping occurs, the model will have two additional attributes:
| :py:attr:`best_score` and :py:attr:`best_iteration`. These are used by the
| :py:meth:`predict` and :py:meth:`apply` methods to determine the optimal
| number of trees during inference. If users want to access the full model
| (including trees built after early stopping), they can specify the
| `iteration_range` in these inference methods. In addition, other utilities
| like model plotting can also use the entire model.
|
| - If you prefer to discard the trees after `best_iteration`, consider using the
| callback function :py:class:`xgboost.callback.EarlyStopping`.
|
| - If there's more than one item in **eval_set**, the last entry will be used for
| early stopping. If there's more than one metric in **eval_metric**, the last
| metric will be used for early stopping.
|
| callbacks : typing.Optional[typing.List[xgboost.callback.TrainingCallback]]
|
| List of callback functions that are applied at end of each iteration.
| It is possible to use predefined callbacks by using
| :ref:`Callback API <callback_api>`.
|
| .. note::
|
| States in callback are not preserved during training, which means callback
| objects can not be reused for multiple training sessions without
| reinitialization or deepcopy.
|
| .. code-block:: python
|
| for params in parameters_grid:
| # be sure to (re)initialize the callbacks before each run
| callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
| reg = xgboost.XGBRegressor(**params, callbacks=callbacks)
| reg.fit(X, y)
|
| kwargs : typing.Optional[typing.Any]
|
| Keyword arguments for XGBoost Booster object. Full documentation of parameters
| can be found :doc:`here </parameter>`.
| Attempting to set a parameter via the constructor args and \*\*kwargs
| dict simultaneously will result in a TypeError.
|
| .. note:: \*\*kwargs unsupported by scikit-learn
|
| \*\*kwargs is unsupported by scikit-learn. We do not guarantee
| that parameters passed via this argument will interact properly
| with scikit-learn.
|
| .. note:: Custom objective function
|
| A custom objective function can be provided for the ``objective``
| parameter. In this case, it should have the signature ``objective(y_true,
| y_pred) -> [grad, hess]`` or ``objective(y_true, y_pred, *, sample_weight)
| -> [grad, hess]``:
|
| y_true: array_like of shape [n_samples]
| The target values
| y_pred: array_like of shape [n_samples]
| The predicted values
| sample_weight :
| Optional sample weights.
|
| grad: array_like of shape [n_samples]
| The value of the gradient for each sample point.
| hess: array_like of shape [n_samples]
| The value of the second derivative for each sample point
|
| Note that, if the custom objective produces negative values for
| the Hessian, these will be clipped. If the objective is non-convex,
| one might also consider using the expected Hessian (Fisher
| information) instead.
|
| Method resolution order:
| XGBClassifier
| sklearn.base.ClassifierMixin
| XGBModel
| sklearn.base.BaseEstimator
| sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin
| sklearn.utils._metadata_requests._MetadataRequester
| builtins.object
|
| Methods defined here:
|
| __init__(self, *, objective: Union[str, xgboost.sklearn._SklObjWProto, Callable[[Any, Any], Tuple[numpy.ndarray, numpy.ndarray]], NoneType] = 'binary:logistic', **kwargs: Any) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| __sklearn_tags__(self) -> object
|
| fit(self, X: Any, y: Any, *, sample_weight: Optional[Any] = None, base_margin: Optional[Any] = None, eval_set: Optional[Sequence[Tuple[Any, Any]]] = None, verbose: Union[bool, int, NoneType] = True, xgb_model: Union[xgboost.core.Booster, str, xgboost.sklearn.XGBModel, NoneType] = None, sample_weight_eval_set: Optional[Sequence[Any]] = None, base_margin_eval_set: Optional[Sequence[Any]] = None, feature_weights: Optional[Any] = None) -> 'XGBClassifier'
| Fit gradient boosting classifier.
|
| Note that calling ``fit()`` multiple times will cause the model object to be
| re-fit from scratch. To resume training from a previous checkpoint, explicitly
| pass ``xgb_model`` argument.
|
| Parameters
| ----------
| X :
| Input feature matrix. See :ref:`py-data` for a list of supported types.
|
| When the ``tree_method`` is set to ``hist``, internally, the
| :py:class:`QuantileDMatrix` will be used instead of the :py:class:`DMatrix`
| for conserving memory. However, this has performance implications when the
| device of input data is not matched with algorithm. For instance, if the
| input is a numpy array on CPU but ``cuda`` is used for training, then the
| data is first processed on CPU then transferred to GPU.
| y :
| Labels
| sample_weight :
| instance weights
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| eval_set :
| A list of (X, y) tuple pairs to use as validation sets, for which
| metrics will be computed.
| Validation metrics will help us track the performance of the model.
|
| verbose :
| If `verbose` is True and an evaluation set is used, the evaluation metric
| measured on the validation set is printed to stdout at each boosting stage.
| If `verbose` is an integer, the evaluation metric is printed at each
| `verbose` boosting stage. The last boosting stage / the boosting stage found
| by using `early_stopping_rounds` is also printed.
| xgb_model :
| file name of stored XGBoost model or 'Booster' instance XGBoost model to be
| loaded before training (allows training continuation).
| sample_weight_eval_set :
| A list of the form [L_1, L_2, ..., L_n], where each L_i is an array like
| object storing instance weights for the i-th validation set.
| base_margin_eval_set :
| A list of the form [M_1, M_2, ..., M_n], where each M_i is an array like
| object storing base margin for the i-th validation set.
| feature_weights :
|
| .. deprecated:: 3.0.0
|
| Use `feature_weights` in :py:meth:`__init__` or :py:meth:`set_params`
| instead.
|
| predict(self, X: Any, *, output_margin: bool = False, validate_features: bool = True, base_margin: Optional[Any] = None, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> Any
| Predict with `X`. If the model is trained with early stopping, then
| :py:attr:`best_iteration` is used automatically. The estimator uses
| `inplace_predict` by default and falls back to using :py:class:`DMatrix` if
| devices between the data and the estimator don't match.
|
| .. note:: This function is only thread safe for `gbtree` and `dart`.
|
| Parameters
| ----------
| X :
| Data to predict with. See :ref:`py-data` for a list of supported types.
| output_margin :
| Whether to output the raw untransformed margin value.
| validate_features :
| When this is True, validate that the Booster's and data's feature_names are
| identical. Otherwise, it is assumed that the feature_names are the same.
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying ``iteration_range=(10,
| 20)``, then only the forests built during [10, 20) (half open set) rounds
| are used in this prediction.
|
| .. versionadded:: 1.4.0
|
| Returns
| -------
| prediction
|
| predict_proba(self, X: Any, validate_features: bool = True, base_margin: Optional[Any] = None, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> numpy.ndarray
| Predict the probability of each `X` example being of a given class. If the
| model is trained with early stopping, then :py:attr:`best_iteration` is used
| automatically. The estimator uses `inplace_predict` by default and falls back to
| using :py:class:`DMatrix` if devices between the data and the estimator don't
| match.
|
| .. note:: This function is only thread safe for `gbtree` and `dart`.
|
| Parameters
| ----------
| X :
| Feature matrix. See :ref:`py-data` for a list of supported types.
| validate_features :
| When this is True, validate that the Booster's and data's feature_names are
| identical. Otherwise, it is assumed that the feature_names are the same.
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying `iteration_range=(10,
| 20)`, then only the forests built during [10, 20) (half open set) rounds are
| used in this prediction.
|
| Returns
| -------
| prediction :
| a numpy array of shape array-like of shape (n_samples, n_classes) with the
| probability of each data example being of a given class.
|
| set_fit_request(self: xgboost.sklearn.XGBClassifier, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', base_margin_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', feature_weights: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', verbose: Union[bool, NoneType, str] = '$UNCHANGED$', xgb_model: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBClassifier from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``fit`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``fit`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``fit``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``fit``.
|
| base_margin_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin_eval_set`` parameter in ``fit``.
|
| eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``eval_set`` parameter in ``fit``.
|
| feature_weights : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``feature_weights`` parameter in ``fit``.
|
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``fit``.
|
| sample_weight_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight_eval_set`` parameter in ``fit``.
|
| verbose : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``verbose`` parameter in ``fit``.
|
| xgb_model : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``xgb_model`` parameter in ``fit``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_predict_proba_request(self: xgboost.sklearn.XGBClassifier, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', iteration_range: Union[bool, NoneType, str] = '$UNCHANGED$', validate_features: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBClassifier from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``predict_proba`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``predict_proba`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``predict_proba``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``predict_proba``.
|
| iteration_range : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``iteration_range`` parameter in ``predict_proba``.
|
| validate_features : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``validate_features`` parameter in ``predict_proba``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_predict_request(self: xgboost.sklearn.XGBClassifier, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', iteration_range: Union[bool, NoneType, str] = '$UNCHANGED$', output_margin: Union[bool, NoneType, str] = '$UNCHANGED$', validate_features: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBClassifier from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``predict`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``predict`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``predict``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``predict``.
|
| iteration_range : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``iteration_range`` parameter in ``predict``.
|
| output_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``output_margin`` parameter in ``predict``.
|
| validate_features : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``validate_features`` parameter in ``predict``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_score_request(self: xgboost.sklearn.XGBClassifier, *, sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBClassifier from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``score`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``score`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``score``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``score``.
|
| Returns
| -------
| self : object
| The updated object.
|
| ----------------------------------------------------------------------
| Readonly properties defined here:
|
| classes_
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.ClassifierMixin:
|
| score(self, X, y, sample_weight=None)
| Return the mean accuracy on the given test data and labels.
|
| In multi-label classification, this is the subset accuracy
| which is a harsh metric since you require for each sample that
| each label set be correctly predicted.
|
| Parameters
| ----------
| X : array-like of shape (n_samples, n_features)
| Test samples.
|
| y : array-like of shape (n_samples,) or (n_samples, n_outputs)
| True labels for `X`.
|
| sample_weight : array-like of shape (n_samples,), default=None
| Sample weights.
|
| Returns
| -------
| score : float
| Mean accuracy of ``self.predict(X)`` w.r.t. `y`.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from sklearn.base.ClassifierMixin:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Methods inherited from XGBModel:
|
| __sklearn_is_fitted__(self) -> bool
|
| apply(self, X: Any, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> numpy.ndarray
| Return the predicted leaf every tree for each sample. If the model is trained
| with early stopping, then :py:attr:`best_iteration` is used automatically.
|
| Parameters
| ----------
| X :
| Input features matrix. See :ref:`py-data` for a list of supported types.
|
| iteration_range :
| See :py:meth:`predict`.
|
| Returns
| -------
| X_leaves : array_like, shape=[n_samples, n_trees]
| For each datapoint x in X and for each tree, return the index of the
| leaf x ends up in. Leaves are numbered within
| ``[0; 2**(self.max_depth+1))``, possibly with gaps in the numbering.
|
| evals_result(self) -> Dict[str, Dict[str, List[float]]]
| Return the evaluation results.
|
| If **eval_set** is passed to the :py:meth:`fit` function, you can call
| ``evals_result()`` to get evaluation results for all passed **eval_sets**. When
| **eval_metric** is also passed to the :py:meth:`fit` function, the
| **evals_result** will contain the **eval_metrics** passed to the :py:meth:`fit`
| function.
|
| The returned evaluation result is a dictionary:
|
| .. code-block:: python
|
| {'validation_0': {'logloss': ['0.604835', '0.531479']},
| 'validation_1': {'logloss': ['0.41965', '0.17686']}}
|
| Returns
| -------
| evals_result
|
| get_booster(self) -> xgboost.core.Booster
| Get the underlying xgboost Booster of this model.
|
| This will raise an exception when fit was not called
|
| Returns
| -------
| booster : a xgboost booster of underlying model
|
| get_num_boosting_rounds(self) -> int
| Gets the number of xgboost boosting rounds.
|
| get_params(self, deep: bool = True) -> Dict[str, Any]
| Get parameters.
|
| get_xgb_params(self) -> Dict[str, Any]
| Get xgboost specific parameters.
|
| load_model(self, fname: Union[os.PathLike[~AnyStr], bytearray, str]) -> None
| Load the model from a file or a bytearray.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| model.load_model("model.json")
|
| # or
| model.save_model("model.ubj")
| model.load_model("model.ubj")
|
| # or
| buf = model.save_raw()
| model.load_model(buf)
|
| Parameters
| ----------
| fname :
| Input file name or memory buffer(see also save_raw)
|
| save_model(self, fname: Union[str, os.PathLike]) -> None
| Save the model to a file.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| # or
| model.save_model("model.ubj")
|
| Parameters
| ----------
| fname :
| Output file name
|
| set_params(self, **params: Any) -> 'XGBModel'
| Set the parameters of this estimator. Modification of the sklearn method to
| allow unknown kwargs. This allows using the full range of xgboost
| parameters that are not defined as member variables in sklearn grid
| search.
|
| Returns
| -------
| self
|
| ----------------------------------------------------------------------
| Readonly properties inherited from XGBModel:
|
| best_iteration
| The best iteration obtained by early stopping. This attribute is 0-based,
| for instance if the best iteration is the first round, then best_iteration is 0.
|
| best_score
| The best score obtained by early stopping.
|
| coef_
| Coefficients property
|
| .. note:: Coefficients are defined only for linear learners
|
| Coefficients are only defined when the linear model is chosen as
| base learner (`booster=gblinear`). It is not defined for other base
| learner types, such as tree learners (`booster=gbtree`).
|
| Returns
| -------
| coef_ : array of shape ``[n_features]`` or ``[n_classes, n_features]``
|
| feature_importances_
| Feature importances property, return depends on `importance_type`
| parameter. When model trained with multi-class/multi-label/multi-target dataset,
| the feature importance is "averaged" over all targets. The "average" is defined
| based on the importance type. For instance, if the importance type is
| "total_gain", then the score is sum of loss change for each split from all
| trees.
|
| Returns
| -------
| feature_importances_ : array of shape ``[n_features]`` except for multi-class
| linear model, which returns an array with shape `(n_features, n_classes)`
|
| feature_names_in_
| Names of features seen during :py:meth:`fit`. Defined only when `X` has
| feature names that are all strings.
|
| intercept_
| Intercept (bias) property
|
| For tree-based model, the returned value is the `base_score`.
|
| Returns
| -------
| intercept_ : array of shape ``(1,)`` or ``[n_classes]``
|
| n_features_in_
| Number of features seen during :py:meth:`fit`.
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.BaseEstimator:
|
| __getstate__(self)
| Helper for pickle.
|
| __repr__(self, N_CHAR_MAX=700)
| Return repr(self).
|
| __setstate__(self, state)
|
| __sklearn_clone__(self)
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| get_metadata_routing(self)
| Get metadata routing of this object.
|
| Please check :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| Returns
| -------
| routing : MetadataRequest
| A :class:`~sklearn.utils.metadata_routing.MetadataRequest` encapsulating
| routing information.
|
| ----------------------------------------------------------------------
| Class methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| __init_subclass__(**kwargs)
| Set the ``set_{method}_request`` methods.
|
| This uses PEP-487 [1]_ to set the ``set_{method}_request`` methods. It
| looks for the information available in the set default values which are
| set using ``__metadata_request__*`` class attributes, or inferred
| from method signatures.
|
| The ``__metadata_request__*`` class attributes are used when a method
| does not explicitly accept a metadata through its arguments or if the
| developer would like to specify a request value for those metadata
| which are different from the default ``None``.
|
| References
| ----------
| .. [1] https://www.python.org/dev/peps/pep-0487
xgboost.XGBModel
Python Library Documentation: class XGBModel in module xgboost.sklearn
class XGBModel(sklearn.base.BaseEstimator)
| XGBModel(*, max_depth: Optional[int] = None, max_leaves: Optional[int] = None, max_bin: Optional[int] = None, grow_policy: Optional[str] = None, learning_rate: Optional[float] = None, n_estimators: Optional[int] = None, verbosity: Optional[int] = None, objective: Union[str, xgboost.sklearn._SklObjWProto, Callable[[Any, Any], Tuple[numpy.ndarray, numpy.ndarray]], NoneType] = None, booster: Optional[str] = None, tree_method: Optional[str] = None, n_jobs: Optional[int] = None, gamma: Optional[float] = None, min_child_weight: Optional[float] = None, max_delta_step: Optional[float] = None, subsample: Optional[float] = None, sampling_method: Optional[str] = None, colsample_bytree: Optional[float] = None, colsample_bylevel: Optional[float] = None, colsample_bynode: Optional[float] = None, reg_alpha: Optional[float] = None, reg_lambda: Optional[float] = None, scale_pos_weight: Optional[float] = None, base_score: Union[float, List[float], NoneType] = None, random_state: Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType] = None, missing: float = nan, num_parallel_tree: Optional[int] = None, monotone_constraints: Union[Dict[str, int], str, NoneType] = None, interaction_constraints: Union[str, Sequence[Sequence[str]], NoneType] = None, importance_type: Optional[str] = None, device: Optional[str] = None, validate_parameters: Optional[bool] = None, enable_categorical: bool = True, feature_types: Optional[Sequence[str]] = None, feature_weights: Optional[Any] = None, max_cat_to_onehot: Optional[int] = None, max_cat_threshold: Optional[int] = None, multi_strategy: Optional[str] = None, eval_metric: Union[str, List[Union[str, Callable]], Callable, NoneType] = None, early_stopping_rounds: Optional[int] = None, callbacks: Optional[List[xgboost.callback.TrainingCallback]] = None, **kwargs: Any) -> None
|
| Implementation of the Scikit-Learn API for XGBoost.
| See :doc:`/python/sklearn_estimator` for more information.
|
| Parameters
| ----------
|
| n_estimators : typing.Optional[int]
| Number of gradient boosted trees. Equivalent to number of boosting
| rounds.
|
| max_depth : typing.Optional[int]
|
| Maximum tree depth for base learners.
|
| max_leaves : typing.Optional[int]
|
| Maximum number of leaves; 0 indicates no limit.
|
| max_bin : typing.Optional[int]
|
| If using histogram-based algorithm, maximum number of bins per feature
|
| grow_policy : typing.Optional[str]
|
| Tree growing policy.
|
| - depthwise: Favors splitting at nodes closest to the node,
| - lossguide: Favors splitting at nodes with highest loss change.
|
| learning_rate : typing.Optional[float]
|
| Boosting learning rate (xgb's "eta")
|
| verbosity : typing.Optional[int]
|
| The degree of verbosity. Valid values are 0 (silent) - 3 (debug).
|
| objective : typing.Union[str, xgboost.sklearn._SklObjWProto, typing.Callable[[typing.Any, typing.Any], typing.Tuple[numpy.ndarray, numpy.ndarray]], NoneType]
|
| Specify the learning task and the corresponding learning objective or a custom
| objective function to be used.
|
| For custom objective, see :doc:`/tutorials/custom_metric_obj` and
| :ref:`custom-obj-metric` for more information, along with the end note for
| function signatures.
|
| booster: typing.Optional[str]
|
| Specify which booster to use: ``gbtree``, ``gblinear`` or ``dart``.
|
| .. deprecated:: 3.3.0
|
| ``gblinear`` is deprecated and support will be removed in a future release.
|
| tree_method : typing.Optional[str]
|
| Specify which tree method to use. Default to auto. If this parameter is set to
| default, XGBoost will choose the most conservative option available. It's
| recommended to study this option from the parameters document :doc:`tree method
| </treemethod>`
|
| n_jobs : typing.Optional[int]
|
| Number of parallel threads used to run xgboost. When used with other
| Scikit-Learn algorithms like grid search, you may choose which algorithm to
| parallelize and balance the threads. Creating thread contention will
| significantly slow down both algorithms.
|
| gamma : typing.Optional[float]
|
| (min_split_loss) Minimum loss reduction required to make a further partition on
| a leaf node of the tree.
|
| min_child_weight : typing.Optional[float]
|
| Minimum sum of instance weight(hessian) needed in a child.
|
| max_delta_step : typing.Optional[float]
|
| Maximum delta step we allow each tree's weight estimation to be.
|
| subsample : typing.Optional[float]
|
| Subsample ratio of the training instance.
|
| sampling_method : typing.Optional[str]
|
| Sampling method. Used only by the GPU version of ``hist`` tree method.
|
| - ``uniform``: Select random training instances uniformly.
| - ``gradient_based``: Select random training instances with higher probability
| when the gradient and hessian are larger. (cf. CatBoost)
|
| colsample_bytree : typing.Optional[float]
|
| Subsample ratio of columns when constructing each tree.
|
| colsample_bylevel : typing.Optional[float]
|
| Subsample ratio of columns for each level.
|
| colsample_bynode : typing.Optional[float]
|
| Subsample ratio of columns for each split.
|
| reg_alpha : typing.Optional[float]
|
| L1 regularization term on weights (xgb's alpha).
|
| reg_lambda : typing.Optional[float]
|
| L2 regularization term on weights (xgb's lambda).
|
| scale_pos_weight : typing.Optional[float]
| Balancing of positive and negative weights.
|
| base_score : typing.Union[float, typing.List[float], NoneType]
|
| The initial prediction score of all instances, global bias.
|
| random_state : typing.Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType]
|
| Random number seed.
|
| .. note::
|
| Using gblinear booster with shotgun updater is nondeterministic as
| it uses Hogwild algorithm.
|
| missing : float
|
| Value in the data which needs to be present as a missing value. Default to
| :py:data:`numpy.nan`.
|
| num_parallel_tree: typing.Optional[int]
|
| Used for boosting random forest.
|
| monotone_constraints : typing.Union[typing.Dict[str, int], str, NoneType]
|
| Constraint of variable monotonicity. See :doc:`tutorial </tutorials/monotonic>`
| for more information.
|
| interaction_constraints : typing.Union[str, typing.List[typing.Tuple[str]], NoneType]
|
| Constraints for interaction representing permitted interactions. The
| constraints must be specified in the form of a nested list, e.g. ``[[0, 1], [2,
| 3, 4]]``, where each inner list is a group of indices of features that are
| allowed to interact with each other. See :doc:`tutorial
| </tutorials/feature_interaction_constraint>` for more information
|
| importance_type: typing.Optional[str]
|
| The feature importance type for the feature_importances\_ property:
|
| * For tree model, it's either "gain", "weight", "cover", "total_gain" or
| "total_cover".
| * For linear model, only "weight" is defined and it's the normalized
| coefficients without bias.
|
| device : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| Device ordinal, available options are `cpu`, `cuda`, and `gpu`.
|
| validate_parameters : typing.Optional[bool]
|
| Give warnings for unknown parameter.
|
| enable_categorical : bool
|
| See the same parameter of :py:class:`DMatrix` for details.
|
| feature_types : typing.Optional[typing.Sequence[str]]
|
| .. versionadded:: 1.7.0
|
| Used for specifying feature types without constructing a dataframe. See
| the :py:class:`DMatrix` for details.
|
| feature_weights : Optional[ArrayLike]
|
| Weight for each feature, defines the probability of each feature being selected
| when colsample is being used. All values must be greater than 0, otherwise a
| `ValueError` is thrown.
|
| max_cat_to_onehot : Optional[int]
|
| .. versionadded:: 1.6.0
|
| .. note:: This parameter is experimental
|
| A threshold for deciding whether XGBoost should use one-hot encoding based split
| for categorical data. When number of categories is lesser than the threshold
| then one-hot encoding is chosen, otherwise the categories will be partitioned
| into children nodes. Also, `enable_categorical` needs to be set to have
| categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| max_cat_threshold : typing.Optional[int]
|
| .. versionadded:: 1.7.0
|
| .. note:: This parameter is experimental
|
| Maximum number of categories considered for each split. Used only by
| partition-based splits for preventing over-fitting. Also, `enable_categorical`
| needs to be set to have categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| multi_strategy : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| .. note:: This parameter is working-in-progress.
|
| The strategy used for training multi-target models, including multi-target
| regression and multi-class classification. See :doc:`/tutorials/multioutput` for
| more information.
|
| - ``one_output_per_tree``: One model for each target.
| - ``multi_output_tree``: Use multi-target trees.
|
| eval_metric : typing.Union[str, typing.List[typing.Union[str, typing.Callable]], typing.Callable, NoneType]
|
| .. versionadded:: 1.6.0
|
| Metric used for monitoring the training result and early stopping. It can be a
| string or list of strings as names of predefined metric in XGBoost (See
| :doc:`/parameter`), one of the metrics in :py:mod:`sklearn.metrics`, or any
| other user defined metric that looks like `sklearn.metrics`.
|
| If custom objective is also provided, then custom metric should implement the
| corresponding reverse link function.
|
| Unlike the `scoring` parameter commonly used in scikit-learn, when a callable
| object is provided, it's assumed to be a cost function and by default XGBoost
| will minimize the result during early stopping.
|
| For advanced usage on Early stopping like directly choosing to maximize instead
| of minimize, see :py:obj:`xgboost.callback.EarlyStopping`.
|
| See :doc:`/tutorials/custom_metric_obj` and :ref:`custom-obj-metric` for more
| information.
|
| .. code-block:: python
|
| from sklearn.datasets import load_diabetes
| from sklearn.metrics import mean_absolute_error
| X, y = load_diabetes(return_X_y=True)
| reg = xgb.XGBRegressor(
| tree_method="hist",
| eval_metric=mean_absolute_error,
| )
| reg.fit(X, y, eval_set=[(X, y)])
|
| early_stopping_rounds : typing.Optional[int]
|
| .. versionadded:: 1.6.0
|
| - Activates early stopping. Validation metric needs to improve at least once in
| every **early_stopping_rounds** round(s) to continue training. Requires at
| least one item in **eval_set** in :py:meth:`fit`.
|
| - If early stopping occurs, the model will have two additional attributes:
| :py:attr:`best_score` and :py:attr:`best_iteration`. These are used by the
| :py:meth:`predict` and :py:meth:`apply` methods to determine the optimal
| number of trees during inference. If users want to access the full model
| (including trees built after early stopping), they can specify the
| `iteration_range` in these inference methods. In addition, other utilities
| like model plotting can also use the entire model.
|
| - If you prefer to discard the trees after `best_iteration`, consider using the
| callback function :py:class:`xgboost.callback.EarlyStopping`.
|
| - If there's more than one item in **eval_set**, the last entry will be used for
| early stopping. If there's more than one metric in **eval_metric**, the last
| metric will be used for early stopping.
|
| callbacks : typing.Optional[typing.List[xgboost.callback.TrainingCallback]]
|
| List of callback functions that are applied at end of each iteration.
| It is possible to use predefined callbacks by using
| :ref:`Callback API <callback_api>`.
|
| .. note::
|
| States in callback are not preserved during training, which means callback
| objects can not be reused for multiple training sessions without
| reinitialization or deepcopy.
|
| .. code-block:: python
|
| for params in parameters_grid:
| # be sure to (re)initialize the callbacks before each run
| callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
| reg = xgboost.XGBRegressor(**params, callbacks=callbacks)
| reg.fit(X, y)
|
| kwargs : typing.Optional[typing.Any]
|
| Keyword arguments for XGBoost Booster object. Full documentation of parameters
| can be found :doc:`here </parameter>`.
| Attempting to set a parameter via the constructor args and \*\*kwargs
| dict simultaneously will result in a TypeError.
|
| .. note:: \*\*kwargs unsupported by scikit-learn
|
| \*\*kwargs is unsupported by scikit-learn. We do not guarantee
| that parameters passed via this argument will interact properly
| with scikit-learn.
|
| .. note:: Custom objective function
|
| A custom objective function can be provided for the ``objective``
| parameter. In this case, it should have the signature ``objective(y_true,
| y_pred) -> [grad, hess]`` or ``objective(y_true, y_pred, *, sample_weight)
| -> [grad, hess]``:
|
| y_true: array_like of shape [n_samples]
| The target values
| y_pred: array_like of shape [n_samples]
| The predicted values
| sample_weight :
| Optional sample weights.
|
| grad: array_like of shape [n_samples]
| The value of the gradient for each sample point.
| hess: array_like of shape [n_samples]
| The value of the second derivative for each sample point
|
| Note that, if the custom objective produces negative values for
| the Hessian, these will be clipped. If the objective is non-convex,
| one might also consider using the expected Hessian (Fisher
| information) instead.
|
| Method resolution order:
| XGBModel
| sklearn.base.BaseEstimator
| sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin
| sklearn.utils._metadata_requests._MetadataRequester
| builtins.object
|
| Methods defined here:
|
| __init__(self, *, max_depth: Optional[int] = None, max_leaves: Optional[int] = None, max_bin: Optional[int] = None, grow_policy: Optional[str] = None, learning_rate: Optional[float] = None, n_estimators: Optional[int] = None, verbosity: Optional[int] = None, objective: Union[str, xgboost.sklearn._SklObjWProto, Callable[[Any, Any], Tuple[numpy.ndarray, numpy.ndarray]], NoneType] = None, booster: Optional[str] = None, tree_method: Optional[str] = None, n_jobs: Optional[int] = None, gamma: Optional[float] = None, min_child_weight: Optional[float] = None, max_delta_step: Optional[float] = None, subsample: Optional[float] = None, sampling_method: Optional[str] = None, colsample_bytree: Optional[float] = None, colsample_bylevel: Optional[float] = None, colsample_bynode: Optional[float] = None, reg_alpha: Optional[float] = None, reg_lambda: Optional[float] = None, scale_pos_weight: Optional[float] = None, base_score: Union[float, List[float], NoneType] = None, random_state: Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType] = None, missing: float = nan, num_parallel_tree: Optional[int] = None, monotone_constraints: Union[Dict[str, int], str, NoneType] = None, interaction_constraints: Union[str, Sequence[Sequence[str]], NoneType] = None, importance_type: Optional[str] = None, device: Optional[str] = None, validate_parameters: Optional[bool] = None, enable_categorical: bool = True, feature_types: Optional[Sequence[str]] = None, feature_weights: Optional[Any] = None, max_cat_to_onehot: Optional[int] = None, max_cat_threshold: Optional[int] = None, multi_strategy: Optional[str] = None, eval_metric: Union[str, List[Union[str, Callable]], Callable, NoneType] = None, early_stopping_rounds: Optional[int] = None, callbacks: Optional[List[xgboost.callback.TrainingCallback]] = None, **kwargs: Any) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| __sklearn_is_fitted__(self) -> bool
|
| __sklearn_tags__(self) -> object
|
| apply(self, X: Any, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> numpy.ndarray
| Return the predicted leaf every tree for each sample. If the model is trained
| with early stopping, then :py:attr:`best_iteration` is used automatically.
|
| Parameters
| ----------
| X :
| Input features matrix. See :ref:`py-data` for a list of supported types.
|
| iteration_range :
| See :py:meth:`predict`.
|
| Returns
| -------
| X_leaves : array_like, shape=[n_samples, n_trees]
| For each datapoint x in X and for each tree, return the index of the
| leaf x ends up in. Leaves are numbered within
| ``[0; 2**(self.max_depth+1))``, possibly with gaps in the numbering.
|
| evals_result(self) -> Dict[str, Dict[str, List[float]]]
| Return the evaluation results.
|
| If **eval_set** is passed to the :py:meth:`fit` function, you can call
| ``evals_result()`` to get evaluation results for all passed **eval_sets**. When
| **eval_metric** is also passed to the :py:meth:`fit` function, the
| **evals_result** will contain the **eval_metrics** passed to the :py:meth:`fit`
| function.
|
| The returned evaluation result is a dictionary:
|
| .. code-block:: python
|
| {'validation_0': {'logloss': ['0.604835', '0.531479']},
| 'validation_1': {'logloss': ['0.41965', '0.17686']}}
|
| Returns
| -------
| evals_result
|
| fit(self, X: Any, y: Any, *, sample_weight: Optional[Any] = None, base_margin: Optional[Any] = None, eval_set: Optional[Sequence[Tuple[Any, Any]]] = None, verbose: Union[bool, int, NoneType] = True, xgb_model: Union[xgboost.core.Booster, ForwardRef('XGBModel'), str, NoneType] = None, sample_weight_eval_set: Optional[Sequence[Any]] = None, base_margin_eval_set: Optional[Sequence[Any]] = None, feature_weights: Optional[Any] = None) -> 'XGBModel'
| Fit gradient boosting model.
|
| Note that calling ``fit()`` multiple times will cause the model object to be
| re-fit from scratch. To resume training from a previous checkpoint, explicitly
| pass ``xgb_model`` argument.
|
| Parameters
| ----------
| X :
| Input feature matrix. See :ref:`py-data` for a list of supported types.
|
| When the ``tree_method`` is set to ``hist``, internally, the
| :py:class:`QuantileDMatrix` will be used instead of the :py:class:`DMatrix`
| for conserving memory. However, this has performance implications when the
| device of input data is not matched with algorithm. For instance, if the
| input is a numpy array on CPU but ``cuda`` is used for training, then the
| data is first processed on CPU then transferred to GPU.
| y :
| Labels
| sample_weight :
| instance weights
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| eval_set :
| A list of (X, y) tuple pairs to use as validation sets, for which
| metrics will be computed.
| Validation metrics will help us track the performance of the model.
|
| verbose :
| If `verbose` is True and an evaluation set is used, the evaluation metric
| measured on the validation set is printed to stdout at each boosting stage.
| If `verbose` is an integer, the evaluation metric is printed at each
| `verbose` boosting stage. The last boosting stage / the boosting stage found
| by using `early_stopping_rounds` is also printed.
| xgb_model :
| file name of stored XGBoost model or 'Booster' instance XGBoost model to be
| loaded before training (allows training continuation).
| sample_weight_eval_set :
| A list of the form [L_1, L_2, ..., L_n], where each L_i is an array like
| object storing instance weights for the i-th validation set.
| base_margin_eval_set :
| A list of the form [M_1, M_2, ..., M_n], where each M_i is an array like
| object storing base margin for the i-th validation set.
| feature_weights :
|
| .. deprecated:: 3.0.0
|
| Use `feature_weights` in :py:meth:`__init__` or :py:meth:`set_params`
| instead.
|
| get_booster(self) -> xgboost.core.Booster
| Get the underlying xgboost Booster of this model.
|
| This will raise an exception when fit was not called
|
| Returns
| -------
| booster : a xgboost booster of underlying model
|
| get_num_boosting_rounds(self) -> int
| Gets the number of xgboost boosting rounds.
|
| get_params(self, deep: bool = True) -> Dict[str, Any]
| Get parameters.
|
| get_xgb_params(self) -> Dict[str, Any]
| Get xgboost specific parameters.
|
| load_model(self, fname: Union[os.PathLike[~AnyStr], bytearray, str]) -> None
| Load the model from a file or a bytearray.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| model.load_model("model.json")
|
| # or
| model.save_model("model.ubj")
| model.load_model("model.ubj")
|
| # or
| buf = model.save_raw()
| model.load_model(buf)
|
| Parameters
| ----------
| fname :
| Input file name or memory buffer(see also save_raw)
|
| predict(self, X: Any, *, output_margin: bool = False, validate_features: bool = True, base_margin: Optional[Any] = None, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> Any
| Predict with `X`. If the model is trained with early stopping, then
| :py:attr:`best_iteration` is used automatically. The estimator uses
| `inplace_predict` by default and falls back to using :py:class:`DMatrix` if
| devices between the data and the estimator don't match.
|
| .. note:: This function is only thread safe for `gbtree` and `dart`.
|
| Parameters
| ----------
| X :
| Data to predict with. See :ref:`py-data` for a list of supported types.
| output_margin :
| Whether to output the raw untransformed margin value.
| validate_features :
| When this is True, validate that the Booster's and data's feature_names are
| identical. Otherwise, it is assumed that the feature_names are the same.
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying ``iteration_range=(10,
| 20)``, then only the forests built during [10, 20) (half open set) rounds
| are used in this prediction.
|
| .. versionadded:: 1.4.0
|
| Returns
| -------
| prediction
|
| save_model(self, fname: Union[str, os.PathLike]) -> None
| Save the model to a file.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| # or
| model.save_model("model.ubj")
|
| Parameters
| ----------
| fname :
| Output file name
|
| set_fit_request(self: xgboost.sklearn.XGBModel, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', base_margin_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', feature_weights: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', verbose: Union[bool, NoneType, str] = '$UNCHANGED$', xgb_model: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBModel from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``fit`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``fit`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``fit``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``fit``.
|
| base_margin_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin_eval_set`` parameter in ``fit``.
|
| eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``eval_set`` parameter in ``fit``.
|
| feature_weights : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``feature_weights`` parameter in ``fit``.
|
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``fit``.
|
| sample_weight_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight_eval_set`` parameter in ``fit``.
|
| verbose : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``verbose`` parameter in ``fit``.
|
| xgb_model : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``xgb_model`` parameter in ``fit``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_params(self, **params: Any) -> 'XGBModel'
| Set the parameters of this estimator. Modification of the sklearn method to
| allow unknown kwargs. This allows using the full range of xgboost
| parameters that are not defined as member variables in sklearn grid
| search.
|
| Returns
| -------
| self
|
| set_predict_request(self: xgboost.sklearn.XGBModel, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', iteration_range: Union[bool, NoneType, str] = '$UNCHANGED$', output_margin: Union[bool, NoneType, str] = '$UNCHANGED$', validate_features: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBModel from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``predict`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``predict`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``predict``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``predict``.
|
| iteration_range : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``iteration_range`` parameter in ``predict``.
|
| output_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``output_margin`` parameter in ``predict``.
|
| validate_features : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``validate_features`` parameter in ``predict``.
|
| Returns
| -------
| self : object
| The updated object.
|
| ----------------------------------------------------------------------
| Readonly properties defined here:
|
| best_iteration
| The best iteration obtained by early stopping. This attribute is 0-based,
| for instance if the best iteration is the first round, then best_iteration is 0.
|
| best_score
| The best score obtained by early stopping.
|
| coef_
| Coefficients property
|
| .. note:: Coefficients are defined only for linear learners
|
| Coefficients are only defined when the linear model is chosen as
| base learner (`booster=gblinear`). It is not defined for other base
| learner types, such as tree learners (`booster=gbtree`).
|
| Returns
| -------
| coef_ : array of shape ``[n_features]`` or ``[n_classes, n_features]``
|
| feature_importances_
| Feature importances property, return depends on `importance_type`
| parameter. When model trained with multi-class/multi-label/multi-target dataset,
| the feature importance is "averaged" over all targets. The "average" is defined
| based on the importance type. For instance, if the importance type is
| "total_gain", then the score is sum of loss change for each split from all
| trees.
|
| Returns
| -------
| feature_importances_ : array of shape ``[n_features]`` except for multi-class
| linear model, which returns an array with shape `(n_features, n_classes)`
|
| feature_names_in_
| Names of features seen during :py:meth:`fit`. Defined only when `X` has
| feature names that are all strings.
|
| intercept_
| Intercept (bias) property
|
| For tree-based model, the returned value is the `base_score`.
|
| Returns
| -------
| intercept_ : array of shape ``(1,)`` or ``[n_classes]``
|
| n_features_in_
| Number of features seen during :py:meth:`fit`.
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.BaseEstimator:
|
| __getstate__(self)
| Helper for pickle.
|
| __repr__(self, N_CHAR_MAX=700)
| Return repr(self).
|
| __setstate__(self, state)
|
| __sklearn_clone__(self)
|
| ----------------------------------------------------------------------
| Data descriptors inherited from sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| get_metadata_routing(self)
| Get metadata routing of this object.
|
| Please check :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| Returns
| -------
| routing : MetadataRequest
| A :class:`~sklearn.utils.metadata_routing.MetadataRequest` encapsulating
| routing information.
|
| ----------------------------------------------------------------------
| Class methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| __init_subclass__(**kwargs)
| Set the ``set_{method}_request`` methods.
|
| This uses PEP-487 [1]_ to set the ``set_{method}_request`` methods. It
| looks for the information available in the set default values which are
| set using ``__metadata_request__*`` class attributes, or inferred
| from method signatures.
|
| The ``__metadata_request__*`` class attributes are used when a method
| does not explicitly accept a metadata through its arguments or if the
| developer would like to specify a request value for those metadata
| which are different from the default ``None``.
|
| References
| ----------
| .. [1] https://www.python.org/dev/peps/pep-0487
xgboost.XGBRFClassifier
Python Library Documentation: class XGBRFClassifier in module xgboost.sklearn
class XGBRFClassifier(XGBClassifier)
| XGBRFClassifier(*, learning_rate: float = 1.0, subsample: float = 0.8, colsample_bynode: float = 0.8, reg_lambda: float = 1e-05, **kwargs: Any)
|
| scikit-learn API for XGBoost random forest classification.
| See :doc:`/python/sklearn_estimator` for more information.
|
| Parameters
| ----------
|
| n_estimators : Optional[int]
| Number of trees in random forest to fit.
|
| max_depth : typing.Optional[int]
|
| Maximum tree depth for base learners.
|
| max_leaves : typing.Optional[int]
|
| Maximum number of leaves; 0 indicates no limit.
|
| max_bin : typing.Optional[int]
|
| If using histogram-based algorithm, maximum number of bins per feature
|
| grow_policy : typing.Optional[str]
|
| Tree growing policy.
|
| - depthwise: Favors splitting at nodes closest to the node,
| - lossguide: Favors splitting at nodes with highest loss change.
|
| learning_rate : typing.Optional[float]
|
| Boosting learning rate (xgb's "eta")
|
| verbosity : typing.Optional[int]
|
| The degree of verbosity. Valid values are 0 (silent) - 3 (debug).
|
| objective : typing.Union[str, xgboost.sklearn._SklObjWProto, typing.Callable[[typing.Any, typing.Any], typing.Tuple[numpy.ndarray, numpy.ndarray]], NoneType]
|
| Specify the learning task and the corresponding learning objective or a custom
| objective function to be used.
|
| For custom objective, see :doc:`/tutorials/custom_metric_obj` and
| :ref:`custom-obj-metric` for more information, along with the end note for
| function signatures.
|
| booster: typing.Optional[str]
|
| Specify which booster to use: ``gbtree``, ``gblinear`` or ``dart``.
|
| .. deprecated:: 3.3.0
|
| ``gblinear`` is deprecated and support will be removed in a future release.
|
| tree_method : typing.Optional[str]
|
| Specify which tree method to use. Default to auto. If this parameter is set to
| default, XGBoost will choose the most conservative option available. It's
| recommended to study this option from the parameters document :doc:`tree method
| </treemethod>`
|
| n_jobs : typing.Optional[int]
|
| Number of parallel threads used to run xgboost. When used with other
| Scikit-Learn algorithms like grid search, you may choose which algorithm to
| parallelize and balance the threads. Creating thread contention will
| significantly slow down both algorithms.
|
| gamma : typing.Optional[float]
|
| (min_split_loss) Minimum loss reduction required to make a further partition on
| a leaf node of the tree.
|
| min_child_weight : typing.Optional[float]
|
| Minimum sum of instance weight(hessian) needed in a child.
|
| max_delta_step : typing.Optional[float]
|
| Maximum delta step we allow each tree's weight estimation to be.
|
| subsample : typing.Optional[float]
|
| Subsample ratio of the training instance.
|
| sampling_method : typing.Optional[str]
|
| Sampling method. Used only by the GPU version of ``hist`` tree method.
|
| - ``uniform``: Select random training instances uniformly.
| - ``gradient_based``: Select random training instances with higher probability
| when the gradient and hessian are larger. (cf. CatBoost)
|
| colsample_bytree : typing.Optional[float]
|
| Subsample ratio of columns when constructing each tree.
|
| colsample_bylevel : typing.Optional[float]
|
| Subsample ratio of columns for each level.
|
| colsample_bynode : typing.Optional[float]
|
| Subsample ratio of columns for each split.
|
| reg_alpha : typing.Optional[float]
|
| L1 regularization term on weights (xgb's alpha).
|
| reg_lambda : typing.Optional[float]
|
| L2 regularization term on weights (xgb's lambda).
|
| scale_pos_weight : typing.Optional[float]
| Balancing of positive and negative weights.
|
| base_score : typing.Union[float, typing.List[float], NoneType]
|
| The initial prediction score of all instances, global bias.
|
| random_state : typing.Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType]
|
| Random number seed.
|
| .. note::
|
| Using gblinear booster with shotgun updater is nondeterministic as
| it uses Hogwild algorithm.
|
| missing : float
|
| Value in the data which needs to be present as a missing value. Default to
| :py:data:`numpy.nan`.
|
| num_parallel_tree: typing.Optional[int]
|
| Used for boosting random forest.
|
| monotone_constraints : typing.Union[typing.Dict[str, int], str, NoneType]
|
| Constraint of variable monotonicity. See :doc:`tutorial </tutorials/monotonic>`
| for more information.
|
| interaction_constraints : typing.Union[str, typing.List[typing.Tuple[str]], NoneType]
|
| Constraints for interaction representing permitted interactions. The
| constraints must be specified in the form of a nested list, e.g. ``[[0, 1], [2,
| 3, 4]]``, where each inner list is a group of indices of features that are
| allowed to interact with each other. See :doc:`tutorial
| </tutorials/feature_interaction_constraint>` for more information
|
| importance_type: typing.Optional[str]
|
| The feature importance type for the feature_importances\_ property:
|
| * For tree model, it's either "gain", "weight", "cover", "total_gain" or
| "total_cover".
| * For linear model, only "weight" is defined and it's the normalized
| coefficients without bias.
|
| device : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| Device ordinal, available options are `cpu`, `cuda`, and `gpu`.
|
| validate_parameters : typing.Optional[bool]
|
| Give warnings for unknown parameter.
|
| enable_categorical : bool
|
| See the same parameter of :py:class:`DMatrix` for details.
|
| feature_types : typing.Optional[typing.Sequence[str]]
|
| .. versionadded:: 1.7.0
|
| Used for specifying feature types without constructing a dataframe. See
| the :py:class:`DMatrix` for details.
|
| feature_weights : Optional[ArrayLike]
|
| Weight for each feature, defines the probability of each feature being selected
| when colsample is being used. All values must be greater than 0, otherwise a
| `ValueError` is thrown.
|
| max_cat_to_onehot : Optional[int]
|
| .. versionadded:: 1.6.0
|
| .. note:: This parameter is experimental
|
| A threshold for deciding whether XGBoost should use one-hot encoding based split
| for categorical data. When number of categories is lesser than the threshold
| then one-hot encoding is chosen, otherwise the categories will be partitioned
| into children nodes. Also, `enable_categorical` needs to be set to have
| categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| max_cat_threshold : typing.Optional[int]
|
| .. versionadded:: 1.7.0
|
| .. note:: This parameter is experimental
|
| Maximum number of categories considered for each split. Used only by
| partition-based splits for preventing over-fitting. Also, `enable_categorical`
| needs to be set to have categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| multi_strategy : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| .. note:: This parameter is working-in-progress.
|
| The strategy used for training multi-target models, including multi-target
| regression and multi-class classification. See :doc:`/tutorials/multioutput` for
| more information.
|
| - ``one_output_per_tree``: One model for each target.
| - ``multi_output_tree``: Use multi-target trees.
|
| eval_metric : typing.Union[str, typing.List[typing.Union[str, typing.Callable]], typing.Callable, NoneType]
|
| .. versionadded:: 1.6.0
|
| Metric used for monitoring the training result and early stopping. It can be a
| string or list of strings as names of predefined metric in XGBoost (See
| :doc:`/parameter`), one of the metrics in :py:mod:`sklearn.metrics`, or any
| other user defined metric that looks like `sklearn.metrics`.
|
| If custom objective is also provided, then custom metric should implement the
| corresponding reverse link function.
|
| Unlike the `scoring` parameter commonly used in scikit-learn, when a callable
| object is provided, it's assumed to be a cost function and by default XGBoost
| will minimize the result during early stopping.
|
| For advanced usage on Early stopping like directly choosing to maximize instead
| of minimize, see :py:obj:`xgboost.callback.EarlyStopping`.
|
| See :doc:`/tutorials/custom_metric_obj` and :ref:`custom-obj-metric` for more
| information.
|
| .. code-block:: python
|
| from sklearn.datasets import load_diabetes
| from sklearn.metrics import mean_absolute_error
| X, y = load_diabetes(return_X_y=True)
| reg = xgb.XGBRegressor(
| tree_method="hist",
| eval_metric=mean_absolute_error,
| )
| reg.fit(X, y, eval_set=[(X, y)])
|
| early_stopping_rounds : typing.Optional[int]
|
| .. versionadded:: 1.6.0
|
| - Activates early stopping. Validation metric needs to improve at least once in
| every **early_stopping_rounds** round(s) to continue training. Requires at
| least one item in **eval_set** in :py:meth:`fit`.
|
| - If early stopping occurs, the model will have two additional attributes:
| :py:attr:`best_score` and :py:attr:`best_iteration`. These are used by the
| :py:meth:`predict` and :py:meth:`apply` methods to determine the optimal
| number of trees during inference. If users want to access the full model
| (including trees built after early stopping), they can specify the
| `iteration_range` in these inference methods. In addition, other utilities
| like model plotting can also use the entire model.
|
| - If you prefer to discard the trees after `best_iteration`, consider using the
| callback function :py:class:`xgboost.callback.EarlyStopping`.
|
| - If there's more than one item in **eval_set**, the last entry will be used for
| early stopping. If there's more than one metric in **eval_metric**, the last
| metric will be used for early stopping.
|
| callbacks : typing.Optional[typing.List[xgboost.callback.TrainingCallback]]
|
| List of callback functions that are applied at end of each iteration.
| It is possible to use predefined callbacks by using
| :ref:`Callback API <callback_api>`.
|
| .. note::
|
| States in callback are not preserved during training, which means callback
| objects can not be reused for multiple training sessions without
| reinitialization or deepcopy.
|
| .. code-block:: python
|
| for params in parameters_grid:
| # be sure to (re)initialize the callbacks before each run
| callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
| reg = xgboost.XGBRegressor(**params, callbacks=callbacks)
| reg.fit(X, y)
|
| kwargs : typing.Optional[typing.Any]
|
| Keyword arguments for XGBoost Booster object. Full documentation of parameters
| can be found :doc:`here </parameter>`.
| Attempting to set a parameter via the constructor args and \*\*kwargs
| dict simultaneously will result in a TypeError.
|
| .. note:: \*\*kwargs unsupported by scikit-learn
|
| \*\*kwargs is unsupported by scikit-learn. We do not guarantee
| that parameters passed via this argument will interact properly
| with scikit-learn.
|
| .. note:: Custom objective function
|
| A custom objective function can be provided for the ``objective``
| parameter. In this case, it should have the signature ``objective(y_true,
| y_pred) -> [grad, hess]`` or ``objective(y_true, y_pred, *, sample_weight)
| -> [grad, hess]``:
|
| y_true: array_like of shape [n_samples]
| The target values
| y_pred: array_like of shape [n_samples]
| The predicted values
| sample_weight :
| Optional sample weights.
|
| grad: array_like of shape [n_samples]
| The value of the gradient for each sample point.
| hess: array_like of shape [n_samples]
| The value of the second derivative for each sample point
|
| Note that, if the custom objective produces negative values for
| the Hessian, these will be clipped. If the objective is non-convex,
| one might also consider using the expected Hessian (Fisher
| information) instead.
|
| Method resolution order:
| XGBRFClassifier
| XGBClassifier
| sklearn.base.ClassifierMixin
| XGBModel
| sklearn.base.BaseEstimator
| sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin
| sklearn.utils._metadata_requests._MetadataRequester
| builtins.object
|
| Methods defined here:
|
| __init__(self, *, learning_rate: float = 1.0, subsample: float = 0.8, colsample_bynode: float = 0.8, reg_lambda: float = 1e-05, **kwargs: Any)
| Initialize self. See help(type(self)) for accurate signature.
|
| fit(self, X: Any, y: Any, *, sample_weight: Optional[Any] = None, base_margin: Optional[Any] = None, eval_set: Optional[Sequence[Tuple[Any, Any]]] = None, verbose: Union[bool, int, NoneType] = True, xgb_model: Union[xgboost.core.Booster, str, xgboost.sklearn.XGBModel, NoneType] = None, sample_weight_eval_set: Optional[Sequence[Any]] = None, base_margin_eval_set: Optional[Sequence[Any]] = None, feature_weights: Optional[Any] = None) -> 'XGBRFClassifier'
| Fit gradient boosting classifier.
|
| Note that calling ``fit()`` multiple times will cause the model object to be
| re-fit from scratch. To resume training from a previous checkpoint, explicitly
| pass ``xgb_model`` argument.
|
| Parameters
| ----------
| X :
| Input feature matrix. See :ref:`py-data` for a list of supported types.
|
| When the ``tree_method`` is set to ``hist``, internally, the
| :py:class:`QuantileDMatrix` will be used instead of the :py:class:`DMatrix`
| for conserving memory. However, this has performance implications when the
| device of input data is not matched with algorithm. For instance, if the
| input is a numpy array on CPU but ``cuda`` is used for training, then the
| data is first processed on CPU then transferred to GPU.
| y :
| Labels
| sample_weight :
| instance weights
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| eval_set :
| A list of (X, y) tuple pairs to use as validation sets, for which
| metrics will be computed.
| Validation metrics will help us track the performance of the model.
|
| verbose :
| If `verbose` is True and an evaluation set is used, the evaluation metric
| measured on the validation set is printed to stdout at each boosting stage.
| If `verbose` is an integer, the evaluation metric is printed at each
| `verbose` boosting stage. The last boosting stage / the boosting stage found
| by using `early_stopping_rounds` is also printed.
| xgb_model :
| file name of stored XGBoost model or 'Booster' instance XGBoost model to be
| loaded before training (allows training continuation).
| sample_weight_eval_set :
| A list of the form [L_1, L_2, ..., L_n], where each L_i is an array like
| object storing instance weights for the i-th validation set.
| base_margin_eval_set :
| A list of the form [M_1, M_2, ..., M_n], where each M_i is an array like
| object storing base margin for the i-th validation set.
| feature_weights :
|
| .. deprecated:: 3.0.0
|
| Use `feature_weights` in :py:meth:`__init__` or :py:meth:`set_params`
| instead.
|
| get_num_boosting_rounds(self) -> int
| Gets the number of xgboost boosting rounds.
|
| get_xgb_params(self) -> Dict[str, Any]
| Get xgboost specific parameters.
|
| set_fit_request(self: xgboost.sklearn.XGBRFClassifier, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', base_margin_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', feature_weights: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', verbose: Union[bool, NoneType, str] = '$UNCHANGED$', xgb_model: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRFClassifier from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``fit`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``fit`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``fit``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``fit``.
|
| base_margin_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin_eval_set`` parameter in ``fit``.
|
| eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``eval_set`` parameter in ``fit``.
|
| feature_weights : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``feature_weights`` parameter in ``fit``.
|
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``fit``.
|
| sample_weight_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight_eval_set`` parameter in ``fit``.
|
| verbose : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``verbose`` parameter in ``fit``.
|
| xgb_model : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``xgb_model`` parameter in ``fit``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_predict_proba_request(self: xgboost.sklearn.XGBRFClassifier, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', iteration_range: Union[bool, NoneType, str] = '$UNCHANGED$', validate_features: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRFClassifier from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``predict_proba`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``predict_proba`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``predict_proba``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``predict_proba``.
|
| iteration_range : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``iteration_range`` parameter in ``predict_proba``.
|
| validate_features : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``validate_features`` parameter in ``predict_proba``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_predict_request(self: xgboost.sklearn.XGBRFClassifier, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', iteration_range: Union[bool, NoneType, str] = '$UNCHANGED$', output_margin: Union[bool, NoneType, str] = '$UNCHANGED$', validate_features: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRFClassifier from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``predict`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``predict`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``predict``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``predict``.
|
| iteration_range : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``iteration_range`` parameter in ``predict``.
|
| output_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``output_margin`` parameter in ``predict``.
|
| validate_features : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``validate_features`` parameter in ``predict``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_score_request(self: xgboost.sklearn.XGBRFClassifier, *, sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRFClassifier from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``score`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``score`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``score``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``score``.
|
| Returns
| -------
| self : object
| The updated object.
|
| ----------------------------------------------------------------------
| Methods inherited from XGBClassifier:
|
| __sklearn_tags__(self) -> object
|
| predict(self, X: Any, *, output_margin: bool = False, validate_features: bool = True, base_margin: Optional[Any] = None, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> Any
| Predict with `X`. If the model is trained with early stopping, then
| :py:attr:`best_iteration` is used automatically. The estimator uses
| `inplace_predict` by default and falls back to using :py:class:`DMatrix` if
| devices between the data and the estimator don't match.
|
| .. note:: This function is only thread safe for `gbtree` and `dart`.
|
| Parameters
| ----------
| X :
| Data to predict with. See :ref:`py-data` for a list of supported types.
| output_margin :
| Whether to output the raw untransformed margin value.
| validate_features :
| When this is True, validate that the Booster's and data's feature_names are
| identical. Otherwise, it is assumed that the feature_names are the same.
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying ``iteration_range=(10,
| 20)``, then only the forests built during [10, 20) (half open set) rounds
| are used in this prediction.
|
| .. versionadded:: 1.4.0
|
| Returns
| -------
| prediction
|
| predict_proba(self, X: Any, validate_features: bool = True, base_margin: Optional[Any] = None, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> numpy.ndarray
| Predict the probability of each `X` example being of a given class. If the
| model is trained with early stopping, then :py:attr:`best_iteration` is used
| automatically. The estimator uses `inplace_predict` by default and falls back to
| using :py:class:`DMatrix` if devices between the data and the estimator don't
| match.
|
| .. note:: This function is only thread safe for `gbtree` and `dart`.
|
| Parameters
| ----------
| X :
| Feature matrix. See :ref:`py-data` for a list of supported types.
| validate_features :
| When this is True, validate that the Booster's and data's feature_names are
| identical. Otherwise, it is assumed that the feature_names are the same.
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying `iteration_range=(10,
| 20)`, then only the forests built during [10, 20) (half open set) rounds are
| used in this prediction.
|
| Returns
| -------
| prediction :
| a numpy array of shape array-like of shape (n_samples, n_classes) with the
| probability of each data example being of a given class.
|
| ----------------------------------------------------------------------
| Readonly properties inherited from XGBClassifier:
|
| classes_
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.ClassifierMixin:
|
| score(self, X, y, sample_weight=None)
| Return the mean accuracy on the given test data and labels.
|
| In multi-label classification, this is the subset accuracy
| which is a harsh metric since you require for each sample that
| each label set be correctly predicted.
|
| Parameters
| ----------
| X : array-like of shape (n_samples, n_features)
| Test samples.
|
| y : array-like of shape (n_samples,) or (n_samples, n_outputs)
| True labels for `X`.
|
| sample_weight : array-like of shape (n_samples,), default=None
| Sample weights.
|
| Returns
| -------
| score : float
| Mean accuracy of ``self.predict(X)`` w.r.t. `y`.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from sklearn.base.ClassifierMixin:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Methods inherited from XGBModel:
|
| __sklearn_is_fitted__(self) -> bool
|
| apply(self, X: Any, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> numpy.ndarray
| Return the predicted leaf every tree for each sample. If the model is trained
| with early stopping, then :py:attr:`best_iteration` is used automatically.
|
| Parameters
| ----------
| X :
| Input features matrix. See :ref:`py-data` for a list of supported types.
|
| iteration_range :
| See :py:meth:`predict`.
|
| Returns
| -------
| X_leaves : array_like, shape=[n_samples, n_trees]
| For each datapoint x in X and for each tree, return the index of the
| leaf x ends up in. Leaves are numbered within
| ``[0; 2**(self.max_depth+1))``, possibly with gaps in the numbering.
|
| evals_result(self) -> Dict[str, Dict[str, List[float]]]
| Return the evaluation results.
|
| If **eval_set** is passed to the :py:meth:`fit` function, you can call
| ``evals_result()`` to get evaluation results for all passed **eval_sets**. When
| **eval_metric** is also passed to the :py:meth:`fit` function, the
| **evals_result** will contain the **eval_metrics** passed to the :py:meth:`fit`
| function.
|
| The returned evaluation result is a dictionary:
|
| .. code-block:: python
|
| {'validation_0': {'logloss': ['0.604835', '0.531479']},
| 'validation_1': {'logloss': ['0.41965', '0.17686']}}
|
| Returns
| -------
| evals_result
|
| get_booster(self) -> xgboost.core.Booster
| Get the underlying xgboost Booster of this model.
|
| This will raise an exception when fit was not called
|
| Returns
| -------
| booster : a xgboost booster of underlying model
|
| get_params(self, deep: bool = True) -> Dict[str, Any]
| Get parameters.
|
| load_model(self, fname: Union[os.PathLike[~AnyStr], bytearray, str]) -> None
| Load the model from a file or a bytearray.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| model.load_model("model.json")
|
| # or
| model.save_model("model.ubj")
| model.load_model("model.ubj")
|
| # or
| buf = model.save_raw()
| model.load_model(buf)
|
| Parameters
| ----------
| fname :
| Input file name or memory buffer(see also save_raw)
|
| save_model(self, fname: Union[str, os.PathLike]) -> None
| Save the model to a file.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| # or
| model.save_model("model.ubj")
|
| Parameters
| ----------
| fname :
| Output file name
|
| set_params(self, **params: Any) -> 'XGBModel'
| Set the parameters of this estimator. Modification of the sklearn method to
| allow unknown kwargs. This allows using the full range of xgboost
| parameters that are not defined as member variables in sklearn grid
| search.
|
| Returns
| -------
| self
|
| ----------------------------------------------------------------------
| Readonly properties inherited from XGBModel:
|
| best_iteration
| The best iteration obtained by early stopping. This attribute is 0-based,
| for instance if the best iteration is the first round, then best_iteration is 0.
|
| best_score
| The best score obtained by early stopping.
|
| coef_
| Coefficients property
|
| .. note:: Coefficients are defined only for linear learners
|
| Coefficients are only defined when the linear model is chosen as
| base learner (`booster=gblinear`). It is not defined for other base
| learner types, such as tree learners (`booster=gbtree`).
|
| Returns
| -------
| coef_ : array of shape ``[n_features]`` or ``[n_classes, n_features]``
|
| feature_importances_
| Feature importances property, return depends on `importance_type`
| parameter. When model trained with multi-class/multi-label/multi-target dataset,
| the feature importance is "averaged" over all targets. The "average" is defined
| based on the importance type. For instance, if the importance type is
| "total_gain", then the score is sum of loss change for each split from all
| trees.
|
| Returns
| -------
| feature_importances_ : array of shape ``[n_features]`` except for multi-class
| linear model, which returns an array with shape `(n_features, n_classes)`
|
| feature_names_in_
| Names of features seen during :py:meth:`fit`. Defined only when `X` has
| feature names that are all strings.
|
| intercept_
| Intercept (bias) property
|
| For tree-based model, the returned value is the `base_score`.
|
| Returns
| -------
| intercept_ : array of shape ``(1,)`` or ``[n_classes]``
|
| n_features_in_
| Number of features seen during :py:meth:`fit`.
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.BaseEstimator:
|
| __getstate__(self)
| Helper for pickle.
|
| __repr__(self, N_CHAR_MAX=700)
| Return repr(self).
|
| __setstate__(self, state)
|
| __sklearn_clone__(self)
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| get_metadata_routing(self)
| Get metadata routing of this object.
|
| Please check :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| Returns
| -------
| routing : MetadataRequest
| A :class:`~sklearn.utils.metadata_routing.MetadataRequest` encapsulating
| routing information.
|
| ----------------------------------------------------------------------
| Class methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| __init_subclass__(**kwargs)
| Set the ``set_{method}_request`` methods.
|
| This uses PEP-487 [1]_ to set the ``set_{method}_request`` methods. It
| looks for the information available in the set default values which are
| set using ``__metadata_request__*`` class attributes, or inferred
| from method signatures.
|
| The ``__metadata_request__*`` class attributes are used when a method
| does not explicitly accept a metadata through its arguments or if the
| developer would like to specify a request value for those metadata
| which are different from the default ``None``.
|
| References
| ----------
| .. [1] https://www.python.org/dev/peps/pep-0487
xgboost.XGBRFRegressor
Python Library Documentation: class XGBRFRegressor in module xgboost.sklearn
class XGBRFRegressor(XGBRegressor)
| XGBRFRegressor(*, learning_rate: float = 1.0, subsample: float = 0.8, colsample_bynode: float = 0.8, reg_lambda: float = 1e-05, **kwargs: Any) -> None
|
| scikit-learn API for XGBoost random forest regression.
| See :doc:`/python/sklearn_estimator` for more information.
|
| Parameters
| ----------
|
| n_estimators : Optional[int]
| Number of trees in random forest to fit.
|
| max_depth : typing.Optional[int]
|
| Maximum tree depth for base learners.
|
| max_leaves : typing.Optional[int]
|
| Maximum number of leaves; 0 indicates no limit.
|
| max_bin : typing.Optional[int]
|
| If using histogram-based algorithm, maximum number of bins per feature
|
| grow_policy : typing.Optional[str]
|
| Tree growing policy.
|
| - depthwise: Favors splitting at nodes closest to the node,
| - lossguide: Favors splitting at nodes with highest loss change.
|
| learning_rate : typing.Optional[float]
|
| Boosting learning rate (xgb's "eta")
|
| verbosity : typing.Optional[int]
|
| The degree of verbosity. Valid values are 0 (silent) - 3 (debug).
|
| objective : typing.Union[str, xgboost.sklearn._SklObjWProto, typing.Callable[[typing.Any, typing.Any], typing.Tuple[numpy.ndarray, numpy.ndarray]], NoneType]
|
| Specify the learning task and the corresponding learning objective or a custom
| objective function to be used.
|
| For custom objective, see :doc:`/tutorials/custom_metric_obj` and
| :ref:`custom-obj-metric` for more information, along with the end note for
| function signatures.
|
| booster: typing.Optional[str]
|
| Specify which booster to use: ``gbtree``, ``gblinear`` or ``dart``.
|
| .. deprecated:: 3.3.0
|
| ``gblinear`` is deprecated and support will be removed in a future release.
|
| tree_method : typing.Optional[str]
|
| Specify which tree method to use. Default to auto. If this parameter is set to
| default, XGBoost will choose the most conservative option available. It's
| recommended to study this option from the parameters document :doc:`tree method
| </treemethod>`
|
| n_jobs : typing.Optional[int]
|
| Number of parallel threads used to run xgboost. When used with other
| Scikit-Learn algorithms like grid search, you may choose which algorithm to
| parallelize and balance the threads. Creating thread contention will
| significantly slow down both algorithms.
|
| gamma : typing.Optional[float]
|
| (min_split_loss) Minimum loss reduction required to make a further partition on
| a leaf node of the tree.
|
| min_child_weight : typing.Optional[float]
|
| Minimum sum of instance weight(hessian) needed in a child.
|
| max_delta_step : typing.Optional[float]
|
| Maximum delta step we allow each tree's weight estimation to be.
|
| subsample : typing.Optional[float]
|
| Subsample ratio of the training instance.
|
| sampling_method : typing.Optional[str]
|
| Sampling method. Used only by the GPU version of ``hist`` tree method.
|
| - ``uniform``: Select random training instances uniformly.
| - ``gradient_based``: Select random training instances with higher probability
| when the gradient and hessian are larger. (cf. CatBoost)
|
| colsample_bytree : typing.Optional[float]
|
| Subsample ratio of columns when constructing each tree.
|
| colsample_bylevel : typing.Optional[float]
|
| Subsample ratio of columns for each level.
|
| colsample_bynode : typing.Optional[float]
|
| Subsample ratio of columns for each split.
|
| reg_alpha : typing.Optional[float]
|
| L1 regularization term on weights (xgb's alpha).
|
| reg_lambda : typing.Optional[float]
|
| L2 regularization term on weights (xgb's lambda).
|
| scale_pos_weight : typing.Optional[float]
| Balancing of positive and negative weights.
|
| base_score : typing.Union[float, typing.List[float], NoneType]
|
| The initial prediction score of all instances, global bias.
|
| random_state : typing.Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType]
|
| Random number seed.
|
| .. note::
|
| Using gblinear booster with shotgun updater is nondeterministic as
| it uses Hogwild algorithm.
|
| missing : float
|
| Value in the data which needs to be present as a missing value. Default to
| :py:data:`numpy.nan`.
|
| num_parallel_tree: typing.Optional[int]
|
| Used for boosting random forest.
|
| monotone_constraints : typing.Union[typing.Dict[str, int], str, NoneType]
|
| Constraint of variable monotonicity. See :doc:`tutorial </tutorials/monotonic>`
| for more information.
|
| interaction_constraints : typing.Union[str, typing.List[typing.Tuple[str]], NoneType]
|
| Constraints for interaction representing permitted interactions. The
| constraints must be specified in the form of a nested list, e.g. ``[[0, 1], [2,
| 3, 4]]``, where each inner list is a group of indices of features that are
| allowed to interact with each other. See :doc:`tutorial
| </tutorials/feature_interaction_constraint>` for more information
|
| importance_type: typing.Optional[str]
|
| The feature importance type for the feature_importances\_ property:
|
| * For tree model, it's either "gain", "weight", "cover", "total_gain" or
| "total_cover".
| * For linear model, only "weight" is defined and it's the normalized
| coefficients without bias.
|
| device : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| Device ordinal, available options are `cpu`, `cuda`, and `gpu`.
|
| validate_parameters : typing.Optional[bool]
|
| Give warnings for unknown parameter.
|
| enable_categorical : bool
|
| See the same parameter of :py:class:`DMatrix` for details.
|
| feature_types : typing.Optional[typing.Sequence[str]]
|
| .. versionadded:: 1.7.0
|
| Used for specifying feature types without constructing a dataframe. See
| the :py:class:`DMatrix` for details.
|
| feature_weights : Optional[ArrayLike]
|
| Weight for each feature, defines the probability of each feature being selected
| when colsample is being used. All values must be greater than 0, otherwise a
| `ValueError` is thrown.
|
| max_cat_to_onehot : Optional[int]
|
| .. versionadded:: 1.6.0
|
| .. note:: This parameter is experimental
|
| A threshold for deciding whether XGBoost should use one-hot encoding based split
| for categorical data. When number of categories is lesser than the threshold
| then one-hot encoding is chosen, otherwise the categories will be partitioned
| into children nodes. Also, `enable_categorical` needs to be set to have
| categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| max_cat_threshold : typing.Optional[int]
|
| .. versionadded:: 1.7.0
|
| .. note:: This parameter is experimental
|
| Maximum number of categories considered for each split. Used only by
| partition-based splits for preventing over-fitting. Also, `enable_categorical`
| needs to be set to have categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| multi_strategy : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| .. note:: This parameter is working-in-progress.
|
| The strategy used for training multi-target models, including multi-target
| regression and multi-class classification. See :doc:`/tutorials/multioutput` for
| more information.
|
| - ``one_output_per_tree``: One model for each target.
| - ``multi_output_tree``: Use multi-target trees.
|
| eval_metric : typing.Union[str, typing.List[typing.Union[str, typing.Callable]], typing.Callable, NoneType]
|
| .. versionadded:: 1.6.0
|
| Metric used for monitoring the training result and early stopping. It can be a
| string or list of strings as names of predefined metric in XGBoost (See
| :doc:`/parameter`), one of the metrics in :py:mod:`sklearn.metrics`, or any
| other user defined metric that looks like `sklearn.metrics`.
|
| If custom objective is also provided, then custom metric should implement the
| corresponding reverse link function.
|
| Unlike the `scoring` parameter commonly used in scikit-learn, when a callable
| object is provided, it's assumed to be a cost function and by default XGBoost
| will minimize the result during early stopping.
|
| For advanced usage on Early stopping like directly choosing to maximize instead
| of minimize, see :py:obj:`xgboost.callback.EarlyStopping`.
|
| See :doc:`/tutorials/custom_metric_obj` and :ref:`custom-obj-metric` for more
| information.
|
| .. code-block:: python
|
| from sklearn.datasets import load_diabetes
| from sklearn.metrics import mean_absolute_error
| X, y = load_diabetes(return_X_y=True)
| reg = xgb.XGBRegressor(
| tree_method="hist",
| eval_metric=mean_absolute_error,
| )
| reg.fit(X, y, eval_set=[(X, y)])
|
| early_stopping_rounds : typing.Optional[int]
|
| .. versionadded:: 1.6.0
|
| - Activates early stopping. Validation metric needs to improve at least once in
| every **early_stopping_rounds** round(s) to continue training. Requires at
| least one item in **eval_set** in :py:meth:`fit`.
|
| - If early stopping occurs, the model will have two additional attributes:
| :py:attr:`best_score` and :py:attr:`best_iteration`. These are used by the
| :py:meth:`predict` and :py:meth:`apply` methods to determine the optimal
| number of trees during inference. If users want to access the full model
| (including trees built after early stopping), they can specify the
| `iteration_range` in these inference methods. In addition, other utilities
| like model plotting can also use the entire model.
|
| - If you prefer to discard the trees after `best_iteration`, consider using the
| callback function :py:class:`xgboost.callback.EarlyStopping`.
|
| - If there's more than one item in **eval_set**, the last entry will be used for
| early stopping. If there's more than one metric in **eval_metric**, the last
| metric will be used for early stopping.
|
| callbacks : typing.Optional[typing.List[xgboost.callback.TrainingCallback]]
|
| List of callback functions that are applied at end of each iteration.
| It is possible to use predefined callbacks by using
| :ref:`Callback API <callback_api>`.
|
| .. note::
|
| States in callback are not preserved during training, which means callback
| objects can not be reused for multiple training sessions without
| reinitialization or deepcopy.
|
| .. code-block:: python
|
| for params in parameters_grid:
| # be sure to (re)initialize the callbacks before each run
| callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
| reg = xgboost.XGBRegressor(**params, callbacks=callbacks)
| reg.fit(X, y)
|
| kwargs : typing.Optional[typing.Any]
|
| Keyword arguments for XGBoost Booster object. Full documentation of parameters
| can be found :doc:`here </parameter>`.
| Attempting to set a parameter via the constructor args and \*\*kwargs
| dict simultaneously will result in a TypeError.
|
| .. note:: \*\*kwargs unsupported by scikit-learn
|
| \*\*kwargs is unsupported by scikit-learn. We do not guarantee
| that parameters passed via this argument will interact properly
| with scikit-learn.
|
| .. note:: Custom objective function
|
| A custom objective function can be provided for the ``objective``
| parameter. In this case, it should have the signature ``objective(y_true,
| y_pred) -> [grad, hess]`` or ``objective(y_true, y_pred, *, sample_weight)
| -> [grad, hess]``:
|
| y_true: array_like of shape [n_samples]
| The target values
| y_pred: array_like of shape [n_samples]
| The predicted values
| sample_weight :
| Optional sample weights.
|
| grad: array_like of shape [n_samples]
| The value of the gradient for each sample point.
| hess: array_like of shape [n_samples]
| The value of the second derivative for each sample point
|
| Note that, if the custom objective produces negative values for
| the Hessian, these will be clipped. If the objective is non-convex,
| one might also consider using the expected Hessian (Fisher
| information) instead.
|
| Method resolution order:
| XGBRFRegressor
| XGBRegressor
| sklearn.base.RegressorMixin
| XGBModel
| sklearn.base.BaseEstimator
| sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin
| sklearn.utils._metadata_requests._MetadataRequester
| builtins.object
|
| Methods defined here:
|
| __init__(self, *, learning_rate: float = 1.0, subsample: float = 0.8, colsample_bynode: float = 0.8, reg_lambda: float = 1e-05, **kwargs: Any) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| fit(self, X: Any, y: Any, *, sample_weight: Optional[Any] = None, base_margin: Optional[Any] = None, eval_set: Optional[Sequence[Tuple[Any, Any]]] = None, verbose: Union[bool, int, NoneType] = True, xgb_model: Union[xgboost.core.Booster, str, xgboost.sklearn.XGBModel, NoneType] = None, sample_weight_eval_set: Optional[Sequence[Any]] = None, base_margin_eval_set: Optional[Sequence[Any]] = None, feature_weights: Optional[Any] = None) -> 'XGBRFRegressor'
| Fit gradient boosting model.
|
| Note that calling ``fit()`` multiple times will cause the model object to be
| re-fit from scratch. To resume training from a previous checkpoint, explicitly
| pass ``xgb_model`` argument.
|
| Parameters
| ----------
| X :
| Input feature matrix. See :ref:`py-data` for a list of supported types.
|
| When the ``tree_method`` is set to ``hist``, internally, the
| :py:class:`QuantileDMatrix` will be used instead of the :py:class:`DMatrix`
| for conserving memory. However, this has performance implications when the
| device of input data is not matched with algorithm. For instance, if the
| input is a numpy array on CPU but ``cuda`` is used for training, then the
| data is first processed on CPU then transferred to GPU.
| y :
| Labels
| sample_weight :
| instance weights
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| eval_set :
| A list of (X, y) tuple pairs to use as validation sets, for which
| metrics will be computed.
| Validation metrics will help us track the performance of the model.
|
| verbose :
| If `verbose` is True and an evaluation set is used, the evaluation metric
| measured on the validation set is printed to stdout at each boosting stage.
| If `verbose` is an integer, the evaluation metric is printed at each
| `verbose` boosting stage. The last boosting stage / the boosting stage found
| by using `early_stopping_rounds` is also printed.
| xgb_model :
| file name of stored XGBoost model or 'Booster' instance XGBoost model to be
| loaded before training (allows training continuation).
| sample_weight_eval_set :
| A list of the form [L_1, L_2, ..., L_n], where each L_i is an array like
| object storing instance weights for the i-th validation set.
| base_margin_eval_set :
| A list of the form [M_1, M_2, ..., M_n], where each M_i is an array like
| object storing base margin for the i-th validation set.
| feature_weights :
|
| .. deprecated:: 3.0.0
|
| Use `feature_weights` in :py:meth:`__init__` or :py:meth:`set_params`
| instead.
|
| get_num_boosting_rounds(self) -> int
| Gets the number of xgboost boosting rounds.
|
| get_xgb_params(self) -> Dict[str, Any]
| Get xgboost specific parameters.
|
| set_fit_request(self: xgboost.sklearn.XGBRFRegressor, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', base_margin_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', feature_weights: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', verbose: Union[bool, NoneType, str] = '$UNCHANGED$', xgb_model: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRFRegressor from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``fit`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``fit`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``fit``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``fit``.
|
| base_margin_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin_eval_set`` parameter in ``fit``.
|
| eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``eval_set`` parameter in ``fit``.
|
| feature_weights : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``feature_weights`` parameter in ``fit``.
|
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``fit``.
|
| sample_weight_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight_eval_set`` parameter in ``fit``.
|
| verbose : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``verbose`` parameter in ``fit``.
|
| xgb_model : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``xgb_model`` parameter in ``fit``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_predict_request(self: xgboost.sklearn.XGBRFRegressor, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', iteration_range: Union[bool, NoneType, str] = '$UNCHANGED$', output_margin: Union[bool, NoneType, str] = '$UNCHANGED$', validate_features: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRFRegressor from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``predict`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``predict`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``predict``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``predict``.
|
| iteration_range : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``iteration_range`` parameter in ``predict``.
|
| output_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``output_margin`` parameter in ``predict``.
|
| validate_features : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``validate_features`` parameter in ``predict``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_score_request(self: xgboost.sklearn.XGBRFRegressor, *, sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRFRegressor from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``score`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``score`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``score``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``score``.
|
| Returns
| -------
| self : object
| The updated object.
|
| ----------------------------------------------------------------------
| Methods inherited from XGBRegressor:
|
| __sklearn_tags__(self) -> object
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.RegressorMixin:
|
| score(self, X, y, sample_weight=None)
| Return the coefficient of determination of the prediction.
|
| The coefficient of determination :math:`R^2` is defined as
| :math:`(1 - \frac{u}{v})`, where :math:`u` is the residual
| sum of squares ``((y_true - y_pred)** 2).sum()`` and :math:`v`
| is the total sum of squares ``((y_true - y_true.mean()) ** 2).sum()``.
| The best possible score is 1.0 and it can be negative (because the
| model can be arbitrarily worse). A constant model that always predicts
| the expected value of `y`, disregarding the input features, would get
| a :math:`R^2` score of 0.0.
|
| Parameters
| ----------
| X : array-like of shape (n_samples, n_features)
| Test samples. For some estimators this may be a precomputed
| kernel matrix or a list of generic objects instead with shape
| ``(n_samples, n_samples_fitted)``, where ``n_samples_fitted``
| is the number of samples used in the fitting for the estimator.
|
| y : array-like of shape (n_samples,) or (n_samples, n_outputs)
| True values for `X`.
|
| sample_weight : array-like of shape (n_samples,), default=None
| Sample weights.
|
| Returns
| -------
| score : float
| :math:`R^2` of ``self.predict(X)`` w.r.t. `y`.
|
| Notes
| -----
| The :math:`R^2` score used when calling ``score`` on a regressor uses
| ``multioutput='uniform_average'`` from version 0.23 to keep consistent
| with default value of :func:`~sklearn.metrics.r2_score`.
| This influences the ``score`` method of all the multioutput
| regressors (except for
| :class:`~sklearn.multioutput.MultiOutputRegressor`).
|
| ----------------------------------------------------------------------
| Data descriptors inherited from sklearn.base.RegressorMixin:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Methods inherited from XGBModel:
|
| __sklearn_is_fitted__(self) -> bool
|
| apply(self, X: Any, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> numpy.ndarray
| Return the predicted leaf every tree for each sample. If the model is trained
| with early stopping, then :py:attr:`best_iteration` is used automatically.
|
| Parameters
| ----------
| X :
| Input features matrix. See :ref:`py-data` for a list of supported types.
|
| iteration_range :
| See :py:meth:`predict`.
|
| Returns
| -------
| X_leaves : array_like, shape=[n_samples, n_trees]
| For each datapoint x in X and for each tree, return the index of the
| leaf x ends up in. Leaves are numbered within
| ``[0; 2**(self.max_depth+1))``, possibly with gaps in the numbering.
|
| evals_result(self) -> Dict[str, Dict[str, List[float]]]
| Return the evaluation results.
|
| If **eval_set** is passed to the :py:meth:`fit` function, you can call
| ``evals_result()`` to get evaluation results for all passed **eval_sets**. When
| **eval_metric** is also passed to the :py:meth:`fit` function, the
| **evals_result** will contain the **eval_metrics** passed to the :py:meth:`fit`
| function.
|
| The returned evaluation result is a dictionary:
|
| .. code-block:: python
|
| {'validation_0': {'logloss': ['0.604835', '0.531479']},
| 'validation_1': {'logloss': ['0.41965', '0.17686']}}
|
| Returns
| -------
| evals_result
|
| get_booster(self) -> xgboost.core.Booster
| Get the underlying xgboost Booster of this model.
|
| This will raise an exception when fit was not called
|
| Returns
| -------
| booster : a xgboost booster of underlying model
|
| get_params(self, deep: bool = True) -> Dict[str, Any]
| Get parameters.
|
| load_model(self, fname: Union[os.PathLike[~AnyStr], bytearray, str]) -> None
| Load the model from a file or a bytearray.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| model.load_model("model.json")
|
| # or
| model.save_model("model.ubj")
| model.load_model("model.ubj")
|
| # or
| buf = model.save_raw()
| model.load_model(buf)
|
| Parameters
| ----------
| fname :
| Input file name or memory buffer(see also save_raw)
|
| predict(self, X: Any, *, output_margin: bool = False, validate_features: bool = True, base_margin: Optional[Any] = None, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> Any
| Predict with `X`. If the model is trained with early stopping, then
| :py:attr:`best_iteration` is used automatically. The estimator uses
| `inplace_predict` by default and falls back to using :py:class:`DMatrix` if
| devices between the data and the estimator don't match.
|
| .. note:: This function is only thread safe for `gbtree` and `dart`.
|
| Parameters
| ----------
| X :
| Data to predict with. See :ref:`py-data` for a list of supported types.
| output_margin :
| Whether to output the raw untransformed margin value.
| validate_features :
| When this is True, validate that the Booster's and data's feature_names are
| identical. Otherwise, it is assumed that the feature_names are the same.
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying ``iteration_range=(10,
| 20)``, then only the forests built during [10, 20) (half open set) rounds
| are used in this prediction.
|
| .. versionadded:: 1.4.0
|
| Returns
| -------
| prediction
|
| save_model(self, fname: Union[str, os.PathLike]) -> None
| Save the model to a file.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| # or
| model.save_model("model.ubj")
|
| Parameters
| ----------
| fname :
| Output file name
|
| set_params(self, **params: Any) -> 'XGBModel'
| Set the parameters of this estimator. Modification of the sklearn method to
| allow unknown kwargs. This allows using the full range of xgboost
| parameters that are not defined as member variables in sklearn grid
| search.
|
| Returns
| -------
| self
|
| ----------------------------------------------------------------------
| Readonly properties inherited from XGBModel:
|
| best_iteration
| The best iteration obtained by early stopping. This attribute is 0-based,
| for instance if the best iteration is the first round, then best_iteration is 0.
|
| best_score
| The best score obtained by early stopping.
|
| coef_
| Coefficients property
|
| .. note:: Coefficients are defined only for linear learners
|
| Coefficients are only defined when the linear model is chosen as
| base learner (`booster=gblinear`). It is not defined for other base
| learner types, such as tree learners (`booster=gbtree`).
|
| Returns
| -------
| coef_ : array of shape ``[n_features]`` or ``[n_classes, n_features]``
|
| feature_importances_
| Feature importances property, return depends on `importance_type`
| parameter. When model trained with multi-class/multi-label/multi-target dataset,
| the feature importance is "averaged" over all targets. The "average" is defined
| based on the importance type. For instance, if the importance type is
| "total_gain", then the score is sum of loss change for each split from all
| trees.
|
| Returns
| -------
| feature_importances_ : array of shape ``[n_features]`` except for multi-class
| linear model, which returns an array with shape `(n_features, n_classes)`
|
| feature_names_in_
| Names of features seen during :py:meth:`fit`. Defined only when `X` has
| feature names that are all strings.
|
| intercept_
| Intercept (bias) property
|
| For tree-based model, the returned value is the `base_score`.
|
| Returns
| -------
| intercept_ : array of shape ``(1,)`` or ``[n_classes]``
|
| n_features_in_
| Number of features seen during :py:meth:`fit`.
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.BaseEstimator:
|
| __getstate__(self)
| Helper for pickle.
|
| __repr__(self, N_CHAR_MAX=700)
| Return repr(self).
|
| __setstate__(self, state)
|
| __sklearn_clone__(self)
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| get_metadata_routing(self)
| Get metadata routing of this object.
|
| Please check :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| Returns
| -------
| routing : MetadataRequest
| A :class:`~sklearn.utils.metadata_routing.MetadataRequest` encapsulating
| routing information.
|
| ----------------------------------------------------------------------
| Class methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| __init_subclass__(**kwargs)
| Set the ``set_{method}_request`` methods.
|
| This uses PEP-487 [1]_ to set the ``set_{method}_request`` methods. It
| looks for the information available in the set default values which are
| set using ``__metadata_request__*`` class attributes, or inferred
| from method signatures.
|
| The ``__metadata_request__*`` class attributes are used when a method
| does not explicitly accept a metadata through its arguments or if the
| developer would like to specify a request value for those metadata
| which are different from the default ``None``.
|
| References
| ----------
| .. [1] https://www.python.org/dev/peps/pep-0487
xgboost.XGBRanker
Python Library Documentation: class XGBRanker in module xgboost.sklearn
class XGBRanker(XGBRankerMixIn, XGBModel)
| XGBRanker(*, objective: str = 'rank:ndcg', **kwargs: Any)
|
| Implementation of the Scikit-Learn API for XGBoost Ranking.
|
| See :doc:`Learning to Rank </tutorials/learning_to_rank>` for an introducion.
|
|
| See :doc:`/python/sklearn_estimator` for more information.
|
| Parameters
| ----------
|
| n_estimators : typing.Optional[int]
| Number of gradient boosted trees. Equivalent to number of boosting
| rounds.
|
| max_depth : typing.Optional[int]
|
| Maximum tree depth for base learners.
|
| max_leaves : typing.Optional[int]
|
| Maximum number of leaves; 0 indicates no limit.
|
| max_bin : typing.Optional[int]
|
| If using histogram-based algorithm, maximum number of bins per feature
|
| grow_policy : typing.Optional[str]
|
| Tree growing policy.
|
| - depthwise: Favors splitting at nodes closest to the node,
| - lossguide: Favors splitting at nodes with highest loss change.
|
| learning_rate : typing.Optional[float]
|
| Boosting learning rate (xgb's "eta")
|
| verbosity : typing.Optional[int]
|
| The degree of verbosity. Valid values are 0 (silent) - 3 (debug).
|
| objective : typing.Union[str, xgboost.sklearn._SklObjWProto, typing.Callable[[typing.Any, typing.Any], typing.Tuple[numpy.ndarray, numpy.ndarray]], NoneType]
|
| Specify the learning task and the corresponding learning objective or a custom
| objective function to be used.
|
| For custom objective, see :doc:`/tutorials/custom_metric_obj` and
| :ref:`custom-obj-metric` for more information, along with the end note for
| function signatures.
|
| booster: typing.Optional[str]
|
| Specify which booster to use: ``gbtree``, ``gblinear`` or ``dart``.
|
| .. deprecated:: 3.3.0
|
| ``gblinear`` is deprecated and support will be removed in a future release.
|
| tree_method : typing.Optional[str]
|
| Specify which tree method to use. Default to auto. If this parameter is set to
| default, XGBoost will choose the most conservative option available. It's
| recommended to study this option from the parameters document :doc:`tree method
| </treemethod>`
|
| n_jobs : typing.Optional[int]
|
| Number of parallel threads used to run xgboost. When used with other
| Scikit-Learn algorithms like grid search, you may choose which algorithm to
| parallelize and balance the threads. Creating thread contention will
| significantly slow down both algorithms.
|
| gamma : typing.Optional[float]
|
| (min_split_loss) Minimum loss reduction required to make a further partition on
| a leaf node of the tree.
|
| min_child_weight : typing.Optional[float]
|
| Minimum sum of instance weight(hessian) needed in a child.
|
| max_delta_step : typing.Optional[float]
|
| Maximum delta step we allow each tree's weight estimation to be.
|
| subsample : typing.Optional[float]
|
| Subsample ratio of the training instance.
|
| sampling_method : typing.Optional[str]
|
| Sampling method. Used only by the GPU version of ``hist`` tree method.
|
| - ``uniform``: Select random training instances uniformly.
| - ``gradient_based``: Select random training instances with higher probability
| when the gradient and hessian are larger. (cf. CatBoost)
|
| colsample_bytree : typing.Optional[float]
|
| Subsample ratio of columns when constructing each tree.
|
| colsample_bylevel : typing.Optional[float]
|
| Subsample ratio of columns for each level.
|
| colsample_bynode : typing.Optional[float]
|
| Subsample ratio of columns for each split.
|
| reg_alpha : typing.Optional[float]
|
| L1 regularization term on weights (xgb's alpha).
|
| reg_lambda : typing.Optional[float]
|
| L2 regularization term on weights (xgb's lambda).
|
| scale_pos_weight : typing.Optional[float]
| Balancing of positive and negative weights.
|
| base_score : typing.Union[float, typing.List[float], NoneType]
|
| The initial prediction score of all instances, global bias.
|
| random_state : typing.Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType]
|
| Random number seed.
|
| .. note::
|
| Using gblinear booster with shotgun updater is nondeterministic as
| it uses Hogwild algorithm.
|
| missing : float
|
| Value in the data which needs to be present as a missing value. Default to
| :py:data:`numpy.nan`.
|
| num_parallel_tree: typing.Optional[int]
|
| Used for boosting random forest.
|
| monotone_constraints : typing.Union[typing.Dict[str, int], str, NoneType]
|
| Constraint of variable monotonicity. See :doc:`tutorial </tutorials/monotonic>`
| for more information.
|
| interaction_constraints : typing.Union[str, typing.List[typing.Tuple[str]], NoneType]
|
| Constraints for interaction representing permitted interactions. The
| constraints must be specified in the form of a nested list, e.g. ``[[0, 1], [2,
| 3, 4]]``, where each inner list is a group of indices of features that are
| allowed to interact with each other. See :doc:`tutorial
| </tutorials/feature_interaction_constraint>` for more information
|
| importance_type: typing.Optional[str]
|
| The feature importance type for the feature_importances\_ property:
|
| * For tree model, it's either "gain", "weight", "cover", "total_gain" or
| "total_cover".
| * For linear model, only "weight" is defined and it's the normalized
| coefficients without bias.
|
| device : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| Device ordinal, available options are `cpu`, `cuda`, and `gpu`.
|
| validate_parameters : typing.Optional[bool]
|
| Give warnings for unknown parameter.
|
| enable_categorical : bool
|
| See the same parameter of :py:class:`DMatrix` for details.
|
| feature_types : typing.Optional[typing.Sequence[str]]
|
| .. versionadded:: 1.7.0
|
| Used for specifying feature types without constructing a dataframe. See
| the :py:class:`DMatrix` for details.
|
| feature_weights : Optional[ArrayLike]
|
| Weight for each feature, defines the probability of each feature being selected
| when colsample is being used. All values must be greater than 0, otherwise a
| `ValueError` is thrown.
|
| max_cat_to_onehot : Optional[int]
|
| .. versionadded:: 1.6.0
|
| .. note:: This parameter is experimental
|
| A threshold for deciding whether XGBoost should use one-hot encoding based split
| for categorical data. When number of categories is lesser than the threshold
| then one-hot encoding is chosen, otherwise the categories will be partitioned
| into children nodes. Also, `enable_categorical` needs to be set to have
| categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| max_cat_threshold : typing.Optional[int]
|
| .. versionadded:: 1.7.0
|
| .. note:: This parameter is experimental
|
| Maximum number of categories considered for each split. Used only by
| partition-based splits for preventing over-fitting. Also, `enable_categorical`
| needs to be set to have categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| multi_strategy : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| .. note:: This parameter is working-in-progress.
|
| The strategy used for training multi-target models, including multi-target
| regression and multi-class classification. See :doc:`/tutorials/multioutput` for
| more information.
|
| - ``one_output_per_tree``: One model for each target.
| - ``multi_output_tree``: Use multi-target trees.
|
| eval_metric : typing.Union[str, typing.List[typing.Union[str, typing.Callable]], typing.Callable, NoneType]
|
| .. versionadded:: 1.6.0
|
| Metric used for monitoring the training result and early stopping. It can be a
| string or list of strings as names of predefined metric in XGBoost (See
| :doc:`/parameter`), one of the metrics in :py:mod:`sklearn.metrics`, or any
| other user defined metric that looks like `sklearn.metrics`.
|
| If custom objective is also provided, then custom metric should implement the
| corresponding reverse link function.
|
| Unlike the `scoring` parameter commonly used in scikit-learn, when a callable
| object is provided, it's assumed to be a cost function and by default XGBoost
| will minimize the result during early stopping.
|
| For advanced usage on Early stopping like directly choosing to maximize instead
| of minimize, see :py:obj:`xgboost.callback.EarlyStopping`.
|
| See :doc:`/tutorials/custom_metric_obj` and :ref:`custom-obj-metric` for more
| information.
|
| .. code-block:: python
|
| from sklearn.datasets import load_diabetes
| from sklearn.metrics import mean_absolute_error
| X, y = load_diabetes(return_X_y=True)
| reg = xgb.XGBRegressor(
| tree_method="hist",
| eval_metric=mean_absolute_error,
| )
| reg.fit(X, y, eval_set=[(X, y)])
|
| early_stopping_rounds : typing.Optional[int]
|
| .. versionadded:: 1.6.0
|
| - Activates early stopping. Validation metric needs to improve at least once in
| every **early_stopping_rounds** round(s) to continue training. Requires at
| least one item in **eval_set** in :py:meth:`fit`.
|
| - If early stopping occurs, the model will have two additional attributes:
| :py:attr:`best_score` and :py:attr:`best_iteration`. These are used by the
| :py:meth:`predict` and :py:meth:`apply` methods to determine the optimal
| number of trees during inference. If users want to access the full model
| (including trees built after early stopping), they can specify the
| `iteration_range` in these inference methods. In addition, other utilities
| like model plotting can also use the entire model.
|
| - If you prefer to discard the trees after `best_iteration`, consider using the
| callback function :py:class:`xgboost.callback.EarlyStopping`.
|
| - If there's more than one item in **eval_set**, the last entry will be used for
| early stopping. If there's more than one metric in **eval_metric**, the last
| metric will be used for early stopping.
|
| callbacks : typing.Optional[typing.List[xgboost.callback.TrainingCallback]]
|
| List of callback functions that are applied at end of each iteration.
| It is possible to use predefined callbacks by using
| :ref:`Callback API <callback_api>`.
|
| .. note::
|
| States in callback are not preserved during training, which means callback
| objects can not be reused for multiple training sessions without
| reinitialization or deepcopy.
|
| .. code-block:: python
|
| for params in parameters_grid:
| # be sure to (re)initialize the callbacks before each run
| callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
| reg = xgboost.XGBRegressor(**params, callbacks=callbacks)
| reg.fit(X, y)
|
| kwargs : typing.Optional[typing.Any]
|
| Keyword arguments for XGBoost Booster object. Full documentation of parameters
| can be found :doc:`here </parameter>`.
| Attempting to set a parameter via the constructor args and \*\*kwargs
| dict simultaneously will result in a TypeError.
|
| .. note:: \*\*kwargs unsupported by scikit-learn
|
| \*\*kwargs is unsupported by scikit-learn. We do not guarantee
| that parameters passed via this argument will interact properly
| with scikit-learn.
|
| .. note::
|
| A custom objective function is currently not supported by XGBRanker.
|
| .. note::
|
| Query group information is only required for ranking training but not
| prediction. Multiple groups can be predicted on a single call to
| :py:meth:`predict`.
|
| When fitting the model with the `group` parameter, your data need to be sorted
| by the query group first. `group` is an array that contains the size of each
| query group.
|
| Similarly, when fitting the model with the `qid` parameter, the data should be
| sorted according to query index and `qid` is an array that contains the query
| index for each training sample.
|
| For example, if your original data look like:
|
| +-------+-----------+---------------+
| | qid | label | features |
| +-------+-----------+---------------+
| | 1 | 0 | x_1 |
| +-------+-----------+---------------+
| | 1 | 1 | x_2 |
| +-------+-----------+---------------+
| | 1 | 0 | x_3 |
| +-------+-----------+---------------+
| | 2 | 0 | x_4 |
| +-------+-----------+---------------+
| | 2 | 1 | x_5 |
| +-------+-----------+---------------+
| | 2 | 1 | x_6 |
| +-------+-----------+---------------+
| | 2 | 1 | x_7 |
| +-------+-----------+---------------+
|
| then :py:meth:`fit` method can be called with either `group` array as ``[3, 4]``
| or with `qid` as ``[1, 1, 1, 2, 2, 2, 2]``, that is the qid column. Also, the
| `qid` can be a special column of input `X` instead of a separated parameter, see
| :py:meth:`fit` for more info.
|
| Method resolution order:
| XGBRanker
| XGBRankerMixIn
| XGBModel
| sklearn.base.BaseEstimator
| sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin
| sklearn.utils._metadata_requests._MetadataRequester
| builtins.object
|
| Methods defined here:
|
| __init__(self, *, objective: str = 'rank:ndcg', **kwargs: Any)
| Initialize self. See help(type(self)) for accurate signature.
|
| apply(self, X: Any, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> Any
| Return the predicted leaf every tree for each sample. If the model is trained
| with early stopping, then :py:attr:`best_iteration` is used automatically.
|
| Parameters
| ----------
| X :
| Input features matrix. See :ref:`py-data` for a list of supported types.
|
| iteration_range :
| See :py:meth:`predict`.
|
| Returns
| -------
| X_leaves : array_like, shape=[n_samples, n_trees]
| For each datapoint x in X and for each tree, return the index of the
| leaf x ends up in. Leaves are numbered within
| ``[0; 2**(self.max_depth+1))``, possibly with gaps in the numbering.
|
| fit(self, X: Any, y: Any, *, group: Optional[Any] = None, qid: Optional[Any] = None, sample_weight: Optional[Any] = None, base_margin: Optional[Any] = None, eval_set: Optional[Sequence[Tuple[Any, Any]]] = None, eval_group: Optional[Sequence[Any]] = None, eval_qid: Optional[Sequence[Any]] = None, verbose: Union[bool, int, NoneType] = True, xgb_model: Union[xgboost.core.Booster, str, xgboost.sklearn.XGBModel, NoneType] = None, sample_weight_eval_set: Optional[Sequence[Any]] = None, base_margin_eval_set: Optional[Sequence[Any]] = None, feature_weights: Optional[Any] = None) -> 'XGBRanker'
| Fit gradient boosting ranker
|
| Note that calling ``fit()`` multiple times will cause the model object to be
| re-fit from scratch. To resume training from a previous checkpoint, explicitly
| pass ``xgb_model`` argument.
|
| Parameters
| ----------
| X :
| Feature matrix. See :ref:`py-data` for a list of supported types.
|
| When this is a :py:class:`pandas.DataFrame` or a :py:class:`cudf.DataFrame`,
| it may contain a special column called ``qid`` for specifying the query
| index. Using a special column is the same as using the `qid` parameter,
| except for being compatible with sklearn utility functions like
| :py:func:`sklearn.model_selection.cross_validation`. The same convention
| applies to the :py:meth:`XGBRanker.score` and :py:meth:`XGBRanker.predict`.
|
| +-----+----------------+----------------+
| | qid | feat_0 | feat_1 |
| +-----+----------------+----------------+
| | 0 | :math:`x_{00}` | :math:`x_{01}` |
| +-----+----------------+----------------+
| | 1 | :math:`x_{10}` | :math:`x_{11}` |
| +-----+----------------+----------------+
| | 1 | :math:`x_{20}` | :math:`x_{21}` |
| +-----+----------------+----------------+
|
| When the ``tree_method`` is set to ``hist``, internally, the
| :py:class:`QuantileDMatrix` will be used instead of the :py:class:`DMatrix`
| for conserving memory. However, this has performance implications when the
| device of input data is not matched with algorithm. For instance, if the
| input is a numpy array on CPU but ``cuda`` is used for training, then the
| data is first processed on CPU then transferred to GPU.
| y :
| Labels
| group :
| Size of each query group of training data. Should have as many elements as
| the query groups in the training data. If this is set to None, then user
| must provide qid.
| qid :
| Query ID for each training sample. Should have the size of n_samples. If
| this is set to None, then user must provide group or a special column in X.
| sample_weight :
| Query group weights
|
| .. note:: Weights are per-group for ranking tasks
|
| In ranking task, one weight is assigned to each query group/id (not each
| data point). This is because we only care about the relative ordering of
| data points within each group, so it doesn't make sense to assign
| weights to individual data points.
|
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| eval_set :
| A list of (X, y) tuple pairs to use as validation sets, for which
| metrics will be computed.
| Validation metrics will help us track the performance of the model.
| eval_group :
| A list in which ``eval_group[i]`` is the list containing the sizes of all
| query groups in the ``i``-th pair in **eval_set**.
| eval_qid :
| A list in which ``eval_qid[i]`` is the array containing query ID of ``i``-th
| pair in **eval_set**. The special column convention in `X` applies to
| validation datasets as well.
|
| verbose :
| If `verbose` is True and an evaluation set is used, the evaluation metric
| measured on the validation set is printed to stdout at each boosting stage.
| If `verbose` is an integer, the evaluation metric is printed at each
| `verbose` boosting stage. The last boosting stage / the boosting stage found
| by using `early_stopping_rounds` is also printed.
| xgb_model :
| file name of stored XGBoost model or 'Booster' instance XGBoost model to be
| loaded before training (allows training continuation).
| sample_weight_eval_set :
| A list of the form [L_1, L_2, ..., L_n], where each L_i is a list of
| group weights on the i-th validation set.
|
| .. note:: Weights are per-group for ranking tasks
|
| In ranking task, one weight is assigned to each query group (not each
| data point). This is because we only care about the relative ordering of
| data points within each group, so it doesn't make sense to assign
| weights to individual data points.
| base_margin_eval_set :
| A list of the form [M_1, M_2, ..., M_n], where each M_i is an array like
| object storing base margin for the i-th validation set.
| feature_weights :
| Weight for each feature, defines the probability of each feature being
| selected when colsample is being used. All values must be greater than 0,
| otherwise a `ValueError` is thrown.
|
| predict(self, X: Any, *, output_margin: bool = False, validate_features: bool = True, base_margin: Optional[Any] = None, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> Any
| Predict with `X`. If the model is trained with early stopping, then
| :py:attr:`best_iteration` is used automatically. The estimator uses
| `inplace_predict` by default and falls back to using :py:class:`DMatrix` if
| devices between the data and the estimator don't match.
|
| .. note:: This function is only thread safe for `gbtree` and `dart`.
|
| Parameters
| ----------
| X :
| Data to predict with. See :ref:`py-data` for a list of supported types.
| output_margin :
| Whether to output the raw untransformed margin value.
| validate_features :
| When this is True, validate that the Booster's and data's feature_names are
| identical. Otherwise, it is assumed that the feature_names are the same.
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying ``iteration_range=(10,
| 20)``, then only the forests built during [10, 20) (half open set) rounds
| are used in this prediction.
|
| .. versionadded:: 1.4.0
|
| Returns
| -------
| prediction
|
| score(self, X: Any, y: Any) -> float
| Evaluate score for data using the last evaluation metric. If the model is
| trained with early stopping, then :py:attr:`best_iteration` is used
| automatically.
|
| Parameters
| ----------
| X : Union[pd.DataFrame, cudf.DataFrame]
| Feature matrix. A DataFrame with a special `qid` column.
|
| y :
| Labels
|
| Returns
| -------
| score :
| The result of the first evaluation metric for the ranker.
|
| set_fit_request(self: xgboost.sklearn.XGBRanker, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', base_margin_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', eval_group: Union[bool, NoneType, str] = '$UNCHANGED$', eval_qid: Union[bool, NoneType, str] = '$UNCHANGED$', eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', feature_weights: Union[bool, NoneType, str] = '$UNCHANGED$', group: Union[bool, NoneType, str] = '$UNCHANGED$', qid: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', verbose: Union[bool, NoneType, str] = '$UNCHANGED$', xgb_model: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRanker from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``fit`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``fit`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``fit``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``fit``.
|
| base_margin_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin_eval_set`` parameter in ``fit``.
|
| eval_group : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``eval_group`` parameter in ``fit``.
|
| eval_qid : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``eval_qid`` parameter in ``fit``.
|
| eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``eval_set`` parameter in ``fit``.
|
| feature_weights : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``feature_weights`` parameter in ``fit``.
|
| group : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``group`` parameter in ``fit``.
|
| qid : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``qid`` parameter in ``fit``.
|
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``fit``.
|
| sample_weight_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight_eval_set`` parameter in ``fit``.
|
| verbose : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``verbose`` parameter in ``fit``.
|
| xgb_model : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``xgb_model`` parameter in ``fit``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_predict_request(self: xgboost.sklearn.XGBRanker, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', iteration_range: Union[bool, NoneType, str] = '$UNCHANGED$', output_margin: Union[bool, NoneType, str] = '$UNCHANGED$', validate_features: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRanker from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``predict`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``predict`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``predict``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``predict``.
|
| iteration_range : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``iteration_range`` parameter in ``predict``.
|
| output_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``output_margin`` parameter in ``predict``.
|
| validate_features : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``validate_features`` parameter in ``predict``.
|
| Returns
| -------
| self : object
| The updated object.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from XGBRankerMixIn:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Methods inherited from XGBModel:
|
| __sklearn_is_fitted__(self) -> bool
|
| __sklearn_tags__(self) -> object
|
| evals_result(self) -> Dict[str, Dict[str, List[float]]]
| Return the evaluation results.
|
| If **eval_set** is passed to the :py:meth:`fit` function, you can call
| ``evals_result()`` to get evaluation results for all passed **eval_sets**. When
| **eval_metric** is also passed to the :py:meth:`fit` function, the
| **evals_result** will contain the **eval_metrics** passed to the :py:meth:`fit`
| function.
|
| The returned evaluation result is a dictionary:
|
| .. code-block:: python
|
| {'validation_0': {'logloss': ['0.604835', '0.531479']},
| 'validation_1': {'logloss': ['0.41965', '0.17686']}}
|
| Returns
| -------
| evals_result
|
| get_booster(self) -> xgboost.core.Booster
| Get the underlying xgboost Booster of this model.
|
| This will raise an exception when fit was not called
|
| Returns
| -------
| booster : a xgboost booster of underlying model
|
| get_num_boosting_rounds(self) -> int
| Gets the number of xgboost boosting rounds.
|
| get_params(self, deep: bool = True) -> Dict[str, Any]
| Get parameters.
|
| get_xgb_params(self) -> Dict[str, Any]
| Get xgboost specific parameters.
|
| load_model(self, fname: Union[os.PathLike[~AnyStr], bytearray, str]) -> None
| Load the model from a file or a bytearray.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| model.load_model("model.json")
|
| # or
| model.save_model("model.ubj")
| model.load_model("model.ubj")
|
| # or
| buf = model.save_raw()
| model.load_model(buf)
|
| Parameters
| ----------
| fname :
| Input file name or memory buffer(see also save_raw)
|
| save_model(self, fname: Union[str, os.PathLike]) -> None
| Save the model to a file.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| # or
| model.save_model("model.ubj")
|
| Parameters
| ----------
| fname :
| Output file name
|
| set_params(self, **params: Any) -> 'XGBModel'
| Set the parameters of this estimator. Modification of the sklearn method to
| allow unknown kwargs. This allows using the full range of xgboost
| parameters that are not defined as member variables in sklearn grid
| search.
|
| Returns
| -------
| self
|
| ----------------------------------------------------------------------
| Readonly properties inherited from XGBModel:
|
| best_iteration
| The best iteration obtained by early stopping. This attribute is 0-based,
| for instance if the best iteration is the first round, then best_iteration is 0.
|
| best_score
| The best score obtained by early stopping.
|
| coef_
| Coefficients property
|
| .. note:: Coefficients are defined only for linear learners
|
| Coefficients are only defined when the linear model is chosen as
| base learner (`booster=gblinear`). It is not defined for other base
| learner types, such as tree learners (`booster=gbtree`).
|
| Returns
| -------
| coef_ : array of shape ``[n_features]`` or ``[n_classes, n_features]``
|
| feature_importances_
| Feature importances property, return depends on `importance_type`
| parameter. When model trained with multi-class/multi-label/multi-target dataset,
| the feature importance is "averaged" over all targets. The "average" is defined
| based on the importance type. For instance, if the importance type is
| "total_gain", then the score is sum of loss change for each split from all
| trees.
|
| Returns
| -------
| feature_importances_ : array of shape ``[n_features]`` except for multi-class
| linear model, which returns an array with shape `(n_features, n_classes)`
|
| feature_names_in_
| Names of features seen during :py:meth:`fit`. Defined only when `X` has
| feature names that are all strings.
|
| intercept_
| Intercept (bias) property
|
| For tree-based model, the returned value is the `base_score`.
|
| Returns
| -------
| intercept_ : array of shape ``(1,)`` or ``[n_classes]``
|
| n_features_in_
| Number of features seen during :py:meth:`fit`.
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.BaseEstimator:
|
| __getstate__(self)
| Helper for pickle.
|
| __repr__(self, N_CHAR_MAX=700)
| Return repr(self).
|
| __setstate__(self, state)
|
| __sklearn_clone__(self)
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| get_metadata_routing(self)
| Get metadata routing of this object.
|
| Please check :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| Returns
| -------
| routing : MetadataRequest
| A :class:`~sklearn.utils.metadata_routing.MetadataRequest` encapsulating
| routing information.
|
| ----------------------------------------------------------------------
| Class methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| __init_subclass__(**kwargs)
| Set the ``set_{method}_request`` methods.
|
| This uses PEP-487 [1]_ to set the ``set_{method}_request`` methods. It
| looks for the information available in the set default values which are
| set using ``__metadata_request__*`` class attributes, or inferred
| from method signatures.
|
| The ``__metadata_request__*`` class attributes are used when a method
| does not explicitly accept a metadata through its arguments or if the
| developer would like to specify a request value for those metadata
| which are different from the default ``None``.
|
| References
| ----------
| .. [1] https://www.python.org/dev/peps/pep-0487
xgboost.XGBRegressor
Python Library Documentation: class XGBRegressor in module xgboost.sklearn
class XGBRegressor(sklearn.base.RegressorMixin, XGBModel)
| XGBRegressor(*, objective: Union[str, xgboost.sklearn._SklObjWProto, Callable[[Any, Any], Tuple[numpy.ndarray, numpy.ndarray]], NoneType] = 'reg:squarederror', **kwargs: Any) -> None
|
| Implementation of the scikit-learn API for XGBoost regression.
| See :doc:`/python/sklearn_estimator` for more information.
|
| Parameters
| ----------
|
| n_estimators : typing.Optional[int]
| Number of gradient boosted trees. Equivalent to number of boosting
| rounds.
|
| max_depth : typing.Optional[int]
|
| Maximum tree depth for base learners.
|
| max_leaves : typing.Optional[int]
|
| Maximum number of leaves; 0 indicates no limit.
|
| max_bin : typing.Optional[int]
|
| If using histogram-based algorithm, maximum number of bins per feature
|
| grow_policy : typing.Optional[str]
|
| Tree growing policy.
|
| - depthwise: Favors splitting at nodes closest to the node,
| - lossguide: Favors splitting at nodes with highest loss change.
|
| learning_rate : typing.Optional[float]
|
| Boosting learning rate (xgb's "eta")
|
| verbosity : typing.Optional[int]
|
| The degree of verbosity. Valid values are 0 (silent) - 3 (debug).
|
| objective : typing.Union[str, xgboost.sklearn._SklObjWProto, typing.Callable[[typing.Any, typing.Any], typing.Tuple[numpy.ndarray, numpy.ndarray]], NoneType]
|
| Specify the learning task and the corresponding learning objective or a custom
| objective function to be used.
|
| For custom objective, see :doc:`/tutorials/custom_metric_obj` and
| :ref:`custom-obj-metric` for more information, along with the end note for
| function signatures.
|
| booster: typing.Optional[str]
|
| Specify which booster to use: ``gbtree``, ``gblinear`` or ``dart``.
|
| .. deprecated:: 3.3.0
|
| ``gblinear`` is deprecated and support will be removed in a future release.
|
| tree_method : typing.Optional[str]
|
| Specify which tree method to use. Default to auto. If this parameter is set to
| default, XGBoost will choose the most conservative option available. It's
| recommended to study this option from the parameters document :doc:`tree method
| </treemethod>`
|
| n_jobs : typing.Optional[int]
|
| Number of parallel threads used to run xgboost. When used with other
| Scikit-Learn algorithms like grid search, you may choose which algorithm to
| parallelize and balance the threads. Creating thread contention will
| significantly slow down both algorithms.
|
| gamma : typing.Optional[float]
|
| (min_split_loss) Minimum loss reduction required to make a further partition on
| a leaf node of the tree.
|
| min_child_weight : typing.Optional[float]
|
| Minimum sum of instance weight(hessian) needed in a child.
|
| max_delta_step : typing.Optional[float]
|
| Maximum delta step we allow each tree's weight estimation to be.
|
| subsample : typing.Optional[float]
|
| Subsample ratio of the training instance.
|
| sampling_method : typing.Optional[str]
|
| Sampling method. Used only by the GPU version of ``hist`` tree method.
|
| - ``uniform``: Select random training instances uniformly.
| - ``gradient_based``: Select random training instances with higher probability
| when the gradient and hessian are larger. (cf. CatBoost)
|
| colsample_bytree : typing.Optional[float]
|
| Subsample ratio of columns when constructing each tree.
|
| colsample_bylevel : typing.Optional[float]
|
| Subsample ratio of columns for each level.
|
| colsample_bynode : typing.Optional[float]
|
| Subsample ratio of columns for each split.
|
| reg_alpha : typing.Optional[float]
|
| L1 regularization term on weights (xgb's alpha).
|
| reg_lambda : typing.Optional[float]
|
| L2 regularization term on weights (xgb's lambda).
|
| scale_pos_weight : typing.Optional[float]
| Balancing of positive and negative weights.
|
| base_score : typing.Union[float, typing.List[float], NoneType]
|
| The initial prediction score of all instances, global bias.
|
| random_state : typing.Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType]
|
| Random number seed.
|
| .. note::
|
| Using gblinear booster with shotgun updater is nondeterministic as
| it uses Hogwild algorithm.
|
| missing : float
|
| Value in the data which needs to be present as a missing value. Default to
| :py:data:`numpy.nan`.
|
| num_parallel_tree: typing.Optional[int]
|
| Used for boosting random forest.
|
| monotone_constraints : typing.Union[typing.Dict[str, int], str, NoneType]
|
| Constraint of variable monotonicity. See :doc:`tutorial </tutorials/monotonic>`
| for more information.
|
| interaction_constraints : typing.Union[str, typing.List[typing.Tuple[str]], NoneType]
|
| Constraints for interaction representing permitted interactions. The
| constraints must be specified in the form of a nested list, e.g. ``[[0, 1], [2,
| 3, 4]]``, where each inner list is a group of indices of features that are
| allowed to interact with each other. See :doc:`tutorial
| </tutorials/feature_interaction_constraint>` for more information
|
| importance_type: typing.Optional[str]
|
| The feature importance type for the feature_importances\_ property:
|
| * For tree model, it's either "gain", "weight", "cover", "total_gain" or
| "total_cover".
| * For linear model, only "weight" is defined and it's the normalized
| coefficients without bias.
|
| device : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| Device ordinal, available options are `cpu`, `cuda`, and `gpu`.
|
| validate_parameters : typing.Optional[bool]
|
| Give warnings for unknown parameter.
|
| enable_categorical : bool
|
| See the same parameter of :py:class:`DMatrix` for details.
|
| feature_types : typing.Optional[typing.Sequence[str]]
|
| .. versionadded:: 1.7.0
|
| Used for specifying feature types without constructing a dataframe. See
| the :py:class:`DMatrix` for details.
|
| feature_weights : Optional[ArrayLike]
|
| Weight for each feature, defines the probability of each feature being selected
| when colsample is being used. All values must be greater than 0, otherwise a
| `ValueError` is thrown.
|
| max_cat_to_onehot : Optional[int]
|
| .. versionadded:: 1.6.0
|
| .. note:: This parameter is experimental
|
| A threshold for deciding whether XGBoost should use one-hot encoding based split
| for categorical data. When number of categories is lesser than the threshold
| then one-hot encoding is chosen, otherwise the categories will be partitioned
| into children nodes. Also, `enable_categorical` needs to be set to have
| categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| max_cat_threshold : typing.Optional[int]
|
| .. versionadded:: 1.7.0
|
| .. note:: This parameter is experimental
|
| Maximum number of categories considered for each split. Used only by
| partition-based splits for preventing over-fitting. Also, `enable_categorical`
| needs to be set to have categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| multi_strategy : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| .. note:: This parameter is working-in-progress.
|
| The strategy used for training multi-target models, including multi-target
| regression and multi-class classification. See :doc:`/tutorials/multioutput` for
| more information.
|
| - ``one_output_per_tree``: One model for each target.
| - ``multi_output_tree``: Use multi-target trees.
|
| eval_metric : typing.Union[str, typing.List[typing.Union[str, typing.Callable]], typing.Callable, NoneType]
|
| .. versionadded:: 1.6.0
|
| Metric used for monitoring the training result and early stopping. It can be a
| string or list of strings as names of predefined metric in XGBoost (See
| :doc:`/parameter`), one of the metrics in :py:mod:`sklearn.metrics`, or any
| other user defined metric that looks like `sklearn.metrics`.
|
| If custom objective is also provided, then custom metric should implement the
| corresponding reverse link function.
|
| Unlike the `scoring` parameter commonly used in scikit-learn, when a callable
| object is provided, it's assumed to be a cost function and by default XGBoost
| will minimize the result during early stopping.
|
| For advanced usage on Early stopping like directly choosing to maximize instead
| of minimize, see :py:obj:`xgboost.callback.EarlyStopping`.
|
| See :doc:`/tutorials/custom_metric_obj` and :ref:`custom-obj-metric` for more
| information.
|
| .. code-block:: python
|
| from sklearn.datasets import load_diabetes
| from sklearn.metrics import mean_absolute_error
| X, y = load_diabetes(return_X_y=True)
| reg = xgb.XGBRegressor(
| tree_method="hist",
| eval_metric=mean_absolute_error,
| )
| reg.fit(X, y, eval_set=[(X, y)])
|
| early_stopping_rounds : typing.Optional[int]
|
| .. versionadded:: 1.6.0
|
| - Activates early stopping. Validation metric needs to improve at least once in
| every **early_stopping_rounds** round(s) to continue training. Requires at
| least one item in **eval_set** in :py:meth:`fit`.
|
| - If early stopping occurs, the model will have two additional attributes:
| :py:attr:`best_score` and :py:attr:`best_iteration`. These are used by the
| :py:meth:`predict` and :py:meth:`apply` methods to determine the optimal
| number of trees during inference. If users want to access the full model
| (including trees built after early stopping), they can specify the
| `iteration_range` in these inference methods. In addition, other utilities
| like model plotting can also use the entire model.
|
| - If you prefer to discard the trees after `best_iteration`, consider using the
| callback function :py:class:`xgboost.callback.EarlyStopping`.
|
| - If there's more than one item in **eval_set**, the last entry will be used for
| early stopping. If there's more than one metric in **eval_metric**, the last
| metric will be used for early stopping.
|
| callbacks : typing.Optional[typing.List[xgboost.callback.TrainingCallback]]
|
| List of callback functions that are applied at end of each iteration.
| It is possible to use predefined callbacks by using
| :ref:`Callback API <callback_api>`.
|
| .. note::
|
| States in callback are not preserved during training, which means callback
| objects can not be reused for multiple training sessions without
| reinitialization or deepcopy.
|
| .. code-block:: python
|
| for params in parameters_grid:
| # be sure to (re)initialize the callbacks before each run
| callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
| reg = xgboost.XGBRegressor(**params, callbacks=callbacks)
| reg.fit(X, y)
|
| kwargs : typing.Optional[typing.Any]
|
| Keyword arguments for XGBoost Booster object. Full documentation of parameters
| can be found :doc:`here </parameter>`.
| Attempting to set a parameter via the constructor args and \*\*kwargs
| dict simultaneously will result in a TypeError.
|
| .. note:: \*\*kwargs unsupported by scikit-learn
|
| \*\*kwargs is unsupported by scikit-learn. We do not guarantee
| that parameters passed via this argument will interact properly
| with scikit-learn.
|
| .. note:: Custom objective function
|
| A custom objective function can be provided for the ``objective``
| parameter. In this case, it should have the signature ``objective(y_true,
| y_pred) -> [grad, hess]`` or ``objective(y_true, y_pred, *, sample_weight)
| -> [grad, hess]``:
|
| y_true: array_like of shape [n_samples]
| The target values
| y_pred: array_like of shape [n_samples]
| The predicted values
| sample_weight :
| Optional sample weights.
|
| grad: array_like of shape [n_samples]
| The value of the gradient for each sample point.
| hess: array_like of shape [n_samples]
| The value of the second derivative for each sample point
|
| Note that, if the custom objective produces negative values for
| the Hessian, these will be clipped. If the objective is non-convex,
| one might also consider using the expected Hessian (Fisher
| information) instead.
|
| Method resolution order:
| XGBRegressor
| sklearn.base.RegressorMixin
| XGBModel
| sklearn.base.BaseEstimator
| sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin
| sklearn.utils._metadata_requests._MetadataRequester
| builtins.object
|
| Methods defined here:
|
| __init__(self, *, objective: Union[str, xgboost.sklearn._SklObjWProto, Callable[[Any, Any], Tuple[numpy.ndarray, numpy.ndarray]], NoneType] = 'reg:squarederror', **kwargs: Any) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| __sklearn_tags__(self) -> object
|
| set_fit_request(self: xgboost.sklearn.XGBRegressor, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', base_margin_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', feature_weights: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', verbose: Union[bool, NoneType, str] = '$UNCHANGED$', xgb_model: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRegressor from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``fit`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``fit`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``fit``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``fit``.
|
| base_margin_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin_eval_set`` parameter in ``fit``.
|
| eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``eval_set`` parameter in ``fit``.
|
| feature_weights : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``feature_weights`` parameter in ``fit``.
|
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``fit``.
|
| sample_weight_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight_eval_set`` parameter in ``fit``.
|
| verbose : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``verbose`` parameter in ``fit``.
|
| xgb_model : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``xgb_model`` parameter in ``fit``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_predict_request(self: xgboost.sklearn.XGBRegressor, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', iteration_range: Union[bool, NoneType, str] = '$UNCHANGED$', output_margin: Union[bool, NoneType, str] = '$UNCHANGED$', validate_features: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRegressor from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``predict`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``predict`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``predict``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``predict``.
|
| iteration_range : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``iteration_range`` parameter in ``predict``.
|
| output_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``output_margin`` parameter in ``predict``.
|
| validate_features : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``validate_features`` parameter in ``predict``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_score_request(self: xgboost.sklearn.XGBRegressor, *, sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRegressor from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``score`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``score`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``score``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``score``.
|
| Returns
| -------
| self : object
| The updated object.
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.RegressorMixin:
|
| score(self, X, y, sample_weight=None)
| Return the coefficient of determination of the prediction.
|
| The coefficient of determination :math:`R^2` is defined as
| :math:`(1 - \frac{u}{v})`, where :math:`u` is the residual
| sum of squares ``((y_true - y_pred)** 2).sum()`` and :math:`v`
| is the total sum of squares ``((y_true - y_true.mean()) ** 2).sum()``.
| The best possible score is 1.0 and it can be negative (because the
| model can be arbitrarily worse). A constant model that always predicts
| the expected value of `y`, disregarding the input features, would get
| a :math:`R^2` score of 0.0.
|
| Parameters
| ----------
| X : array-like of shape (n_samples, n_features)
| Test samples. For some estimators this may be a precomputed
| kernel matrix or a list of generic objects instead with shape
| ``(n_samples, n_samples_fitted)``, where ``n_samples_fitted``
| is the number of samples used in the fitting for the estimator.
|
| y : array-like of shape (n_samples,) or (n_samples, n_outputs)
| True values for `X`.
|
| sample_weight : array-like of shape (n_samples,), default=None
| Sample weights.
|
| Returns
| -------
| score : float
| :math:`R^2` of ``self.predict(X)`` w.r.t. `y`.
|
| Notes
| -----
| The :math:`R^2` score used when calling ``score`` on a regressor uses
| ``multioutput='uniform_average'`` from version 0.23 to keep consistent
| with default value of :func:`~sklearn.metrics.r2_score`.
| This influences the ``score`` method of all the multioutput
| regressors (except for
| :class:`~sklearn.multioutput.MultiOutputRegressor`).
|
| ----------------------------------------------------------------------
| Data descriptors inherited from sklearn.base.RegressorMixin:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Methods inherited from XGBModel:
|
| __sklearn_is_fitted__(self) -> bool
|
| apply(self, X: Any, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> numpy.ndarray
| Return the predicted leaf every tree for each sample. If the model is trained
| with early stopping, then :py:attr:`best_iteration` is used automatically.
|
| Parameters
| ----------
| X :
| Input features matrix. See :ref:`py-data` for a list of supported types.
|
| iteration_range :
| See :py:meth:`predict`.
|
| Returns
| -------
| X_leaves : array_like, shape=[n_samples, n_trees]
| For each datapoint x in X and for each tree, return the index of the
| leaf x ends up in. Leaves are numbered within
| ``[0; 2**(self.max_depth+1))``, possibly with gaps in the numbering.
|
| evals_result(self) -> Dict[str, Dict[str, List[float]]]
| Return the evaluation results.
|
| If **eval_set** is passed to the :py:meth:`fit` function, you can call
| ``evals_result()`` to get evaluation results for all passed **eval_sets**. When
| **eval_metric** is also passed to the :py:meth:`fit` function, the
| **evals_result** will contain the **eval_metrics** passed to the :py:meth:`fit`
| function.
|
| The returned evaluation result is a dictionary:
|
| .. code-block:: python
|
| {'validation_0': {'logloss': ['0.604835', '0.531479']},
| 'validation_1': {'logloss': ['0.41965', '0.17686']}}
|
| Returns
| -------
| evals_result
|
| fit(self, X: Any, y: Any, *, sample_weight: Optional[Any] = None, base_margin: Optional[Any] = None, eval_set: Optional[Sequence[Tuple[Any, Any]]] = None, verbose: Union[bool, int, NoneType] = True, xgb_model: Union[xgboost.core.Booster, ForwardRef('XGBModel'), str, NoneType] = None, sample_weight_eval_set: Optional[Sequence[Any]] = None, base_margin_eval_set: Optional[Sequence[Any]] = None, feature_weights: Optional[Any] = None) -> 'XGBModel'
| Fit gradient boosting model.
|
| Note that calling ``fit()`` multiple times will cause the model object to be
| re-fit from scratch. To resume training from a previous checkpoint, explicitly
| pass ``xgb_model`` argument.
|
| Parameters
| ----------
| X :
| Input feature matrix. See :ref:`py-data` for a list of supported types.
|
| When the ``tree_method`` is set to ``hist``, internally, the
| :py:class:`QuantileDMatrix` will be used instead of the :py:class:`DMatrix`
| for conserving memory. However, this has performance implications when the
| device of input data is not matched with algorithm. For instance, if the
| input is a numpy array on CPU but ``cuda`` is used for training, then the
| data is first processed on CPU then transferred to GPU.
| y :
| Labels
| sample_weight :
| instance weights
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| eval_set :
| A list of (X, y) tuple pairs to use as validation sets, for which
| metrics will be computed.
| Validation metrics will help us track the performance of the model.
|
| verbose :
| If `verbose` is True and an evaluation set is used, the evaluation metric
| measured on the validation set is printed to stdout at each boosting stage.
| If `verbose` is an integer, the evaluation metric is printed at each
| `verbose` boosting stage. The last boosting stage / the boosting stage found
| by using `early_stopping_rounds` is also printed.
| xgb_model :
| file name of stored XGBoost model or 'Booster' instance XGBoost model to be
| loaded before training (allows training continuation).
| sample_weight_eval_set :
| A list of the form [L_1, L_2, ..., L_n], where each L_i is an array like
| object storing instance weights for the i-th validation set.
| base_margin_eval_set :
| A list of the form [M_1, M_2, ..., M_n], where each M_i is an array like
| object storing base margin for the i-th validation set.
| feature_weights :
|
| .. deprecated:: 3.0.0
|
| Use `feature_weights` in :py:meth:`__init__` or :py:meth:`set_params`
| instead.
|
| get_booster(self) -> xgboost.core.Booster
| Get the underlying xgboost Booster of this model.
|
| This will raise an exception when fit was not called
|
| Returns
| -------
| booster : a xgboost booster of underlying model
|
| get_num_boosting_rounds(self) -> int
| Gets the number of xgboost boosting rounds.
|
| get_params(self, deep: bool = True) -> Dict[str, Any]
| Get parameters.
|
| get_xgb_params(self) -> Dict[str, Any]
| Get xgboost specific parameters.
|
| load_model(self, fname: Union[os.PathLike[~AnyStr], bytearray, str]) -> None
| Load the model from a file or a bytearray.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| model.load_model("model.json")
|
| # or
| model.save_model("model.ubj")
| model.load_model("model.ubj")
|
| # or
| buf = model.save_raw()
| model.load_model(buf)
|
| Parameters
| ----------
| fname :
| Input file name or memory buffer(see also save_raw)
|
| predict(self, X: Any, *, output_margin: bool = False, validate_features: bool = True, base_margin: Optional[Any] = None, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> Any
| Predict with `X`. If the model is trained with early stopping, then
| :py:attr:`best_iteration` is used automatically. The estimator uses
| `inplace_predict` by default and falls back to using :py:class:`DMatrix` if
| devices between the data and the estimator don't match.
|
| .. note:: This function is only thread safe for `gbtree` and `dart`.
|
| Parameters
| ----------
| X :
| Data to predict with. See :ref:`py-data` for a list of supported types.
| output_margin :
| Whether to output the raw untransformed margin value.
| validate_features :
| When this is True, validate that the Booster's and data's feature_names are
| identical. Otherwise, it is assumed that the feature_names are the same.
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying ``iteration_range=(10,
| 20)``, then only the forests built during [10, 20) (half open set) rounds
| are used in this prediction.
|
| .. versionadded:: 1.4.0
|
| Returns
| -------
| prediction
|
| save_model(self, fname: Union[str, os.PathLike]) -> None
| Save the model to a file.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| # or
| model.save_model("model.ubj")
|
| Parameters
| ----------
| fname :
| Output file name
|
| set_params(self, **params: Any) -> 'XGBModel'
| Set the parameters of this estimator. Modification of the sklearn method to
| allow unknown kwargs. This allows using the full range of xgboost
| parameters that are not defined as member variables in sklearn grid
| search.
|
| Returns
| -------
| self
|
| ----------------------------------------------------------------------
| Readonly properties inherited from XGBModel:
|
| best_iteration
| The best iteration obtained by early stopping. This attribute is 0-based,
| for instance if the best iteration is the first round, then best_iteration is 0.
|
| best_score
| The best score obtained by early stopping.
|
| coef_
| Coefficients property
|
| .. note:: Coefficients are defined only for linear learners
|
| Coefficients are only defined when the linear model is chosen as
| base learner (`booster=gblinear`). It is not defined for other base
| learner types, such as tree learners (`booster=gbtree`).
|
| Returns
| -------
| coef_ : array of shape ``[n_features]`` or ``[n_classes, n_features]``
|
| feature_importances_
| Feature importances property, return depends on `importance_type`
| parameter. When model trained with multi-class/multi-label/multi-target dataset,
| the feature importance is "averaged" over all targets. The "average" is defined
| based on the importance type. For instance, if the importance type is
| "total_gain", then the score is sum of loss change for each split from all
| trees.
|
| Returns
| -------
| feature_importances_ : array of shape ``[n_features]`` except for multi-class
| linear model, which returns an array with shape `(n_features, n_classes)`
|
| feature_names_in_
| Names of features seen during :py:meth:`fit`. Defined only when `X` has
| feature names that are all strings.
|
| intercept_
| Intercept (bias) property
|
| For tree-based model, the returned value is the `base_score`.
|
| Returns
| -------
| intercept_ : array of shape ``(1,)`` or ``[n_classes]``
|
| n_features_in_
| Number of features seen during :py:meth:`fit`.
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.BaseEstimator:
|
| __getstate__(self)
| Helper for pickle.
|
| __repr__(self, N_CHAR_MAX=700)
| Return repr(self).
|
| __setstate__(self, state)
|
| __sklearn_clone__(self)
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| get_metadata_routing(self)
| Get metadata routing of this object.
|
| Please check :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| Returns
| -------
| routing : MetadataRequest
| A :class:`~sklearn.utils.metadata_routing.MetadataRequest` encapsulating
| routing information.
|
| ----------------------------------------------------------------------
| Class methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| __init_subclass__(**kwargs)
| Set the ``set_{method}_request`` methods.
|
| This uses PEP-487 [1]_ to set the ``set_{method}_request`` methods. It
| looks for the information available in the set default values which are
| set using ``__metadata_request__*`` class attributes, or inferred
| from method signatures.
|
| The ``__metadata_request__*`` class attributes are used when a method
| does not explicitly accept a metadata through its arguments or if the
| developer would like to specify a request value for those metadata
| which are different from the default ``None``.
|
| References
| ----------
| .. [1] https://www.python.org/dev/peps/pep-0487
xgboost.build_info
Python Library Documentation: function build_info in module xgboost.core
build_info() -> dict
Build information of XGBoost. The returned value format is not stable. Also,
please note that build time dependency is not the same as runtime dependency. For
instance, it's possible to build XGBoost with older CUDA version but run it with the
lastest one.
.. versionadded:: 1.6.0
xgboost.callback
Python Library Documentation: module xgboost.callback in xgboost
NAME
xgboost.callback
DESCRIPTION
Callback library containing training routines. See :doc:`Callback Functions
</python/callbacks>` for a quick introduction.
CLASSES
abc.ABC(builtins.object)
TrainingCallback
EarlyStopping
EvaluationMonitor
LearningRateScheduler
TrainingCheckPoint
builtins.object
CallbackContainer
class CallbackContainer(builtins.object)
| CallbackContainer(callbacks: Sequence[xgboost.callback.TrainingCallback], metric: Optional[Callable] = None, output_margin: bool = True, is_cv: bool = False) -> None
|
| A special internal callback for invoking a list of other callbacks.
|
| .. versionadded:: 1.3.0
|
| Methods defined here:
|
| __init__(self, callbacks: Sequence[xgboost.callback.TrainingCallback], metric: Optional[Callable] = None, output_margin: bool = True, is_cv: bool = False) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| after_iteration(self, model: Any, epoch: int, dtrain: xgboost.core.DMatrix, evals: Optional[List[Tuple[xgboost.core.DMatrix, str]]]) -> bool
| Function called after training iteration.
|
| after_training(self, model: Any) -> Any
| Function called after training.
|
| before_iteration(self, model: Any, epoch: int, dtrain: xgboost.core.DMatrix, evals: Optional[List[Tuple[xgboost.core.DMatrix, str]]]) -> bool
| Function called before training iteration.
|
| before_training(self, model: Any) -> Any
| Function called before training.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
class EarlyStopping(TrainingCallback)
| EarlyStopping(*, rounds: int, metric_name: Optional[str] = None, data_name: Optional[str] = None, maximize: Optional[bool] = None, save_best: Optional[bool] = False, min_delta: float = 0.0) -> None
|
| Callback function for early stopping
|
| .. versionadded:: 1.3.0
|
| Parameters
| ----------
| rounds :
| Early stopping rounds.
| metric_name :
| Name of metric that is used for early stopping.
| data_name :
| Name of dataset that is used for early stopping.
| maximize :
| Whether to maximize evaluation metric. None means auto (discouraged).
| save_best :
| Whether training should return the best model or the last model. If set to
| `True`, it will only keep the boosting rounds up to the detected best iteration,
| discarding the ones that come after. This is only supported with tree methods
| (not `gblinear`). Also, the `cv` function doesn't return a model, the parameter
| is not applicable.
| min_delta :
|
| .. versionadded:: 1.5.0
|
| Minimum absolute change in score to be qualified as an improvement.
|
| Examples
| --------
|
| .. code-block:: python
|
| es = xgboost.callback.EarlyStopping(
| rounds=2,
| min_delta=1e-3,
| save_best=True,
| maximize=False,
| data_name="validation_0",
| metric_name="mlogloss",
| )
| clf = xgboost.XGBClassifier(tree_method="hist", device="cuda", callbacks=[es])
|
| X, y = load_digits(return_X_y=True)
| clf.fit(X, y, eval_set=[(X, y)])
|
| Method resolution order:
| EarlyStopping
| TrainingCallback
| abc.ABC
| builtins.object
|
| Methods defined here:
|
| __init__(self, *, rounds: int, metric_name: Optional[str] = None, data_name: Optional[str] = None, maximize: Optional[bool] = None, save_best: Optional[bool] = False, min_delta: float = 0.0) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| after_iteration(self, model: Any, epoch: int, evals_log: Dict[str, Dict[str, Union[List[float], List[Tuple[float, float]]]]]) -> bool
| Run after each iteration. Returns `True` when training should stop.
|
| Parameters
| ----------
|
| model :
| Eeither a :py:class:`~xgboost.Booster` object or a CVPack if the cv function
| in xgboost is being used.
| epoch :
| The current training iteration.
| evals_log :
| A dictionary containing the evaluation history:
|
| .. code-block:: python
|
| {"data_name": {"metric_name": [0.5, ...]}}
|
| after_training(self, model: Any) -> Any
| Run after training is finished.
|
| before_training(self, model: Any) -> Any
| Run before training starts.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset()
|
| __annotations__ = {}
|
| ----------------------------------------------------------------------
| Methods inherited from TrainingCallback:
|
| before_iteration(self, model: Any, epoch: int, evals_log: Dict[str, Dict[str, Union[List[float], List[Tuple[float, float]]]]]) -> bool
| Run before each iteration. Returns True when training should stop. See
| :py:meth:`after_iteration` for details.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from TrainingCallback:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Data and other attributes inherited from TrainingCallback:
|
| EvalsLog = typing.Dict[str, typing.Dict[str, typing.Union[t...loat], t...
class EvaluationMonitor(TrainingCallback)
| EvaluationMonitor(rank: int = 0, period: int = 1, show_stdv: bool = False, logger: Callable[[str], NoneType] = <function communicator_print at 0x000001406EFE39C0>)
|
| Print the evaluation result at each iteration.
|
| .. versionadded:: 1.3.0
|
| Parameters
| ----------
|
| rank :
| Which worker should be used for printing the result.
| period :
| How many epoches between printing.
| show_stdv :
| Used in cv to show standard deviation. Users should not specify it.
| logger :
| A callable used for logging evaluation result.
|
| Method resolution order:
| EvaluationMonitor
| TrainingCallback
| abc.ABC
| builtins.object
|
| Methods defined here:
|
| __init__(self, rank: int = 0, period: int = 1, show_stdv: bool = False, logger: Callable[[str], NoneType] = <function communicator_print at 0x000001406EFE39C0>)
| Initialize self. See help(type(self)) for accurate signature.
|
| after_iteration(self, model: Any, epoch: int, evals_log: Dict[str, Dict[str, Union[List[float], List[Tuple[float, float]]]]]) -> bool
| Run after each iteration. Returns `True` when training should stop.
|
| Parameters
| ----------
|
| model :
| Eeither a :py:class:`~xgboost.Booster` object or a CVPack if the cv function
| in xgboost is being used.
| epoch :
| The current training iteration.
| evals_log :
| A dictionary containing the evaluation history:
|
| .. code-block:: python
|
| {"data_name": {"metric_name": [0.5, ...]}}
|
| after_training(self, model: Any) -> Any
| Run after training is finished.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset()
|
| __annotations__ = {}
|
| ----------------------------------------------------------------------
| Methods inherited from TrainingCallback:
|
| before_iteration(self, model: Any, epoch: int, evals_log: Dict[str, Dict[str, Union[List[float], List[Tuple[float, float]]]]]) -> bool
| Run before each iteration. Returns True when training should stop. See
| :py:meth:`after_iteration` for details.
|
| before_training(self, model: Any) -> Any
| Run before training starts.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from TrainingCallback:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Data and other attributes inherited from TrainingCallback:
|
| EvalsLog = typing.Dict[str, typing.Dict[str, typing.Union[t...loat], t...
class LearningRateScheduler(TrainingCallback)
| LearningRateScheduler(learning_rates: Union[Callable[[int], float], Sequence[float]]) -> None
|
| Callback function for scheduling learning rate.
|
| .. versionadded:: 1.3.0
|
| Parameters
| ----------
|
| learning_rates :
| If it's a callable object, then it should accept an integer parameter
| `epoch` and returns the corresponding learning rate. Otherwise it
| should be a sequence like list or tuple with the same size of boosting
| rounds.
|
| Method resolution order:
| LearningRateScheduler
| TrainingCallback
| abc.ABC
| builtins.object
|
| Methods defined here:
|
| __init__(self, learning_rates: Union[Callable[[int], float], Sequence[float]]) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| after_iteration(self, model: Any, epoch: int, evals_log: Dict[str, Dict[str, Union[List[float], List[Tuple[float, float]]]]]) -> bool
| Run after each iteration. Returns `True` when training should stop.
|
| Parameters
| ----------
|
| model :
| Eeither a :py:class:`~xgboost.Booster` object or a CVPack if the cv function
| in xgboost is being used.
| epoch :
| The current training iteration.
| evals_log :
| A dictionary containing the evaluation history:
|
| .. code-block:: python
|
| {"data_name": {"metric_name": [0.5, ...]}}
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset()
|
| __annotations__ = {}
|
| ----------------------------------------------------------------------
| Methods inherited from TrainingCallback:
|
| after_training(self, model: Any) -> Any
| Run after training is finished.
|
| before_iteration(self, model: Any, epoch: int, evals_log: Dict[str, Dict[str, Union[List[float], List[Tuple[float, float]]]]]) -> bool
| Run before each iteration. Returns True when training should stop. See
| :py:meth:`after_iteration` for details.
|
| before_training(self, model: Any) -> Any
| Run before training starts.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from TrainingCallback:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Data and other attributes inherited from TrainingCallback:
|
| EvalsLog = typing.Dict[str, typing.Dict[str, typing.Union[t...loat], t...
class TrainingCallback(abc.ABC)
| TrainingCallback() -> None
|
| Interface for training callback.
|
| .. versionadded:: 1.3.0
|
| Method resolution order:
| TrainingCallback
| abc.ABC
| builtins.object
|
| Methods defined here:
|
| __init__(self) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| after_iteration(self, model: Any, epoch: int, evals_log: Dict[str, Dict[str, Union[List[float], List[Tuple[float, float]]]]]) -> bool
| Run after each iteration. Returns `True` when training should stop.
|
| Parameters
| ----------
|
| model :
| Eeither a :py:class:`~xgboost.Booster` object or a CVPack if the cv function
| in xgboost is being used.
| epoch :
| The current training iteration.
| evals_log :
| A dictionary containing the evaluation history:
|
| .. code-block:: python
|
| {"data_name": {"metric_name": [0.5, ...]}}
|
| after_training(self, model: Any) -> Any
| Run after training is finished.
|
| before_iteration(self, model: Any, epoch: int, evals_log: Dict[str, Dict[str, Union[List[float], List[Tuple[float, float]]]]]) -> bool
| Run before each iteration. Returns True when training should stop. See
| :py:meth:`after_iteration` for details.
|
| before_training(self, model: Any) -> Any
| Run before training starts.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| EvalsLog = typing.Dict[str, typing.Dict[str, typing.Union[t...loat], t...
|
| __abstractmethods__ = frozenset()
|
| __annotations__ = {'EvalsLog': typing.TypeAlias}
class TrainingCheckPoint(TrainingCallback)
| TrainingCheckPoint(directory: Union[str, os.PathLike], name: str = 'model', as_pickle: bool = False, interval: int = 100) -> None
|
| Checkpointing operation. Users are encouraged to create their own callbacks for
| checkpoint as XGBoost doesn't handle distributed file systems. When checkpointing on
| distributed systems, be sure to know the rank of the worker to avoid multiple
| workers checkpointing to the same place.
|
| .. versionadded:: 1.3.0
|
| Since XGBoost 2.1.0, the default format is changed to UBJSON.
|
| Parameters
| ----------
|
| directory :
| Output model directory.
| name :
| pattern of output model file. Models will be saved as name_0.ubj, name_1.ubj,
| name_2.ubj ....
| as_pickle :
| When set to True, all training parameters will be saved in pickle format,
| instead of saving only the model.
| interval :
| Interval of checkpointing. Checkpointing is slow so setting a larger number can
| reduce performance hit.
|
| Method resolution order:
| TrainingCheckPoint
| TrainingCallback
| abc.ABC
| builtins.object
|
| Methods defined here:
|
| __init__(self, directory: Union[str, os.PathLike], name: str = 'model', as_pickle: bool = False, interval: int = 100) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| after_iteration(self, model: Any, epoch: int, evals_log: Dict[str, Dict[str, Union[List[float], List[Tuple[float, float]]]]]) -> bool
| Run after each iteration. Returns `True` when training should stop.
|
| Parameters
| ----------
|
| model :
| Eeither a :py:class:`~xgboost.Booster` object or a CVPack if the cv function
| in xgboost is being used.
| epoch :
| The current training iteration.
| evals_log :
| A dictionary containing the evaluation history:
|
| .. code-block:: python
|
| {"data_name": {"metric_name": [0.5, ...]}}
|
| before_training(self, model: Any) -> Any
| Run before training starts.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset()
|
| __annotations__ = {}
|
| default_format = 'ubj'
|
| ----------------------------------------------------------------------
| Methods inherited from TrainingCallback:
|
| after_training(self, model: Any) -> Any
| Run after training is finished.
|
| before_iteration(self, model: Any, epoch: int, evals_log: Dict[str, Dict[str, Union[List[float], List[Tuple[float, float]]]]]) -> bool
| Run before each iteration. Returns True when training should stop. See
| :py:meth:`after_iteration` for details.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from TrainingCallback:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Data and other attributes inherited from TrainingCallback:
|
| EvalsLog = typing.Dict[str, typing.Dict[str, typing.Union[t...loat], t...
DATA
__all__ = ['TrainingCallback', 'LearningRateScheduler', 'EarlyStopping...
FILE
c:\users\youse\appdata\local\programs\python\python312\lib\site-packages\xgboost\callback.py
xgboost.collective
Python Library Documentation: module xgboost.collective in xgboost
NAME
xgboost.collective - XGBoost collective communication related API.
CLASSES
builtins.object
CommunicatorContext
Config
enum.IntEnum(builtins.int, enum.ReprEnum)
Op
class CommunicatorContext(builtins.object)
| CommunicatorContext(**args: Union[int, str, NoneType]) -> None
|
| A context controlling collective communicator initialization and finalization.
|
| Methods defined here:
|
| __enter__(self) -> Dict[str, Union[int, str, NoneType]]
|
| __exit__(self, *args: Any) -> None
|
| __init__(self, **args: Union[int, str, NoneType]) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
class Config(builtins.object)
| Config(retry: Optional[int] = None, timeout: Optional[int] = None, tracker_host_ip: Optional[str] = None, tracker_port: Optional[int] = None, tracker_timeout: Optional[int] = None, worker_port: Union[Callable[[], int], int, NoneType] = None) -> None
|
| User configuration for the communicator context. This is used for easier
| integration with distributed frameworks. Users of the collective module can pass the
| parameters directly into tracker and the communicator.
|
| .. versionadded:: 3.0
|
| Attributes
| ----------
| retry : See `dmlc_retry` in :py:meth:`init`.
|
| timeout :
| See `dmlc_timeout` in :py:meth:`init`. This is only used for communicators, not
| the tracker. They are different parameters since the timeout for tracker limits
| only the time for starting and finalizing the communication group, whereas the
| timeout for communicators limits the time used for collective operations, like
| :py:meth:`allreduce`.
|
| tracker_host_ip : See :py:class:`~xgboost.tracker.RabitTracker`.
|
| tracker_port : See :py:class:`~xgboost.tracker.RabitTracker`.
|
| tracker_timeout : See :py:class:`~xgboost.tracker.RabitTracker`.
|
| worker_port :
|
| The port each worker listens to for peer-to-peer connections. By default,
| workers use an available port assigned by the OS. This option can be used in
| restricted network environments where only specific ports are open.
|
| This can be an integer for a fixed port used by all workers, or a callback
| function that takes no arguments and returns a port number. The callback is
| invoked per-worker at the worker side.
|
| .. note::
|
| The option does not affect the NCCL communicator group, which must be
| configured via NCCL's own environment variables.
|
| Methods defined here:
|
| __eq__(self, other)
| Return self==value.
|
| __init__(self, retry: Optional[int] = None, timeout: Optional[int] = None, tracker_host_ip: Optional[str] = None, tracker_port: Optional[int] = None, tracker_timeout: Optional[int] = None, worker_port: Union[Callable[[], int], int, NoneType] = None) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| __repr__(self)
| Return repr(self).
|
| get_comm_config(self, args: Dict[str, Union[int, str]]) -> Dict[str, Union[int, str]]
| Update the arguments for the communicator.
|
| update_worker_args(self, args: Dict[str, Union[int, str]]) -> Dict[str, Union[int, str]]
| Worker side arguments resolution.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __annotations__ = {'retry': typing.Optional[int], 'timeout': typing.Op...
|
| __dataclass_fields__ = {'retry': Field(name='retry',type=typing.Option...
|
| __dataclass_params__ = _DataclassParams(init=True,repr=True,eq=True,or...
|
| __hash__ = None
|
| __match_args__ = ('retry', 'timeout', 'tracker_host_ip', 'tracker_port...
|
| retry = None
|
| timeout = None
|
| tracker_host_ip = None
|
| tracker_port = None
|
| tracker_timeout = None
|
| worker_port = None
class Op(enum.IntEnum)
| Op(*values)
|
| Supported operations for allreduce.
|
| Method resolution order:
| Op
| enum.IntEnum
| builtins.int
| enum.ReprEnum
| enum.Enum
| builtins.object
|
| Methods defined here:
|
| __format__(self, format_spec, /) from builtins.int
| Convert to a string according to format_spec.
|
| __new__(cls, value) from enum.Enum
| Create and return a new object. See help(type) for accurate signature.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| BITWISE_AND = <Op.BITWISE_AND: 3>
|
| BITWISE_OR = <Op.BITWISE_OR: 4>
|
| BITWISE_XOR = <Op.BITWISE_XOR: 5>
|
| MAX = <Op.MAX: 0>
|
| MIN = <Op.MIN: 1>
|
| SUM = <Op.SUM: 2>
|
| ----------------------------------------------------------------------
| Methods inherited from enum.IntEnum:
|
| __repr__(self) from enum.Enum
| Return repr(self).
|
| __str__ = __repr__(self, /) from builtins.int
| Return repr(self).
|
| ----------------------------------------------------------------------
| Methods inherited from builtins.int:
|
| __abs__(self, /)
| abs(self)
|
| __add__(self, value, /)
| Return self+value.
|
| __and__(self, value, /)
| Return self&value.
|
| __bool__(self, /)
| True if self else False
|
| __ceil__(...)
| Ceiling of an Integral returns itself.
|
| __divmod__(self, value, /)
| Return divmod(self, value).
|
| __eq__(self, value, /)
| Return self==value.
|
| __float__(self, /)
| float(self)
|
| __floor__(...)
| Flooring an Integral returns itself.
|
| __floordiv__(self, value, /)
| Return self//value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getnewargs__(self, /)
|
| __gt__(self, value, /)
| Return self>value.
|
| __hash__(self, /)
| Return hash(self).
|
| __index__(self, /)
| Return self converted to an integer, if self is suitable for use as an index into a list.
|
| __int__(self, /)
| int(self)
|
| __invert__(self, /)
| ~self
|
| __le__(self, value, /)
| Return self<=value.
|
| __lshift__(self, value, /)
| Return self<<value.
|
| __lt__(self, value, /)
| Return self<value.
|
| __mod__(self, value, /)
| Return self%value.
|
| __mul__(self, value, /)
| Return self*value.
|
| __ne__(self, value, /)
| Return self!=value.
|
| __neg__(self, /)
| -self
|
| __or__(self, value, /)
| Return self|value.
|
| __pos__(self, /)
| +self
|
| __pow__(self, value, mod=None, /)
| Return pow(self, value, mod).
|
| __radd__(self, value, /)
| Return value+self.
|
| __rand__(self, value, /)
| Return value&self.
|
| __rdivmod__(self, value, /)
| Return divmod(value, self).
|
| __rfloordiv__(self, value, /)
| Return value//self.
|
| __rlshift__(self, value, /)
| Return value<<self.
|
| __rmod__(self, value, /)
| Return value%self.
|
| __rmul__(self, value, /)
| Return value*self.
|
| __ror__(self, value, /)
| Return value|self.
|
| __round__(...)
| Rounding an Integral returns itself.
|
| Rounding with an ndigits argument also returns an integer.
|
| __rpow__(self, value, mod=None, /)
| Return pow(value, self, mod).
|
| __rrshift__(self, value, /)
| Return value>>self.
|
| __rshift__(self, value, /)
| Return self>>value.
|
| __rsub__(self, value, /)
| Return value-self.
|
| __rtruediv__(self, value, /)
| Return value/self.
|
| __rxor__(self, value, /)
| Return value^self.
|
| __sizeof__(self, /)
| Returns size in memory, in bytes.
|
| __sub__(self, value, /)
| Return self-value.
|
| __truediv__(self, value, /)
| Return self/value.
|
| __trunc__(...)
| Truncating an Integral returns itself.
|
| __xor__(self, value, /)
| Return self^value.
|
| as_integer_ratio(self, /)
| Return a pair of integers, whose ratio is equal to the original int.
|
| The ratio is in lowest terms and has a positive denominator.
|
| >>> (10).as_integer_ratio()
| (10, 1)
| >>> (-10).as_integer_ratio()
| (-10, 1)
| >>> (0).as_integer_ratio()
| (0, 1)
|
| bit_count(self, /)
| Number of ones in the binary representation of the absolute value of self.
|
| Also known as the population count.
|
| >>> bin(13)
| '0b1101'
| >>> (13).bit_count()
| 3
|
| bit_length(self, /)
| Number of bits necessary to represent self in binary.
|
| >>> bin(37)
| '0b100101'
| >>> (37).bit_length()
| 6
|
| conjugate(...)
| Returns self, the complex conjugate of any int.
|
| is_integer(self, /)
| Returns True. Exists for duck type compatibility with float.is_integer.
|
| to_bytes(self, /, length=1, byteorder='big', *, signed=False)
| Return an array of bytes representing an integer.
|
| length
| Length of bytes object to use. An OverflowError is raised if the
| integer is not representable with the given number of bytes. Default
| is length 1.
| byteorder
| The byte order used to represent the integer. If byteorder is 'big',
| the most significant byte is at the beginning of the byte array. If
| byteorder is 'little', the most significant byte is at the end of the
| byte array. To request the native byte order of the host system, use
| `sys.byteorder' as the byte order value. Default is to use 'big'.
| signed
| Determines whether two's complement is used to represent the integer.
| If signed is False and a negative integer is given, an OverflowError
| is raised.
|
| ----------------------------------------------------------------------
| Class methods inherited from builtins.int:
|
| from_bytes(bytes, byteorder='big', *, signed=False)
| Return the integer represented by the given array of bytes.
|
| bytes
| Holds the array of bytes to convert. The argument must either
| support the buffer protocol or be an iterable object producing bytes.
| Bytes and bytearray are examples of built-in objects that support the
| buffer protocol.
| byteorder
| The byte order used to represent the integer. If byteorder is 'big',
| the most significant byte is at the beginning of the byte array. If
| byteorder is 'little', the most significant byte is at the end of the
| byte array. To request the native byte order of the host system, use
| `sys.byteorder' as the byte order value. Default is to use 'big'.
| signed
| Indicates whether two's complement is used to represent the integer.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from builtins.int:
|
| denominator
| the denominator of a rational number in lowest terms
|
| imag
| the imaginary part of a complex number
|
| numerator
| the numerator of a rational number in lowest terms
|
| real
| the real part of a complex number
|
| ----------------------------------------------------------------------
| Methods inherited from enum.Enum:
|
| __dir__(self)
| Returns public methods and other interesting attributes.
|
| __init__(self, *args, **kwds)
| Initialize self. See help(type(self)) for accurate signature.
|
| __reduce_ex__(self, proto)
| Helper for pickle.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from enum.Enum:
|
| name
| The name of the Enum member.
|
| value
| The value of the Enum member.
|
| ----------------------------------------------------------------------
| Static methods inherited from enum.EnumType:
|
| __contains__(value)
| Return True if `value` is in `cls`.
|
| `value` is in `cls` if:
| 1) `value` is a member of `cls`, or
| 2) `value` is the value of one of the `cls`'s members.
|
| __getitem__(name)
| Return the member matching `name`.
|
| __iter__()
| Return members in definition order.
|
| __len__()
| Return the number of members (no aliases)
|
| ----------------------------------------------------------------------
| Readonly properties inherited from enum.EnumType:
|
| __members__
| Returns a mapping of member name->value.
|
| This mapping lists all enum members, including aliases. Note that this
| is a read-only view of the internal mapping.
FUNCTIONS
allreduce(data: numpy.ndarray, op: xgboost.collective.Op) -> numpy.ndarray
Perform allreduce, return the result.
Parameters
----------
data :
Input data.
op :
Reduction operator.
Returns
-------
result :
The result of allreduce, have same shape as data
Notes
-----
This function is not thread-safe.
broadcast(data: ~_T, root: int) -> ~_T
Broadcast object from one node to all other nodes.
Parameters
----------
data : any type that can be pickled
Input data, if current rank does not equal root, this can be None
root : int
Rank of the node to broadcast data from.
Returns
-------
object : int
the result of broadcast.
communicator_print(msg: Any) -> None
Print message to the communicator.
This function can be used to communicate the information of
the progress to the communicator.
Parameters
----------
msg : str
The message to be printed to the communicator.
finalize() -> None
Finalize the communicator.
get_processor_name() -> str
Get the processor name.
Returns
-------
name :
The name of processor(host)
get_rank() -> int
Get rank of current process.
Returns
-------
rank : int
Rank of current process.
get_world_size() -> int
Get total number workers.
Returns
-------
n :
Total number of process.
init(**args: Union[int, str, NoneType]) -> None
Initialize the collective library with arguments.
Parameters
----------
args :
Keyword arguments representing the parameters and their values.
Accepted parameters:
- dmlc_communicator: The type of the communicator.
* rabit: Use Rabit. This is the default if the type is unspecified.
* federated: Use the gRPC interface for Federated Learning.
Only applicable to the Rabit communicator:
- dmlc_tracker_uri: Hostname of the tracker.
- dmlc_tracker_port: Port number of the tracker.
- dmlc_task_id: ID of the current task, can be used to obtain deterministic
- dmlc_retry: The number of retry when handling network errors.
- dmlc_timeout: Timeout in seconds.
- dmlc_nccl_path: Path to load (dlopen) nccl for GPU-based communication.
Only applicable to the Federated communicator:
- federated_server_address: Address of the federated server.
- federated_world_size: Number of federated workers.
- federated_rank: Rank of the current worker.
- federated_server_cert: Server certificate file path. Only needed for the SSL
mode.
- federated_client_key: Client key file path. Only needed for the SSL mode.
- federated_client_cert: Client certificate file path. Only needed for the SSL
mode.
Use upper case for environment variables, use lower case for runtime
configuration.
is_distributed() -> bool
If the collective communicator is distributed.
signal_error() -> None
Kill the process.
DATA
Callable = typing.Callable
Deprecated alias to collections.abc.Callable.
Callable[[int], str] signifies a function that takes a single
parameter of type int and returns a str.
The subscription syntax must always be used with exactly two
values: the argument list and the return type.
The argument list must be a list of types, a ParamSpec,
Concatenate or ellipsis. The return type must be a single type.
There is no syntax to indicate optional or keyword arguments;
such function types are rarely used as callback types.
Dict = typing.Dict
A generic version of dict.
LOGGER = <Logger [xgboost.collective] (WARNING)>
Optional = typing.Optional
Optional[X] is equivalent to Union[X, None].
TypeAlias = typing.TypeAlias
Special form for marking type aliases.
Use TypeAlias to indicate that an assignment should
be recognized as a proper type alias definition by type
checkers.
For example::
Predicate: TypeAlias = Callable[..., bool]
It's invalid when used anywhere except as in the example above.
Union = typing.Union
Union type; Union[X, Y] means either X or Y.
On Python 3.10 and higher, the | operator
can also be used to denote unions;
X | Y means the same thing to the type checker as Union[X, Y].
To define a union, use e.g. Union[int, str]. Details:
- The arguments must be types and there must be at least one.
- None as an argument is a special case and is replaced by
type(None).
- Unions of unions are flattened, e.g.::
assert Union[Union[int, str], float] == Union[int, str, float]
- Unions of a single argument vanish, e.g.::
assert Union[int] == int # The constructor actually returns int
- Redundant arguments are skipped, e.g.::
assert Union[int, str, int] == Union[int, str]
- When comparing unions, the argument order is ignored, e.g.::
assert Union[int, str] == Union[str, int]
- You cannot subclass or instantiate a union.
- You can use Optional[X] as a shorthand for Union[X, None].
__annotations__ = {'_ArgVals': typing.TypeAlias, '_Args': typing.TypeA...
FILE
c:\users\youse\appdata\local\programs\python\python312\lib\site-packages\xgboost\collective.py
xgboost.compat
Python Library Documentation: module xgboost.compat in xgboost
NAME
xgboost.compat - For compatibility and optional dependencies.
FUNCTIONS
concat(value: Sequence[~_T]) -> ~_T
Concatenate row-wise.
import_cupy() -> module
Import cupy.
import_pandas() -> module
Import pandas with memory cache.
import_polars() -> module
Import polars with memory cache.
import_pyarrow() -> module
Import pyarrow with memory cache.
is_cudf_available() -> bool
Check cuDF package available or not
is_cupy_available() -> bool
Check cupy package available or not
is_dataframe(data: Any) -> bool
Whether the input is a dataframe. Currently supported dataframes:
- pandas
- cudf
- cudf.pandas
- polars
- pyarrow
- modin
is_pandas_available() -> bool
Check the pandas package is available or not.
is_pyarrow_available() -> bool
Check pyarrow package available or not
lazy_isinstance(instance: Any, module: str, name: str) -> bool
Use string representation to identify a type.
py_str(x: bytes | None) -> str
convert c string back to python string
DATA
SKLEARN_INSTALLED = True
Sequence = typing.Sequence
A generic version of collections.abc.Sequence.
TYPE_CHECKING = False
TypeGuard = typing.TypeGuard
Special typing construct for marking user-defined type guard functions.
``TypeGuard`` can be used to annotate the return type of a user-defined
type guard function. ``TypeGuard`` only accepts a single type argument.
At runtime, functions marked this way should return a boolean.
``TypeGuard`` aims to benefit *type narrowing* -- a technique used by static
type checkers to determine a more precise type of an expression within a
program's code flow. Usually type narrowing is done by analyzing
conditional code flow and applying the narrowing to a block of code. The
conditional expression here is sometimes referred to as a "type guard".
Sometimes it would be convenient to use a user-defined boolean function
as a type guard. Such a function should use ``TypeGuard[...]`` as its
return type to alert static type checkers to this intention.
Using ``-> TypeGuard`` tells the static type checker that for a given
function:
1. The return value is a boolean.
2. If the return value is ``True``, the type of its argument
is the type inside ``TypeGuard``.
For example::
def is_str_list(val: list[object]) -> TypeGuard[list[str]]:
'''Determines whether all objects in the list are strings'''
return all(isinstance(x, str) for x in val)
def func1(val: list[object]):
if is_str_list(val):
# Type of ``val`` is narrowed to ``list[str]``.
print(" ".join(val))
else:
# Type of ``val`` remains as ``list[object]``.
print("Not a list of strings!")
Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower
form of ``TypeA`` (it can even be a wider form) and this may lead to
type-unsafe results. The main reason is to allow for things like
narrowing ``list[object]`` to ``list[str]`` even though the latter is not
a subtype of the former, since ``list`` is invariant. The responsibility of
writing type-safe type guards is left to the user.
``TypeGuard`` also works with type variables. For more information, see
PEP 647 (User-Defined Type Guards).
FILE
c:\users\youse\appdata\local\programs\python\python312\lib\site-packages\xgboost\compat.py
xgboost.config
Python Library Documentation: module xgboost.config in xgboost
NAME
xgboost.config - Global configuration for XGBoost
FUNCTIONS
config_context(**new_config: Any) -> Iterator[NoneType]
Context manager for global XGBoost configuration.
Global configuration consists of a collection of parameters that can be applied in the
global scope. See :ref:`global_config` for the full list of parameters supported in
the global configuration.
.. note::
All settings, not just those presently modified, will be returned to their
previous values when the context manager is exited. This is not thread-safe.
.. versionadded:: 1.4.0
Parameters
----------
new_config: Dict[str, Any]
Keyword arguments representing the parameters and their values
Example
-------
.. code-block:: python
import xgboost as xgb
# Show all messages, including ones pertaining to debugging
xgb.set_config(verbosity=2)
# Get current value of global configuration
# This is a dict containing all parameters in the global configuration,
# including 'verbosity'
config = xgb.get_config()
assert config['verbosity'] == 2
# Example of using the context manager xgb.config_context().
# The context manager will restore the previous value of the global
# configuration upon exiting.
with xgb.config_context(verbosity=0):
# Suppress warning caused by model generated with XGBoost version < 1.0.0
bst = xgb.Booster(model_file='./old_model.bin')
assert xgb.get_config()['verbosity'] == 2 # old value restored
Nested configuration context is also supported:
Example
-------
.. code-block:: python
with xgb.config_context(verbosity=3):
assert xgb.get_config()["verbosity"] == 3
with xgb.config_context(verbosity=2):
assert xgb.get_config()["verbosity"] == 2
xgb.set_config(verbosity=2)
assert xgb.get_config()["verbosity"] == 2
with xgb.config_context(verbosity=3):
assert xgb.get_config()["verbosity"] == 3
See Also
--------
set_config: Set global XGBoost configuration
get_config: Get current values of the global configuration
config_doc(*, header: Optional[str] = None, extra_note: Optional[str] = None, parameters: Optional[str] = None, returns: Optional[str] = None, see_also: Optional[str] = None) -> Callable[[~_F], ~_F]
Decorator to format docstring for config functions.
Parameters
----------
header: str
An introducion to the function
extra_note: str
Additional notes
parameters: str
Parameters of the function
returns: str
Return value
see_also: str
Related functions
get_config() -> Dict[str, Any]
Get current values of the global configuration.
Global configuration consists of a collection of parameters that can be applied in the
global scope. See :ref:`global_config` for the full list of parameters supported in
the global configuration.
.. versionadded:: 1.4.0
Returns
-------
args: Dict[str, Any]
The list of global parameters and their values
Example
-------
.. code-block:: python
import xgboost as xgb
# Show all messages, including ones pertaining to debugging
xgb.set_config(verbosity=2)
# Get current value of global configuration
# This is a dict containing all parameters in the global configuration,
# including 'verbosity'
config = xgb.get_config()
assert config['verbosity'] == 2
# Example of using the context manager xgb.config_context().
# The context manager will restore the previous value of the global
# configuration upon exiting.
with xgb.config_context(verbosity=0):
# Suppress warning caused by model generated with XGBoost version < 1.0.0
bst = xgb.Booster(model_file='./old_model.bin')
assert xgb.get_config()['verbosity'] == 2 # old value restored
Nested configuration context is also supported:
Example
-------
.. code-block:: python
with xgb.config_context(verbosity=3):
assert xgb.get_config()["verbosity"] == 3
with xgb.config_context(verbosity=2):
assert xgb.get_config()["verbosity"] == 2
xgb.set_config(verbosity=2)
assert xgb.get_config()["verbosity"] == 2
with xgb.config_context(verbosity=3):
assert xgb.get_config()["verbosity"] == 3
set_config(**new_config: Any) -> None
Set global configuration.
Global configuration consists of a collection of parameters that can be applied in the
global scope. See :ref:`global_config` for the full list of parameters supported in
the global configuration.
.. versionadded:: 1.4.0
Parameters
----------
new_config: Dict[str, Any]
Keyword arguments representing the parameters and their values
Example
-------
.. code-block:: python
import xgboost as xgb
# Show all messages, including ones pertaining to debugging
xgb.set_config(verbosity=2)
# Get current value of global configuration
# This is a dict containing all parameters in the global configuration,
# including 'verbosity'
config = xgb.get_config()
assert config['verbosity'] == 2
# Example of using the context manager xgb.config_context().
# The context manager will restore the previous value of the global
# configuration upon exiting.
with xgb.config_context(verbosity=0):
# Suppress warning caused by model generated with XGBoost version < 1.0.0
bst = xgb.Booster(model_file='./old_model.bin')
assert xgb.get_config()['verbosity'] == 2 # old value restored
Nested configuration context is also supported:
Example
-------
.. code-block:: python
with xgb.config_context(verbosity=3):
assert xgb.get_config()["verbosity"] == 3
with xgb.config_context(verbosity=2):
assert xgb.get_config()["verbosity"] == 2
xgb.set_config(verbosity=2)
assert xgb.get_config()["verbosity"] == 2
with xgb.config_context(verbosity=3):
assert xgb.get_config()["verbosity"] == 3
DATA
Callable = typing.Callable
Deprecated alias to collections.abc.Callable.
Callable[[int], str] signifies a function that takes a single
parameter of type int and returns a str.
The subscription syntax must always be used with exactly two
values: the argument list and the return type.
The argument list must be a list of types, a ParamSpec,
Concatenate or ellipsis. The return type must be a single type.
There is no syntax to indicate optional or keyword arguments;
such function types are rarely used as callback types.
Dict = typing.Dict
A generic version of dict.
Iterator = typing.Iterator
A generic version of collections.abc.Iterator.
Optional = typing.Optional
Optional[X] is equivalent to Union[X, None].
FILE
c:\users\youse\appdata\local\programs\python\python312\lib\site-packages\xgboost\config.py
xgboost.config_context
Python Library Documentation: function config_context in module xgboost.config
config_context(**new_config: Any) -> Iterator[NoneType]
Context manager for global XGBoost configuration.
Global configuration consists of a collection of parameters that can be applied in the
global scope. See :ref:`global_config` for the full list of parameters supported in
the global configuration.
.. note::
All settings, not just those presently modified, will be returned to their
previous values when the context manager is exited. This is not thread-safe.
.. versionadded:: 1.4.0
Parameters
----------
new_config: Dict[str, Any]
Keyword arguments representing the parameters and their values
Example
-------
.. code-block:: python
import xgboost as xgb
# Show all messages, including ones pertaining to debugging
xgb.set_config(verbosity=2)
# Get current value of global configuration
# This is a dict containing all parameters in the global configuration,
# including 'verbosity'
config = xgb.get_config()
assert config['verbosity'] == 2
# Example of using the context manager xgb.config_context().
# The context manager will restore the previous value of the global
# configuration upon exiting.
with xgb.config_context(verbosity=0):
# Suppress warning caused by model generated with XGBoost version < 1.0.0
bst = xgb.Booster(model_file='./old_model.bin')
assert xgb.get_config()['verbosity'] == 2 # old value restored
Nested configuration context is also supported:
Example
-------
.. code-block:: python
with xgb.config_context(verbosity=3):
assert xgb.get_config()["verbosity"] == 3
with xgb.config_context(verbosity=2):
assert xgb.get_config()["verbosity"] == 2
xgb.set_config(verbosity=2)
assert xgb.get_config()["verbosity"] == 2
with xgb.config_context(verbosity=3):
assert xgb.get_config()["verbosity"] == 3
See Also
--------
set_config: Set global XGBoost configuration
get_config: Get current values of the global configuration
xgboost.core
Python Library Documentation: module xgboost.core in xgboost
NAME
xgboost.core - Core XGBoost Library.
CLASSES
abc.ABC(builtins.object)
DataIter
SingleBatchInternalIter
builtins.object
Booster
DMatrix
ExtMemQuantileDMatrix(DMatrix, _RefMixIn)
QuantileDMatrix(DMatrix, _RefMixIn)
class Booster(builtins.object)
| Booster(params: Union[List, Dict[str, Any], NoneType] = None, cache: Optional[Sequence[xgboost.core.DMatrix]] = None, model_file: Union[ForwardRef('Booster'), bytearray, os.PathLike, str, NoneType] = None) -> None
|
| A Booster of XGBoost.
|
| Booster is the model of xgboost, that contains low level routines for
| training, prediction and evaluation.
|
| Methods defined here:
|
| __copy__(self) -> 'Booster'
|
| __deepcopy__(self, _: Any) -> 'Booster'
| Return a copy of booster.
|
| __del__(self) -> None
|
| __getitem__(self, val: Union[int, numpy.integer, tuple, slice, ellipsis]) -> 'Booster'
| Get a slice of the tree-based model. Attributes like `best_iteration` and
| `best_score` are removed in the resulting booster.
|
| .. versionadded:: 1.3.0
|
| __getstate__(self) -> Dict
| Helper for pickle.
|
| __init__(self, params: Union[List, Dict[str, Any], NoneType] = None, cache: Optional[Sequence[xgboost.core.DMatrix]] = None, model_file: Union[ForwardRef('Booster'), bytearray, os.PathLike, str, NoneType] = None) -> None
| Parameters
| ----------
| params :
| Parameters for boosters.
| cache :
| List of cache items.
| model_file :
| Path to the model file if it's string or PathLike.
|
| __iter__(self) -> Generator[ForwardRef('Booster'), NoneType, NoneType]
| Iterator method for getting individual trees.
|
| .. versionadded:: 2.0.0
|
| __setstate__(self, state: Dict) -> None
|
| attr(self, key: str) -> Optional[str]
| Get attribute string from the Booster.
|
| Parameters
| ----------
| key :
| The key to get attribute from.
|
| Returns
| -------
| value :
| The attribute value of the key, returns None if attribute do not exist.
|
| attributes(self) -> Dict[str, Optional[str]]
| Get attributes stored in the Booster as a dictionary.
|
| Returns
| -------
| result : dictionary of attribute_name: attribute_value pairs of strings.
| Returns an empty dict if there's no attributes.
|
| boost(self, dtrain: xgboost.core.DMatrix, iteration: int, *, grad: Union[numpy.ndarray, Any, NoneType] = None, hess: Union[numpy.ndarray, Any, NoneType] = None, fobj: Optional[Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[numpy.ndarray, numpy.ndarray]]] = None) -> None
| Boost the booster for one iteration with customized gradient statistics.
|
| .. warning::
|
| Like :py:meth:`.update`, this function should not be called directly by
| users.
|
| Parameters
| ----------
| dtrain :
| The training DMatrix.
| iteration :
| The current training iteration.
| grad :
| The first order of gradient.
| hess :
| The second order of gradient.
| fobj :
| A custom objective function. If gradient is None, then an objective function
| is required.
|
| copy(self) -> 'Booster'
| Copy the booster object.
|
| Returns
| -------
| booster :
| A copied booster model
|
| dump_model(self, fout: Union[str, os.PathLike], fmap: Union[str, os.PathLike] = '', with_stats: bool = False, dump_format: str = 'text') -> None
| Dump model into a text or JSON file. Unlike :py:meth:`save_model`, the
| output format is primarily used for visualization or interpretation,
| hence it's more human readable but cannot be loaded back to XGBoost.
|
| Parameters
| ----------
| fout :
| Output file name.
| fmap :
| Name of the file containing feature map names.
| with_stats :
| Controls whether the split statistics are output.
| dump_format :
| Format of model dump file. Can be 'text' or 'json'.
|
| eval(self, data: xgboost.core.DMatrix, name: str = 'eval', iteration: int = 0) -> str
| Evaluate the model on mat.
|
| Parameters
| ----------
| data :
| The dmatrix storing the input.
|
| name :
| The name of the dataset.
|
| iteration :
| The current iteration number.
|
| Returns
| -------
| result: str
| Evaluation result string.
|
| eval_set(self, evals: Sequence[Tuple[xgboost.core.DMatrix, str]], iteration: int = 0, feval: Optional[Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[str, float]]] = None, output_margin: bool = True) -> str
| Evaluate a set of data.
|
| Parameters
| ----------
| evals :
| List of items to be evaluated.
| iteration :
| Current iteration.
| feval :
| Custom evaluation function.
|
| Returns
| -------
| result: str
| Evaluation result string.
|
| get_categories(self, export_to_arrow: bool = False) -> xgboost._data_utils.Categories
| Same method as :py:meth:`DMatrix.get_categories`.
|
| get_dump(self, fmap: Union[str, os.PathLike] = '', with_stats: bool = False, dump_format: str = 'text') -> List[str]
| Returns the model dump as a list of strings. Unlike :py:meth:`save_model`,
| the output format is primarily used for visualization or interpretation, hence
| it's more human readable but cannot be loaded back to XGBoost.
|
| Parameters
| ----------
| fmap :
| Name of the file containing feature map names.
| with_stats :
| Controls whether the split statistics should be included.
| dump_format :
| Format of model dump. Can be 'text', 'json' or 'dot'.
|
| get_fscore(self, fmap: Union[str, os.PathLike] = '') -> Dict[str, Union[float, List[float]]]
| Get feature importance of each feature.
|
| .. note:: Zero-importance features will not be included
|
| Keep in mind that this function does not include zero-importance feature,
| i.e. those features that have not been used in any split conditions.
|
| Parameters
| ----------
| fmap :
| The name of feature map file
|
| get_score(self, fmap: Union[str, os.PathLike] = '', importance_type: str = 'weight') -> Dict[str, Union[float, List[float]]]
| Get feature importance of each feature.
| For tree model Importance type can be defined as:
|
| * 'weight': the number of times a feature is used to split the data across all
| trees.
| * 'gain': the average gain across all splits the feature is used in.
| * 'cover': the average coverage across all splits the feature is used in.
| * 'total_gain': the total gain across all splits the feature is used in.
| * 'total_cover': the total coverage across all splits the feature is used in.
|
| .. note::
|
| For linear model, only "weight" is defined and it's the normalized
| coefficients without bias.
|
| .. note:: Zero-importance features will not be included
|
| Keep in mind that this function does not include zero-importance feature,
| i.e. those features that have not been used in any split conditions.
|
| Parameters
| ----------
| fmap :
| The name of feature map file.
| importance_type :
| One of the importance types defined above.
|
| Returns
| -------
| A map between feature names and their scores. When `gblinear` is used for
| multi-class classification the scores for each feature is a list with length
| `n_classes`, otherwise they're scalars.
|
| get_split_value_histogram(self, feature: str, fmap: Union[str, os.PathLike] = '', bins: Optional[int] = None, as_pandas: bool = True) -> Union[numpy.ndarray, ForwardRef('PdDataFrame')]
| Get split value histogram of a feature
|
| Parameters
| ----------
| feature :
| The name of the feature.
| fmap:
| The name of feature map file.
| bin :
| The maximum number of bins.
| Number of bins equals number of unique split values n_unique,
| if bins == None or bins > n_unique.
| as_pandas :
| Return pd.DataFrame when pandas is installed.
| If False or pandas is not installed, return numpy ndarray.
|
| Returns
| -------
| a histogram of used splitting values for the specified feature
| either as numpy array or pandas DataFrame.
|
| inplace_predict(self, data: Any, *, iteration_range: Tuple[Union[int, numpy.integer], Union[int, numpy.integer]] = (0, 0), predict_type: str = 'value', missing: float = nan, validate_features: bool = True, base_margin: Any = None, strict_shape: bool = False) -> Union[numpy.ndarray, Any]
| Run prediction in-place when possible, Unlike :py:meth:`predict` method,
| inplace prediction does not cache the prediction result.
|
| Calling only ``inplace_predict`` in multiple threads is safe and lock
| free. But the safety does not hold when used in conjunction with other
| methods. E.g. you can't train the booster in one thread and perform
| prediction in the other.
|
| .. note::
|
| If the device ordinal of the input data doesn't match the one configured for
| the booster, data will be copied to the booster device.
|
| .. code-block:: python
|
| booster.set_param({"device": "cuda:0"})
| booster.inplace_predict(cupy_array)
|
| booster.set_param({"device": "cpu"})
| booster.inplace_predict(numpy_array)
|
| .. versionadded:: 1.1.0
|
| Parameters
| ----------
| data :
| The input data.
| iteration_range :
| See :py:meth:`predict` for details.
| predict_type :
| * `value` Output model prediction values.
| * `margin` Output the raw untransformed margin value.
| missing :
| See :py:obj:`xgboost.DMatrix` for details.
| validate_features:
| See :py:meth:`xgboost.Booster.predict` for details.
| base_margin:
| See :py:obj:`xgboost.DMatrix` for details.
|
| .. versionadded:: 1.4.0
|
| strict_shape:
| See :py:meth:`xgboost.Booster.predict` for details.
|
| .. versionadded:: 1.4.0
|
| Returns
| -------
| prediction : numpy.ndarray/cupy.ndarray
| The prediction result. When input data is on GPU, prediction result is
| stored in a cupy array.
|
| load_config(self, config: str) -> None
| Load configuration returned by `save_config`.
|
| .. versionadded:: 1.0.0
|
| load_model(self, fname: Union[os.PathLike[~AnyStr], bytearray, str]) -> None
| Load the model from a file or a bytearray.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| model.load_model("model.json")
|
| # or
| model.save_model("model.ubj")
| model.load_model("model.ubj")
|
| # or
| buf = model.save_raw()
| model.load_model(buf)
|
| Parameters
| ----------
| fname :
| Input file name or memory buffer(see also save_raw)
|
| num_boosted_rounds(self) -> int
| Get number of boosted rounds. For gblinear this is reset to 0 after
| serializing the model.
|
| num_features(self) -> int
| Number of features in booster.
|
| predict(self, data: xgboost.core.DMatrix, *, output_margin: bool = False, pred_leaf: bool = False, pred_contribs: bool = False, approx_contribs: bool = False, pred_interactions: bool = False, validate_features: bool = True, training: bool = False, iteration_range: Tuple[Union[int, numpy.integer], Union[int, numpy.integer]] = (0, 0), strict_shape: bool = False) -> numpy.ndarray
| Predict with data. The full model will be used unless `iteration_range` is
| specified, meaning users have to either slice the model or use the
| ``best_iteration`` attribute to get prediction from best model returned from
| early stopping.
|
| .. note::
|
| See :doc:`Prediction </prediction>` for issues like thread safety and a
| summary of outputs from this function.
|
| Parameters
| ----------
| data :
| The dmatrix storing the input.
|
| output_margin :
| Whether to output the raw untransformed margin value.
|
| pred_leaf :
| When this option is on, the output will be a matrix of (nsample,
| ntrees) with each record indicating the predicted leaf index of
| each sample in each tree. Note that the leaf index of a tree is
| unique per tree, so you may find leaf 1 in both tree 1 and tree 0.
|
| pred_contribs :
| When this is True the output will be a matrix of size (nsample,
| nfeats + 1) with each record indicating the feature contributions
| (SHAP values) for that prediction. The sum of all feature
| contributions is equal to the raw untransformed margin value of the
| prediction. Note the final column is the bias term.
|
| approx_contribs :
| Approximate the contributions of each feature. Used when ``pred_contribs``
| or ``pred_interactions`` is set to True. Changing the default of this
| parameter (False) is not recommended.
|
| pred_interactions :
| When this is True the output will be a matrix of size (nsample,
| nfeats + 1, nfeats + 1) indicating the SHAP interaction values for
| each pair of features. The sum of each row (or column) of the
| interaction values equals the corresponding SHAP value (from
| pred_contribs), and the sum of the entire matrix equals the raw
| untransformed margin value of the prediction. Note the last row and
| column correspond to the bias term.
|
| validate_features :
| When this is True, validate that the Booster's and data's
| feature_names are identical. Otherwise, it is assumed that the
| feature_names are the same.
|
| training :
| Whether the prediction value is used for training. This can effect `dart`
| booster, which performs dropouts during training iterations but use all
| trees for inference. If you want to obtain result with dropouts, set this
| parameter to `True`. Also, the parameter is set to true when obtaining
| prediction for custom objective function.
|
| .. versionadded:: 1.0.0
|
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying `iteration_range=(10,
| 20)`, then only the forests built during [10, 20) (half open set) rounds are
| used in this prediction.
|
| .. versionadded:: 1.4.0
|
| strict_shape :
| When set to True, output shape is invariant to whether classification is
| used. For both value and margin prediction, the output shape is (n_samples,
| n_groups), n_groups == 1 when multi-class is not used. Default to False, in
| which case the output shape can be (n_samples, ) if multi-class is not used.
|
| .. versionadded:: 1.4.0
|
| Returns
| -------
| prediction : numpy array
|
| reset(self) -> 'Booster'
| Reset the booster object to release data caches used for training.
|
| .. versionadded:: 3.0.0
|
| save_config(self) -> str
| Output internal parameter configuration of Booster as a JSON
| string.
|
| .. versionadded:: 1.0.0
|
| save_model(self, fname: Union[str, os.PathLike]) -> None
| Save the model to a file.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| # or
| model.save_model("model.ubj")
|
| Parameters
| ----------
| fname :
| Output file name
|
| save_raw(self, raw_format: str = 'ubj') -> bytearray
| Save the model to a in memory buffer representation instead of file.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| Parameters
| ----------
| raw_format :
| Format of output buffer. Can be `json` or `ubj`.
|
| Returns
| -------
| An in memory buffer representation of the model
|
| set_attr(self, **kwargs: Optional[Any]) -> None
| Set the attribute of the Booster.
|
| Parameters
| ----------
| **kwargs
| The attributes to set. Setting a value to None deletes an attribute.
|
| set_param(self, params: Union[Dict, Iterable[Tuple[str, Any]], str], value: Optional[str] = None) -> None
| Set parameters into the Booster.
|
| Parameters
| ----------
| params :
| list of key,value pairs, dict of key to value or simply str key
| value :
| value of the specified parameter, when params is str key
|
| trees_to_dataframe(self, fmap: Union[str, os.PathLike] = '') -> 'PdDataFrame'
| Parse a boosted tree model text dump into a pandas DataFrame structure.
|
| This feature is only defined when the decision tree model is chosen as base
| learner (`booster in {gbtree, dart}`). It is not defined for other base learner
| types, such as linear learners (`booster=gblinear`).
|
| Parameters
| ----------
| fmap :
| The name of feature map file.
|
| update(self, dtrain: xgboost.core.DMatrix, iteration: int, fobj: Optional[Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[numpy.ndarray, numpy.ndarray]]] = None) -> None
| Update for one iteration, with objective function calculated
| internally.
|
| .. warning::
|
| This function should not be called directly by users.
|
| Parameters
| ----------
| dtrain :
| Training data.
| iteration :
| The current training iteration.
| fobj :
| Custom objective function.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| best_iteration
| The best iteration during training.
|
| best_score
| The best evaluation score during training.
|
| feature_names
| Feature names for this booster. Can be directly set by input data or by
| assignment.
|
| feature_types
| Feature types for this booster. Can be directly set by input data or by
| assignment. See :py:class:`DMatrix` for details.
class DMatrix(builtins.object)
| DMatrix(data: Any, label: Optional[Any] = None, *, weight: Optional[Any] = None, base_margin: Optional[Any] = None, missing: Optional[float] = None, silent: bool = False, feature_names: Optional[Sequence[str]] = None, feature_types: Union[Sequence[str], xgboost._data_utils.Categories, NoneType] = None, nthread: Optional[int] = None, group: Optional[Any] = None, qid: Optional[Any] = None, label_lower_bound: Optional[Any] = None, label_upper_bound: Optional[Any] = None, feature_weights: Optional[Any] = None, enable_categorical: bool = True, data_split_mode: xgboost._typing.DataSplitMode = <DataSplitMode.ROW: 0>) -> None
|
| Data Matrix used in XGBoost.
|
| DMatrix is an internal data structure that is used by XGBoost, which is optimized
| for both memory efficiency and training speed. You can construct DMatrix from
| multiple different sources of data.
|
| Methods defined here:
|
| __del__(self) -> None
|
| __init__(self, data: Any, label: Optional[Any] = None, *, weight: Optional[Any] = None, base_margin: Optional[Any] = None, missing: Optional[float] = None, silent: bool = False, feature_names: Optional[Sequence[str]] = None, feature_types: Union[Sequence[str], xgboost._data_utils.Categories, NoneType] = None, nthread: Optional[int] = None, group: Optional[Any] = None, qid: Optional[Any] = None, label_lower_bound: Optional[Any] = None, label_upper_bound: Optional[Any] = None, feature_weights: Optional[Any] = None, enable_categorical: bool = True, data_split_mode: xgboost._typing.DataSplitMode = <DataSplitMode.ROW: 0>) -> None
| Parameters
| ----------
| data :
| Data source of DMatrix. See :ref:`py-data` for a list of supported input
| types.
|
| Note that, if passing an iterator, it **will cache data on disk**, and note
| that fields like ``label`` will be concatenated in-memory from multiple
| calls to the iterator.
| label :
| Label of the training data.
| weight :
| Weight for each instance.
|
| .. note::
|
| For ranking task, weights are per-group. In ranking task, one weight
| is assigned to each group (not each data point). This is because we
| only care about the relative ordering of data points within each group,
| so it doesn't make sense to assign weights to individual data points.
|
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| missing :
| Value in the input data which needs to be present as a missing value. If
| None, defaults to np.nan.
| silent :
| Whether print messages during construction
| feature_names :
| Set names for features.
| feature_types :
|
| Set types for features. If `data` is a DataFrame type and passing
| `enable_categorical=True`, the types will be deduced automatically from the
| column types.
|
| Otherwise, one can pass a list-like input with the same length as number of
| columns in `data`, with the following possible values:
|
| - "c", which represents categorical columns.
| - "q", which represents numeric columns.
| - "int", which represents integer columns.
| - "i", which represents boolean columns.
|
| Note that, while categorical types are treated differently from the rest for
| model fitting purposes, the other types do not influence the generated
| model, but have effects in other functionalities such as feature
| importances.
|
| For categorical features, the input is assumed to be preprocessed and
| encoded by the users. The encoding can be done via
| :py:class:`sklearn.preprocessing.OrdinalEncoder` or pandas dataframe
| `.cat.codes` method. This is useful when users want to specify categorical
| features without having to construct a dataframe as input.
|
| .. versionadded:: 3.1.0
|
| Alternatively, user can pass a :py:class:`~xgboost.core.Categories` object
| returned from previous training as a reference for re-coding. One can obtain
| the reference with the :py:meth:`.get_categories` from the previous training
| DMatrix or the Booster. This feature is experimental.
|
| nthread :
| Number of threads to use for loading data when parallelization is
| applicable. If -1, uses maximum threads available on the system.
| group :
| Group size for all ranking group.
| qid :
| Query ID for data samples, used for ranking.
| label_lower_bound :
| Lower bound for survival training.
| label_upper_bound :
| Upper bound for survival training.
| feature_weights :
| Set feature weights for column sampling.
| enable_categorical :
|
| .. versionadded:: 1.3.0
|
| .. note:: This parameter is experimental
|
| Experimental support of specializing for categorical features. See
| :doc:`/tutorials/categorical` for more info.
|
| If passing `True` and `data` is a data frame (from supported libraries such
| as Pandas, Modin, polars, and cuDF), The DMatrix recognizes categorical
| columns and automatically set the `feature_types` parameter. If `data` is
| not a data frame, this argument is ignored.
|
| If passing `False` and `data` is a data frame with categorical columns, it
| will result in an error.
|
| See notes in the :py:class:`DataIter` for consistency requirement when the
| input is an iterator.
|
| .. versionchanged:: 3.1.0
|
| XGBoost can remember the encoding of categories when the input is a
| dataframe.
|
| data_split_mode(self) -> xgboost._typing.DataSplitMode
| Get the data split mode of the DMatrix.
|
| .. versionadded:: 2.1.0
|
| get_base_margin(self) -> Union[numpy.ndarray, Any]
| Get the base margin of the DMatrix.
|
| get_categories(self, export_to_arrow: bool = False) -> xgboost._data_utils.Categories
| Get the categories in the dataset.
|
| .. versionadded:: 3.1.0
|
| .. warning::
|
| This function is experimental.
|
| Parameters
| ----------
| export_to_arrow :
| The returned container will contain a list of ``pyarrow`` arrays for the
| categories. See the :py:meth:`~Categories.to_arrow` for more info.
|
| get_data(self) -> scipy.sparse._csr.csr_matrix
| Get the predictors from DMatrix as a CSR matrix. This getter is mostly for
| testing purposes. If this is a quantized DMatrix then quantized values are
| returned instead of input values.
|
| .. versionadded:: 1.7.0
|
| get_float_info(self, field: str) -> Union[numpy.ndarray, Any]
| Get float property from the DMatrix.
|
| Parameters
| ----------
| field: str
| The field name of the information.
|
| get_group(self) -> numpy.ndarray
| Get the group of the DMatrix.
|
| Returns
| -------
| group
|
| get_label(self) -> Union[numpy.ndarray, Any]
| Get the label of the DMatrix.
|
| get_quantile_cut(self) -> Tuple[numpy.ndarray, numpy.ndarray]
| Get quantile cuts for quantization.
|
| .. versionadded:: 2.0.0
|
| get_uint_info(self, field: str) -> Union[numpy.ndarray, Any]
| Get unsigned integer property from the DMatrix.
|
| Parameters
| ----------
| field: str
| The field name of the information.
|
| get_weight(self) -> Union[numpy.ndarray, Any]
| Get the weight of the DMatrix.
|
| num_col(self) -> int
| Get the number of columns (features) in the DMatrix.
|
| num_nonmissing(self) -> int
| Get the number of non-missing values in the DMatrix.
|
| .. versionadded:: 1.7.0
|
| num_row(self) -> int
| Get the number of rows in the DMatrix.
|
| save_binary(self, fname: Union[str, os.PathLike], silent: bool = True) -> None
| Save DMatrix to an XGBoost buffer. Saved binary can be later loaded
| by providing the path to :py:func:`xgboost.DMatrix` as input.
|
| Parameters
| ----------
| fname : string or os.PathLike
| Name of the output buffer file.
| silent : bool (optional; default: True)
| If set, the output is suppressed.
|
| set_base_margin(self, margin: Any) -> None
| Set base margin of booster to start from.
|
| This can be used to specify a prediction value of existing model to be
| base_margin However, remember margin is needed, instead of transformed
| prediction e.g. for logistic regression: need to put in value before
| logistic transformation see also example/demo.py
|
| Parameters
| ----------
| margin: array like
| Prediction margin of each datapoint
|
| set_float_info(self, field: str, data: Any) -> None
| Set float type property into the DMatrix.
|
| Parameters
| ----------
| field: str
| The field name of the information
|
| data: numpy array
| The array of data to be set
|
| set_float_info_npy2d(self, field: str, data: Any) -> None
| Set float type property into the DMatrix
| for numpy 2d array input
|
| Parameters
| ----------
| field: str
| The field name of the information
|
| data: numpy array
| The array of data to be set
|
| set_group(self, group: Any) -> None
| Set group size of DMatrix (used for ranking).
|
| Parameters
| ----------
| group : array like
| Group size of each group
|
| set_info(self, *, label: Optional[Any] = None, weight: Optional[Any] = None, base_margin: Optional[Any] = None, group: Optional[Any] = None, qid: Optional[Any] = None, label_lower_bound: Optional[Any] = None, label_upper_bound: Optional[Any] = None, feature_names: Optional[Sequence[str]] = None, feature_types: Optional[Sequence[str]] = None, feature_weights: Optional[Any] = None) -> None
| Set meta info for DMatrix. See doc string for :py:obj:`xgboost.DMatrix`.
|
| set_label(self, label: Any) -> None
| Set label of dmatrix
|
| Parameters
| ----------
| label: array like
| The label information to be set into DMatrix
|
| set_uint_info(self, field: str, data: Any) -> None
| Set uint type property into the DMatrix.
|
| Parameters
| ----------
| field: str
| The field name of the information
|
| data: numpy array
| The array of data to be set
|
| set_weight(self, weight: Any) -> None
| Set weight of each instance.
|
| Parameters
| ----------
| weight : array like
| Weight for each data point
|
| .. note:: For ranking task, weights are per-group.
|
| In ranking task, one weight is assigned to each group (not each
| data point). This is because we only care about the relative
| ordering of data points within each group, so it doesn't make
| sense to assign weights to individual data points.
|
| slice(self, rindex: Union[List[int], numpy.ndarray], allow_groups: bool = False) -> 'DMatrix'
| Slice the DMatrix and return a new DMatrix that only contains `rindex`.
|
| Parameters
| ----------
| rindex
| List of indices to be selected.
| allow_groups
| Allow slicing of a matrix with a groups attribute
|
| Returns
| -------
| res
| A new DMatrix containing only selected indices.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| feature_names
| Labels for features (column labels).
|
| Setting it to ``None`` resets existing feature names.
|
| feature_types
| Type of features (column types).
|
| This is for displaying the results and categorical data support. See
| :py:class:`DMatrix` for details.
|
| Setting it to ``None`` resets existing feature types.
class DataIter(abc.ABC)
| DataIter(cache_prefix: Optional[str] = None, release_data: bool = True, *, on_host: bool = True, min_cache_page_bytes: Optional[int] = None) -> None
|
| The interface for user defined data iterator. The iterator facilitates
| distributed training, :py:class:`QuantileDMatrix`, and external memory support using
| :py:class:`DMatrix` or :py:class:`ExtMemQuantileDMatrix`. Most of time, users don't
| need to interact with this class directly.
|
| .. note::
|
| The class caches some intermediate results using the `data` input (predictor
| `X`) as key. Don't repeat the `X` for multiple batches with different meta data
| (like `label`), make a copy if necessary.
|
| .. note::
|
| When the input for each batch is a DataFrame, we assume categories are
| consistently encoded for all batches. For example, given two dataframes for two
| batches, this is invalid:
|
| .. code-block::
|
| import pandas as pd
|
| x0 = pd.DataFrame({"a": [0, 1]}, dtype="category")
| x1 = pd.DataFrame({"a": [1, 2]}, dtype="category")
|
| This is invalid because the `x0` has `[0, 1]` as categories while `x2` has `[1,
| 2]`. They should share the same set of categories and encoding:
|
| .. code-block::
|
| import numpy as np
|
| categories = np.array([0, 1, 2])
| x0["a"] = pd.Categorical.from_codes(
| codes=np.array([0, 1]), categories=categories
| )
| x1["a"] = pd.Categorical.from_codes(
| codes=np.array([1, 2]), categories=categories
| )
|
| You can make sure the consistent encoding in your preprocessing step be careful
| that the data is stored in formats that preserve the encoding when chunking the
| data.
|
| Parameters
| ----------
| cache_prefix :
| Prefix to the cache files, only used in external memory.
|
| Note that using this class for external memory **will cache data
| on disk** under the path passed here.
|
| release_data :
| Whether the iterator should release the data during iteration. Set it to True if
| the data transformation (converting data to np.float32 type) is memory
| intensive. Otherwise, if the transformation is computation intensive then we can
| keep the cache.
|
| on_host :
| Whether the data should be cached on the host memory instead of the file system
| when using GPU with external memory. When set to true (the default), the
| "external memory" is the CPU (host) memory. See
| :doc:`/tutorials/external_memory` for more info.
|
| .. versionadded:: 3.0.0
|
| .. warning::
|
| This is an experimental parameter and subject to change.
|
| min_cache_page_bytes :
| The minimum number of bytes of each cached pages. Only used for on-host cache
| with GPU-based :py:class:`ExtMemQuantileDMatrix`. When using GPU-based external
| memory with the data cached in the host memory, XGBoost can concatenate the
| pages internally to increase the batch size for the GPU. The default page size
| is about 1/16 of the total device memory. Users can manually set the value based
| on the actual hardware and datasets. Set this to 0 to disable page
| concatenation.
|
| .. versionadded:: 3.0.0
|
| .. warning::
|
| This is an experimental parameter and subject to change.
|
| Method resolution order:
| DataIter
| abc.ABC
| builtins.object
|
| Methods defined here:
|
| __del__(self) -> None
|
| __init__(self, cache_prefix: Optional[str] = None, release_data: bool = True, *, on_host: bool = True, min_cache_page_bytes: Optional[int] = None) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| get_callbacks(self, enable_categorical: bool) -> Tuple[Callable, Callable]
| Get callback functions for iterating in C. This is an internal function.
|
| next(self, input_data: Callable) -> bool
| Set the next batch of data.
|
| Parameters
| ----------
|
| input_data:
| A function with same data fields like `data`, `label` with
| `xgboost.DMatrix`.
|
| Returns
| -------
| False if there's no more batch, otherwise True.
|
| reraise(self) -> None
| Reraise the exception thrown during iteration.
|
| reset(self) -> None
| Reset the data iterator. Prototype for user defined function.
|
| ----------------------------------------------------------------------
| Readonly properties defined here:
|
| proxy
| Handle of DMatrix proxy.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset({'next', 'reset'})
|
| __annotations__ = {}
class ExtMemQuantileDMatrix(DMatrix, _RefMixIn)
| ExtMemQuantileDMatrix(data: xgboost.core.DataIter, *, missing: Optional[float] = None, nthread: Optional[int] = None, max_bin: Optional[int] = None, ref: Optional[xgboost.core.DMatrix] = None, enable_categorical: bool = True, max_quantile_batches: Optional[int] = None, cache_host_ratio: Optional[float] = None) -> None
|
| The external memory version of the :py:class:`QuantileDMatrix`.
|
| See :doc:`/tutorials/external_memory` for explanation and usage examples, and
| :py:class:`QuantileDMatrix` for parameter document.
|
| .. warning::
|
| This is an experimental feature and subject to change.
|
| .. versionadded:: 3.0.0
|
| Method resolution order:
| ExtMemQuantileDMatrix
| DMatrix
| _RefMixIn
| builtins.object
|
| Methods defined here:
|
| __init__(self, data: xgboost.core.DataIter, *, missing: Optional[float] = None, nthread: Optional[int] = None, max_bin: Optional[int] = None, ref: Optional[xgboost.core.DMatrix] = None, enable_categorical: bool = True, max_quantile_batches: Optional[int] = None, cache_host_ratio: Optional[float] = None) -> None
| Parameters
| ----------
| data :
| A user-defined :py:class:`DataIter` for loading data.
|
| max_quantile_batches :
| Deprecated. See :py:class:`QuantileDMatrix`.
|
| cache_host_ratio :
|
| .. versionadded:: 3.1.0
|
| Used by the GPU implementation. For GPU-based inputs, XGBoost can split the
| cache into host and device caches to reduce the data transfer overhead. This
| parameter specifies the size of host cache compared to the size of the
| entire cache: :math:`host / (host + device)`.
|
| See :ref:`extmem-adaptive-cache` for more info.
|
| ----------------------------------------------------------------------
| Methods inherited from DMatrix:
|
| __del__(self) -> None
|
| data_split_mode(self) -> xgboost._typing.DataSplitMode
| Get the data split mode of the DMatrix.
|
| .. versionadded:: 2.1.0
|
| get_base_margin(self) -> Union[numpy.ndarray, Any]
| Get the base margin of the DMatrix.
|
| get_categories(self, export_to_arrow: bool = False) -> xgboost._data_utils.Categories
| Get the categories in the dataset.
|
| .. versionadded:: 3.1.0
|
| .. warning::
|
| This function is experimental.
|
| Parameters
| ----------
| export_to_arrow :
| The returned container will contain a list of ``pyarrow`` arrays for the
| categories. See the :py:meth:`~Categories.to_arrow` for more info.
|
| get_data(self) -> scipy.sparse._csr.csr_matrix
| Get the predictors from DMatrix as a CSR matrix. This getter is mostly for
| testing purposes. If this is a quantized DMatrix then quantized values are
| returned instead of input values.
|
| .. versionadded:: 1.7.0
|
| get_float_info(self, field: str) -> Union[numpy.ndarray, Any]
| Get float property from the DMatrix.
|
| Parameters
| ----------
| field: str
| The field name of the information.
|
| get_group(self) -> numpy.ndarray
| Get the group of the DMatrix.
|
| Returns
| -------
| group
|
| get_label(self) -> Union[numpy.ndarray, Any]
| Get the label of the DMatrix.
|
| get_quantile_cut(self) -> Tuple[numpy.ndarray, numpy.ndarray]
| Get quantile cuts for quantization.
|
| .. versionadded:: 2.0.0
|
| get_uint_info(self, field: str) -> Union[numpy.ndarray, Any]
| Get unsigned integer property from the DMatrix.
|
| Parameters
| ----------
| field: str
| The field name of the information.
|
| get_weight(self) -> Union[numpy.ndarray, Any]
| Get the weight of the DMatrix.
|
| num_col(self) -> int
| Get the number of columns (features) in the DMatrix.
|
| num_nonmissing(self) -> int
| Get the number of non-missing values in the DMatrix.
|
| .. versionadded:: 1.7.0
|
| num_row(self) -> int
| Get the number of rows in the DMatrix.
|
| save_binary(self, fname: Union[str, os.PathLike], silent: bool = True) -> None
| Save DMatrix to an XGBoost buffer. Saved binary can be later loaded
| by providing the path to :py:func:`xgboost.DMatrix` as input.
|
| Parameters
| ----------
| fname : string or os.PathLike
| Name of the output buffer file.
| silent : bool (optional; default: True)
| If set, the output is suppressed.
|
| set_base_margin(self, margin: Any) -> None
| Set base margin of booster to start from.
|
| This can be used to specify a prediction value of existing model to be
| base_margin However, remember margin is needed, instead of transformed
| prediction e.g. for logistic regression: need to put in value before
| logistic transformation see also example/demo.py
|
| Parameters
| ----------
| margin: array like
| Prediction margin of each datapoint
|
| set_float_info(self, field: str, data: Any) -> None
| Set float type property into the DMatrix.
|
| Parameters
| ----------
| field: str
| The field name of the information
|
| data: numpy array
| The array of data to be set
|
| set_float_info_npy2d(self, field: str, data: Any) -> None
| Set float type property into the DMatrix
| for numpy 2d array input
|
| Parameters
| ----------
| field: str
| The field name of the information
|
| data: numpy array
| The array of data to be set
|
| set_group(self, group: Any) -> None
| Set group size of DMatrix (used for ranking).
|
| Parameters
| ----------
| group : array like
| Group size of each group
|
| set_info(self, *, label: Optional[Any] = None, weight: Optional[Any] = None, base_margin: Optional[Any] = None, group: Optional[Any] = None, qid: Optional[Any] = None, label_lower_bound: Optional[Any] = None, label_upper_bound: Optional[Any] = None, feature_names: Optional[Sequence[str]] = None, feature_types: Optional[Sequence[str]] = None, feature_weights: Optional[Any] = None) -> None
| Set meta info for DMatrix. See doc string for :py:obj:`xgboost.DMatrix`.
|
| set_label(self, label: Any) -> None
| Set label of dmatrix
|
| Parameters
| ----------
| label: array like
| The label information to be set into DMatrix
|
| set_uint_info(self, field: str, data: Any) -> None
| Set uint type property into the DMatrix.
|
| Parameters
| ----------
| field: str
| The field name of the information
|
| data: numpy array
| The array of data to be set
|
| set_weight(self, weight: Any) -> None
| Set weight of each instance.
|
| Parameters
| ----------
| weight : array like
| Weight for each data point
|
| .. note:: For ranking task, weights are per-group.
|
| In ranking task, one weight is assigned to each group (not each
| data point). This is because we only care about the relative
| ordering of data points within each group, so it doesn't make
| sense to assign weights to individual data points.
|
| slice(self, rindex: Union[List[int], numpy.ndarray], allow_groups: bool = False) -> 'DMatrix'
| Slice the DMatrix and return a new DMatrix that only contains `rindex`.
|
| Parameters
| ----------
| rindex
| List of indices to be selected.
| allow_groups
| Allow slicing of a matrix with a groups attribute
|
| Returns
| -------
| res
| A new DMatrix containing only selected indices.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from DMatrix:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| feature_names
| Labels for features (column labels).
|
| Setting it to ``None`` resets existing feature names.
|
| feature_types
| Type of features (column types).
|
| This is for displaying the results and categorical data support. See
| :py:class:`DMatrix` for details.
|
| Setting it to ``None`` resets existing feature types.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from _RefMixIn:
|
| ref
| Internal method for retrieving a reference to the training DMatrix.
class QuantileDMatrix(DMatrix, _RefMixIn)
| QuantileDMatrix(data: Any, label: Optional[Any] = None, *, weight: Optional[Any] = None, base_margin: Optional[Any] = None, missing: Optional[float] = None, silent: bool = False, feature_names: Optional[Sequence[str]] = None, feature_types: Optional[Sequence[str]] = None, nthread: Optional[int] = None, max_bin: Optional[int] = None, ref: Optional[xgboost.core.DMatrix] = None, group: Optional[Any] = None, qid: Optional[Any] = None, label_lower_bound: Optional[Any] = None, label_upper_bound: Optional[Any] = None, feature_weights: Optional[Any] = None, enable_categorical: bool = True, max_quantile_batches: Optional[int] = None, data_split_mode: xgboost._typing.DataSplitMode = <DataSplitMode.ROW: 0>) -> None
|
| A DMatrix variant that generates quantilized data directly from input for the
| ``hist`` tree method. This DMatrix is primarily designed to save memory in training
| by avoiding intermediate storage. Set ``max_bin`` to control the number of bins
| during quantisation, which should be consistent with the training parameter
| ``max_bin``. When ``QuantileDMatrix`` is used for validation/test dataset, ``ref``
| should be another ``QuantileDMatrix`` or ``DMatrix``, but not recommended as it
| defeats the purpose of saving memory) constructed from training dataset. See
| :py:obj:`xgboost.DMatrix` for documents on meta info.
|
| .. note::
|
| Do not use ``QuantileDMatrix`` as validation/test dataset without supplying a
| reference (the training dataset) ``QuantileDMatrix`` using ``ref`` as some
| information may be lost in quantisation.
|
| .. versionadded:: 1.7.0
|
| Examples
| --------
|
| .. code-block::
|
| from sklearn.datasets import make_regression
| from sklearn.model_selection import train_test_split
|
| X, y = make_regression()
| X_train, X_test, y_train, y_test = train_test_split(X, y)
| Xy_train = xgb.QuantileDMatrix(X_train, y_train)
| # It's necessary to have the training DMatrix as a reference for valid
| # quantiles.
| Xy_test = xgb.QuantileDMatrix(X_test, y_test, ref=Xy_train)
|
| Parameters
| ----------
| max_bin :
| The number of histogram bin, should be consistent with the training parameter
| ``max_bin``.
|
| ref :
| The training dataset that provides quantile information, needed when creating
| validation/test dataset with ``QuantileDMatrix``. Supplying the training DMatrix
| as a reference means that the same quantisation applied to the training data is
| applied to the validation/test data
|
| max_quantile_batches :
| Deprecated. This parameter no longer has any effect and will be removed in a
| future release.
|
| .. versionadded:: 3.0.0
|
| .. deprecated:: 3.3.0
|
| Method resolution order:
| QuantileDMatrix
| DMatrix
| _RefMixIn
| builtins.object
|
| Methods defined here:
|
| __init__(self, data: Any, label: Optional[Any] = None, *, weight: Optional[Any] = None, base_margin: Optional[Any] = None, missing: Optional[float] = None, silent: bool = False, feature_names: Optional[Sequence[str]] = None, feature_types: Optional[Sequence[str]] = None, nthread: Optional[int] = None, max_bin: Optional[int] = None, ref: Optional[xgboost.core.DMatrix] = None, group: Optional[Any] = None, qid: Optional[Any] = None, label_lower_bound: Optional[Any] = None, label_upper_bound: Optional[Any] = None, feature_weights: Optional[Any] = None, enable_categorical: bool = True, max_quantile_batches: Optional[int] = None, data_split_mode: xgboost._typing.DataSplitMode = <DataSplitMode.ROW: 0>) -> None
| Parameters
| ----------
| data :
| Data source of DMatrix. See :ref:`py-data` for a list of supported input
| types.
|
| Note that, if passing an iterator, it **will cache data on disk**, and note
| that fields like ``label`` will be concatenated in-memory from multiple
| calls to the iterator.
| label :
| Label of the training data.
| weight :
| Weight for each instance.
|
| .. note::
|
| For ranking task, weights are per-group. In ranking task, one weight
| is assigned to each group (not each data point). This is because we
| only care about the relative ordering of data points within each group,
| so it doesn't make sense to assign weights to individual data points.
|
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| missing :
| Value in the input data which needs to be present as a missing value. If
| None, defaults to np.nan.
| silent :
| Whether print messages during construction
| feature_names :
| Set names for features.
| feature_types :
|
| Set types for features. If `data` is a DataFrame type and passing
| `enable_categorical=True`, the types will be deduced automatically from the
| column types.
|
| Otherwise, one can pass a list-like input with the same length as number of
| columns in `data`, with the following possible values:
|
| - "c", which represents categorical columns.
| - "q", which represents numeric columns.
| - "int", which represents integer columns.
| - "i", which represents boolean columns.
|
| Note that, while categorical types are treated differently from the rest for
| model fitting purposes, the other types do not influence the generated
| model, but have effects in other functionalities such as feature
| importances.
|
| For categorical features, the input is assumed to be preprocessed and
| encoded by the users. The encoding can be done via
| :py:class:`sklearn.preprocessing.OrdinalEncoder` or pandas dataframe
| `.cat.codes` method. This is useful when users want to specify categorical
| features without having to construct a dataframe as input.
|
| .. versionadded:: 3.1.0
|
| Alternatively, user can pass a :py:class:`~xgboost.core.Categories` object
| returned from previous training as a reference for re-coding. One can obtain
| the reference with the :py:meth:`.get_categories` from the previous training
| DMatrix or the Booster. This feature is experimental.
|
| nthread :
| Number of threads to use for loading data when parallelization is
| applicable. If -1, uses maximum threads available on the system.
| group :
| Group size for all ranking group.
| qid :
| Query ID for data samples, used for ranking.
| label_lower_bound :
| Lower bound for survival training.
| label_upper_bound :
| Upper bound for survival training.
| feature_weights :
| Set feature weights for column sampling.
| enable_categorical :
|
| .. versionadded:: 1.3.0
|
| .. note:: This parameter is experimental
|
| Experimental support of specializing for categorical features. See
| :doc:`/tutorials/categorical` for more info.
|
| If passing `True` and `data` is a data frame (from supported libraries such
| as Pandas, Modin, polars, and cuDF), The DMatrix recognizes categorical
| columns and automatically set the `feature_types` parameter. If `data` is
| not a data frame, this argument is ignored.
|
| If passing `False` and `data` is a data frame with categorical columns, it
| will result in an error.
|
| See notes in the :py:class:`DataIter` for consistency requirement when the
| input is an iterator.
|
| .. versionchanged:: 3.1.0
|
| XGBoost can remember the encoding of categories when the input is a
| dataframe.
|
| ----------------------------------------------------------------------
| Methods inherited from DMatrix:
|
| __del__(self) -> None
|
| data_split_mode(self) -> xgboost._typing.DataSplitMode
| Get the data split mode of the DMatrix.
|
| .. versionadded:: 2.1.0
|
| get_base_margin(self) -> Union[numpy.ndarray, Any]
| Get the base margin of the DMatrix.
|
| get_categories(self, export_to_arrow: bool = False) -> xgboost._data_utils.Categories
| Get the categories in the dataset.
|
| .. versionadded:: 3.1.0
|
| .. warning::
|
| This function is experimental.
|
| Parameters
| ----------
| export_to_arrow :
| The returned container will contain a list of ``pyarrow`` arrays for the
| categories. See the :py:meth:`~Categories.to_arrow` for more info.
|
| get_data(self) -> scipy.sparse._csr.csr_matrix
| Get the predictors from DMatrix as a CSR matrix. This getter is mostly for
| testing purposes. If this is a quantized DMatrix then quantized values are
| returned instead of input values.
|
| .. versionadded:: 1.7.0
|
| get_float_info(self, field: str) -> Union[numpy.ndarray, Any]
| Get float property from the DMatrix.
|
| Parameters
| ----------
| field: str
| The field name of the information.
|
| get_group(self) -> numpy.ndarray
| Get the group of the DMatrix.
|
| Returns
| -------
| group
|
| get_label(self) -> Union[numpy.ndarray, Any]
| Get the label of the DMatrix.
|
| get_quantile_cut(self) -> Tuple[numpy.ndarray, numpy.ndarray]
| Get quantile cuts for quantization.
|
| .. versionadded:: 2.0.0
|
| get_uint_info(self, field: str) -> Union[numpy.ndarray, Any]
| Get unsigned integer property from the DMatrix.
|
| Parameters
| ----------
| field: str
| The field name of the information.
|
| get_weight(self) -> Union[numpy.ndarray, Any]
| Get the weight of the DMatrix.
|
| num_col(self) -> int
| Get the number of columns (features) in the DMatrix.
|
| num_nonmissing(self) -> int
| Get the number of non-missing values in the DMatrix.
|
| .. versionadded:: 1.7.0
|
| num_row(self) -> int
| Get the number of rows in the DMatrix.
|
| save_binary(self, fname: Union[str, os.PathLike], silent: bool = True) -> None
| Save DMatrix to an XGBoost buffer. Saved binary can be later loaded
| by providing the path to :py:func:`xgboost.DMatrix` as input.
|
| Parameters
| ----------
| fname : string or os.PathLike
| Name of the output buffer file.
| silent : bool (optional; default: True)
| If set, the output is suppressed.
|
| set_base_margin(self, margin: Any) -> None
| Set base margin of booster to start from.
|
| This can be used to specify a prediction value of existing model to be
| base_margin However, remember margin is needed, instead of transformed
| prediction e.g. for logistic regression: need to put in value before
| logistic transformation see also example/demo.py
|
| Parameters
| ----------
| margin: array like
| Prediction margin of each datapoint
|
| set_float_info(self, field: str, data: Any) -> None
| Set float type property into the DMatrix.
|
| Parameters
| ----------
| field: str
| The field name of the information
|
| data: numpy array
| The array of data to be set
|
| set_float_info_npy2d(self, field: str, data: Any) -> None
| Set float type property into the DMatrix
| for numpy 2d array input
|
| Parameters
| ----------
| field: str
| The field name of the information
|
| data: numpy array
| The array of data to be set
|
| set_group(self, group: Any) -> None
| Set group size of DMatrix (used for ranking).
|
| Parameters
| ----------
| group : array like
| Group size of each group
|
| set_info(self, *, label: Optional[Any] = None, weight: Optional[Any] = None, base_margin: Optional[Any] = None, group: Optional[Any] = None, qid: Optional[Any] = None, label_lower_bound: Optional[Any] = None, label_upper_bound: Optional[Any] = None, feature_names: Optional[Sequence[str]] = None, feature_types: Optional[Sequence[str]] = None, feature_weights: Optional[Any] = None) -> None
| Set meta info for DMatrix. See doc string for :py:obj:`xgboost.DMatrix`.
|
| set_label(self, label: Any) -> None
| Set label of dmatrix
|
| Parameters
| ----------
| label: array like
| The label information to be set into DMatrix
|
| set_uint_info(self, field: str, data: Any) -> None
| Set uint type property into the DMatrix.
|
| Parameters
| ----------
| field: str
| The field name of the information
|
| data: numpy array
| The array of data to be set
|
| set_weight(self, weight: Any) -> None
| Set weight of each instance.
|
| Parameters
| ----------
| weight : array like
| Weight for each data point
|
| .. note:: For ranking task, weights are per-group.
|
| In ranking task, one weight is assigned to each group (not each
| data point). This is because we only care about the relative
| ordering of data points within each group, so it doesn't make
| sense to assign weights to individual data points.
|
| slice(self, rindex: Union[List[int], numpy.ndarray], allow_groups: bool = False) -> 'DMatrix'
| Slice the DMatrix and return a new DMatrix that only contains `rindex`.
|
| Parameters
| ----------
| rindex
| List of indices to be selected.
| allow_groups
| Allow slicing of a matrix with a groups attribute
|
| Returns
| -------
| res
| A new DMatrix containing only selected indices.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from DMatrix:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| feature_names
| Labels for features (column labels).
|
| Setting it to ``None`` resets existing feature names.
|
| feature_types
| Type of features (column types).
|
| This is for displaying the results and categorical data support. See
| :py:class:`DMatrix` for details.
|
| Setting it to ``None`` resets existing feature types.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from _RefMixIn:
|
| ref
| Internal method for retrieving a reference to the training DMatrix.
class SingleBatchInternalIter(DataIter)
| SingleBatchInternalIter(**kwargs: Any) -> None
|
| An iterator for single batch data to help creating device DMatrix.
| Transforming input directly to histogram with normal single batch data API
| can not access weight for sketching. So this iterator acts as a staging
| area for meta info.
|
| Method resolution order:
| SingleBatchInternalIter
| DataIter
| abc.ABC
| builtins.object
|
| Methods defined here:
|
| __init__(self, **kwargs: Any) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| next(self, input_data: Callable) -> bool
| Set the next batch of data.
|
| Parameters
| ----------
|
| input_data:
| A function with same data fields like `data`, `label` with
| `xgboost.DMatrix`.
|
| Returns
| -------
| False if there's no more batch, otherwise True.
|
| reset(self) -> None
| Reset the data iterator. Prototype for user defined function.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset()
|
| __annotations__ = {}
|
| ----------------------------------------------------------------------
| Methods inherited from DataIter:
|
| __del__(self) -> None
|
| get_callbacks(self, enable_categorical: bool) -> Tuple[Callable, Callable]
| Get callback functions for iterating in C. This is an internal function.
|
| reraise(self) -> None
| Reraise the exception thrown during iteration.
|
| ----------------------------------------------------------------------
| Readonly properties inherited from DataIter:
|
| proxy
| Handle of DMatrix proxy.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from DataIter:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
FUNCTIONS
build_info() -> dict
Build information of XGBoost. The returned value format is not stable. Also,
please note that build time dependency is not the same as runtime dependency. For
instance, it's possible to build XGBoost with older CUDA version but run it with the
lastest one.
.. versionadded:: 1.6.0
c_array(ctype: Type[~CTypeT], values: Any) -> Union[_ctypes.Array, _ctypes._Pointer]
Convert a python array to c array.
ctypes2buffer(cptr: _ctypes._Pointer, length: int) -> bytearray
Convert ctypes pointer to buffer type.
ctypes2numpy(cptr: _ctypes._Pointer, length: int, dtype: Type[numpy.number]) -> numpy.ndarray
Convert a ctypes pointer array to a numpy array.
require_keyword_args(error: bool) -> Callable[[Callable[..., ~_T]], Callable[..., ~_T]]
Decorator for methods that issues warnings for positional arguments
Using the keyword-only argument syntax in pep 3102, arguments after the
* will issue a warning or error when passed as a positional argument.
Modified from sklearn utils.validation.
Parameters
----------
error :
Whether to throw an error or raise a warning.
DATA
ArrowCatList = typing.List[typing.Tuple[str, typing.Union[Forwa...ray'...
BoosterParam = typing.Union[typing.List, typing.Dict[str, typing.Any]]
CNumeric = typing.Union[ctypes.c_float, ctypes.c_double, ct...es.c_ulo...
CTypeT = ~CTypeT
Callable = typing.Callable
Deprecated alias to collections.abc.Callable.
Callable[[int], str] signifies a function that takes a single
parameter of type int and returns a str.
The subscription syntax must always be used with exactly two
values: the argument list and the return type.
The argument list must be a list of types, a ParamSpec,
Concatenate or ellipsis. The return type must be a single type.
There is no syntax to indicate optional or keyword arguments;
such function types are rarely used as callback types.
Dict = typing.Dict
A generic version of dict.
FeatureInfo = typing.Sequence[str]
FeatureNames = typing.Sequence[str]
FeatureTypes = typing.Sequence[str]
Generator = typing.Generator
A generic version of collections.abc.Generator.
Integer = typing.Union[int, numpy.integer]
IterRange = ~IterRange
Iterable = typing.Iterable
A generic version of collections.abc.Iterable.
IterationRange = typing.Tuple[typing.Union[int, numpy.integer], typing...
List = typing.List
A generic version of list.
Metric = typing.Callable[[numpy.ndarray, xgboost.core.DMatrix], typing...
ModelIn = typing.Union[os.PathLike[~AnyStr], bytearray, str]
NumpyOrCupy = typing.Union[numpy.ndarray, typing.Any]
Optional = typing.Optional
Optional[X] is equivalent to Union[X, None].
PathLike = typing.Union[str, os.PathLike]
PlainObj = typing.Callable[[numpy.ndarray, xgboost.core.DMatrix], typi...
Sequence = typing.Sequence
A generic version of collections.abc.Sequence.
TYPE_CHECKING = False
TransformedData = typing.Tuple[typing.Any, typing.Optional[typing....n...
Tuple = typing.Tuple
Deprecated alias to builtins.tuple.
Tuple[X, Y] is the cross-product type of X and Y.
Example: Tuple[T1, T2] is a tuple of two elements corresponding
to type variables T1 and T2. Tuple[int, float, str] is a tuple
of an int, a float and a string.
To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
Type = typing.Type
Deprecated alias to builtins.type.
builtins.type or typing.Type can be used to annotate class objects.
For example, suppose we have the following classes::
class User: ... # Abstract base for User classes
class BasicUser(User): ...
class ProUser(User): ...
class TeamUser(User): ...
And a function that takes a class argument that's a subclass of
User and returns an instance of the corresponding class::
def new_user[U](user_class: Type[U]) -> U:
user = user_class()
# (Here we could write the user object to a database)
return user
joe = new_user(BasicUser)
At this point the type checker knows that joe has type BasicUser.
TypeGuard = typing.TypeGuard
Special typing construct for marking user-defined type guard functions.
``TypeGuard`` can be used to annotate the return type of a user-defined
type guard function. ``TypeGuard`` only accepts a single type argument.
At runtime, functions marked this way should return a boolean.
``TypeGuard`` aims to benefit *type narrowing* -- a technique used by static
type checkers to determine a more precise type of an expression within a
program's code flow. Usually type narrowing is done by analyzing
conditional code flow and applying the narrowing to a block of code. The
conditional expression here is sometimes referred to as a "type guard".
Sometimes it would be convenient to use a user-defined boolean function
as a type guard. Such a function should use ``TypeGuard[...]`` as its
return type to alert static type checkers to this intention.
Using ``-> TypeGuard`` tells the static type checker that for a given
function:
1. The return value is a boolean.
2. If the return value is ``True``, the type of its argument
is the type inside ``TypeGuard``.
For example::
def is_str_list(val: list[object]) -> TypeGuard[list[str]]:
'''Determines whether all objects in the list are strings'''
return all(isinstance(x, str) for x in val)
def func1(val: list[object]):
if is_str_list(val):
# Type of ``val`` is narrowed to ``list[str]``.
print(" ".join(val))
else:
# Type of ``val`` remains as ``list[object]``.
print("Not a list of strings!")
Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower
form of ``TypeA`` (it can even be a wider form) and this may lead to
type-unsafe results. The main reason is to allow for things like
narrowing ``list[object]`` to ``list[str]`` even though the latter is not
a subtype of the former, since ``list`` is invariant. The responsibility of
writing type-safe type guards is left to the user.
``TypeGuard`` also works with type variables. For more information, see
PEP 647 (User-Defined Type Guards).
Union = typing.Union
Union type; Union[X, Y] means either X or Y.
On Python 3.10 and higher, the | operator
can also be used to denote unions;
X | Y means the same thing to the type checker as Union[X, Y].
To define a union, use e.g. Union[int, str]. Details:
- The arguments must be types and there must be at least one.
- None as an argument is a special case and is replaced by
type(None).
- Unions of unions are flattened, e.g.::
assert Union[Union[int, str], float] == Union[int, str, float]
- Unions of a single argument vanish, e.g.::
assert Union[int] == int # The constructor actually returns int
- Redundant arguments are skipped, e.g.::
assert Union[int, str, int] == Union[int, str]
- When comparing unions, the argument order is ignored, e.g.::
assert Union[int, str] == Union[str, int]
- You cannot subclass or instantiate a union.
- You can use Optional[X] as a shorthand for Union[X, None].
FILE
c:\users\youse\appdata\local\programs\python\python312\lib\site-packages\xgboost\core.py
xgboost.cv
Python Library Documentation: function cv in module xgboost.training
cv(params: Union[List, Dict[str, Any]], dtrain: xgboost.core.DMatrix, num_boost_round: int = 10, *, nfold: int = 3, stratified: bool = False, folds: Optional[sklearn.model_selection._split.StratifiedKFold] = None, metrics: Sequence[str] = (), obj: Optional[Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[numpy.ndarray, numpy.ndarray]]] = None, maximize: Optional[bool] = None, early_stopping_rounds: Optional[int] = None, fpreproc: Optional[Callable] = None, as_pandas: bool = True, verbose_eval: Union[bool, int, NoneType] = None, show_stdv: bool = True, seed: int = 0, callbacks: Optional[Sequence[xgboost.callback.TrainingCallback]] = None, shuffle: bool = True, custom_metric: Optional[Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[str, float]]] = None) -> Union[Dict[str, float], ForwardRef('PdDataFrame')]
Cross-validation with given parameters.
Parameters
----------
params : dict
Booster params.
dtrain :
Data to be trained. Only the :py:class:`DMatrix` without external memory is
supported.
num_boost_round :
Number of boosting iterations.
nfold : int
Number of folds in CV.
stratified : bool
Perform stratified sampling.
folds : a KFold or StratifiedKFold instance or list of fold indices
Sklearn KFolds or StratifiedKFolds object.
Alternatively may explicitly pass sample indices for each fold.
For ``n`` folds, **folds** should be a length ``n`` list of tuples.
Each tuple is ``(in,out)`` where ``in`` is a list of indices to be used
as the training samples for the ``n`` th fold and ``out`` is a list of
indices to be used as the testing samples for the ``n`` th fold.
metrics : string or list of strings
Evaluation metrics to be watched in CV.
obj :
Custom objective function. See :doc:`Custom Objective
</tutorials/custom_metric_obj>` for details.
maximize : bool
Whether to maximize the evaluataion metric (score or error).
early_stopping_rounds: int
Activates early stopping. Cross-Validation metric (average of validation
metric computed over CV folds) needs to improve at least once in
every **early_stopping_rounds** round(s) to continue training.
The last entry in the evaluation history will represent the best iteration.
If there's more than one metric in the **eval_metric** parameter given in
**params**, the last metric will be used for early stopping.
fpreproc : function
Preprocessing function that takes (dtrain, dtest, param) and returns
transformed versions of those.
as_pandas : bool, default True
Return pd.DataFrame when pandas is installed.
If False or pandas is not installed, return np.ndarray
verbose_eval : bool, int, or None, default None
Whether to display the progress. If None, progress will be displayed
when np.ndarray is returned. If True, progress will be displayed at
boosting stage. If an integer is given, progress will be displayed
at every given `verbose_eval` boosting stage.
show_stdv : bool, default True
Whether to display the standard deviation in progress.
Results are not affected, and always contains std.
seed : int
Seed used to generate the folds (passed to numpy.random.seed).
callbacks :
List of callback functions that are applied at end of each iteration.
It is possible to use predefined callbacks by using
:ref:`Callback API <callback_api>`.
.. note::
States in callback are not preserved during training, which means callback
objects can not be reused for multiple training sessions without
reinitialization or deepcopy.
.. code-block:: python
for params in parameters_grid:
# be sure to (re)initialize the callbacks before each run
callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
xgboost.train(params, Xy, callbacks=callbacks)
shuffle : bool
Shuffle data before creating folds.
custom_metric :
.. versionadded 1.6.0
Custom metric function. See :doc:`Custom Metric </tutorials/custom_metric_obj>`
for details.
Returns
-------
evaluation history : list(string)
xgboost.data
Python Library Documentation: module xgboost.data in xgboost
NAME
xgboost.data - Data dispatching for DMatrix.
CLASSES
xgboost._data_utils.TransformedDf(abc.ABC)
ArrowTransformed
CudfTransformed
PandasTransformed
class ArrowTransformed(xgboost._data_utils.TransformedDf)
| ArrowTransformed(columns: List[Union[ForwardRef('pa.NumericArray'), ForwardRef('pa.DictionaryArray')]], ref_categories: Optional[xgboost._data_utils.Categories] = None) -> None
|
| A storage class for transformed arrow table.
|
| Method resolution order:
| ArrowTransformed
| xgboost._data_utils.TransformedDf
| abc.ABC
| builtins.object
|
| Methods defined here:
|
| __init__(self, columns: List[Union[ForwardRef('pa.NumericArray'), ForwardRef('pa.DictionaryArray')]], ref_categories: Optional[xgboost._data_utils.Categories] = None) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| ----------------------------------------------------------------------
| Readonly properties defined here:
|
| shape
| Return shape of the transformed DataFrame.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset()
|
| __annotations__ = {}
|
| ----------------------------------------------------------------------
| Methods inherited from xgboost._data_utils.TransformedDf:
|
| array_interface(self) -> bytes
| Return a byte string for JSON encoded array interface.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from xgboost._data_utils.TransformedDf:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
class CudfTransformed(xgboost._data_utils.TransformedDf)
| CudfTransformed(columns: List[Union[ForwardRef('PdSeries'), xgboost._data_utils.DfCatAccessor]], ref_categories: Optional[xgboost._data_utils.Categories]) -> None
|
| A storage class for transformed cuDF dataframe.
|
| Method resolution order:
| CudfTransformed
| xgboost._data_utils.TransformedDf
| abc.ABC
| builtins.object
|
| Methods defined here:
|
| __init__(self, columns: List[Union[ForwardRef('PdSeries'), xgboost._data_utils.DfCatAccessor]], ref_categories: Optional[xgboost._data_utils.Categories]) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| ----------------------------------------------------------------------
| Readonly properties defined here:
|
| shape
| Return shape of the transformed DataFrame.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset()
|
| __annotations__ = {}
|
| ----------------------------------------------------------------------
| Methods inherited from xgboost._data_utils.TransformedDf:
|
| array_interface(self) -> bytes
| Return a byte string for JSON encoded array interface.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from xgboost._data_utils.TransformedDf:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
class PandasTransformed(xgboost._data_utils.TransformedDf)
| PandasTransformed(columns: List[Union[numpy.ndarray, xgboost._data_utils.DfCatAccessor]], ref_categories: Optional[xgboost._data_utils.Categories]) -> None
|
| A storage class for transformed pandas DataFrame.
|
| Method resolution order:
| PandasTransformed
| xgboost._data_utils.TransformedDf
| abc.ABC
| builtins.object
|
| Methods defined here:
|
| __init__(self, columns: List[Union[numpy.ndarray, xgboost._data_utils.DfCatAccessor]], ref_categories: Optional[xgboost._data_utils.Categories]) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| ----------------------------------------------------------------------
| Readonly properties defined here:
|
| shape
| Return shape of the transformed DataFrame.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset()
|
| __annotations__ = {}
|
| ----------------------------------------------------------------------
| Methods inherited from xgboost._data_utils.TransformedDf:
|
| array_interface(self) -> bytes
| Return a byte string for JSON encoded array interface.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from xgboost._data_utils.TransformedDf:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
FUNCTIONS
dispatch_data_backend(*, data: Any, missing: Union[float, numpy.float32, numpy.float64], threads: int, feature_names: Optional[Sequence[str]], feature_types: Union[Sequence[str], xgboost._data_utils.Categories, NoneType], enable_categorical: bool = False, data_split_mode: xgboost._typing.DataSplitMode = <DataSplitMode.ROW: 0>) -> Tuple[ctypes.c_void_p, Optional[Sequence[str]], Optional[Sequence[str]]]
Dispatch data for DMatrix.
dispatch_meta_backend(matrix: 'DMatrix', data: Any, name: str, dtype: Union[str, Type[numpy.number], numpy.dtype[Any], NoneType] = None) -> None
Dispatch for meta info.
dispatch_proxy_set_data(proxy: '_ProxyDMatrix', data: Any) -> None
Dispatch for QuantileDMatrix.
is_nullable_dtype(dtype: Any) -> bool
Whether dtype is a pandas nullable type.
is_on_cuda(data: Any) -> bool
Whether the data is a CUDA-based data structure.
is_pa_ext_categorical_dtype(dtype: Any) -> bool
Check whether dtype is a dictionary type.
is_pa_ext_dtype(dtype: Any) -> bool
Return whether dtype is a pyarrow extension type for pandas
is_pd_cat_dtype(dtype: Any) -> bool
Wrapper for testing pandas category type.
is_pd_sparse_dtype(dtype: Any) -> bool
Wrapper for testing pandas sparse type.
is_scipy_coo(data: Any) -> bool
Predicate for scipy COO input.
is_scipy_csc(data: Any) -> bool
Predicate for scipy CSC input.
is_scipy_csr(data: Any) -> bool
Predicate for scipy CSR input.
pandas_feature_info(data: 'PdDataFrame', meta: Optional[str], feature_names: Optional[Sequence[str]], feature_types: Optional[Sequence[str]], enable_categorical: bool) -> Tuple[Optional[Sequence[str]], Optional[Sequence[str]]]
Handle feature info for pandas dataframe.
pandas_pa_type(ser: Any) -> numpy.ndarray
Handle pandas pyarrow extention.
pandas_transform_data(data: 'PdDataFrame') -> List[Union[numpy.ndarray, xgboost._data_utils.DfCatAccessor]]
Handle categorical dtype and extension types from pandas.
transform_scipy_sparse(data: Any, is_csr: bool) -> Any
Ensure correct data alignment and data type for scipy sparse inputs. Input should
be either csr or csc matrix.
DATA
AifType = typing.List[typing.Union[xgboost._data_utils.Arr...ls.ArrayI...
CAT_T = 'c'
Callable = typing.Callable
Deprecated alias to collections.abc.Callable.
Callable[[int], str] signifies a function that takes a single
parameter of type int and returns a str.
The subscription syntax must always be used with exactly two
values: the argument list and the return type.
The argument list must be a list of types, a ParamSpec,
Concatenate or ellipsis. The return type must be a single type.
There is no syntax to indicate optional or keyword arguments;
such function types are rarely used as callback types.
Dict = typing.Dict
A generic version of dict.
DispatchedDataBackendReturnType = typing.Tuple[ctypes.c_void_p, typing...
FeatureNames = typing.Sequence[str]
FeatureTypes = typing.Sequence[str]
FloatCompatible = typing.Union[float, numpy.float32, numpy.float64]
List = typing.List
A generic version of list.
NumpyDType = typing.Union[str, typing.Type[numpy.number], numpy.dtype[...
Optional = typing.Optional
Optional[X] is equivalent to Union[X, None].
PathLike = typing.Union[str, os.PathLike]
Sequence = typing.Sequence
A generic version of collections.abc.Sequence.
TYPE_CHECKING = False
TransformedData = typing.Tuple[typing.Any, typing.Optional[typing....n...
Tuple = typing.Tuple
Deprecated alias to builtins.tuple.
Tuple[X, Y] is the cross-product type of X and Y.
Example: Tuple[T1, T2] is a tuple of two elements corresponding
to type variables T1 and T2. Tuple[int, float, str] is a tuple
of an int, a float and a string.
To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
TypeAlias = typing.TypeAlias
Special form for marking type aliases.
Use TypeAlias to indicate that an assignment should
be recognized as a proper type alias definition by type
checkers.
For example::
Predicate: TypeAlias = Callable[..., bool]
It's invalid when used anywhere except as in the example above.
TypeGuard = typing.TypeGuard
Special typing construct for marking user-defined type guard functions.
``TypeGuard`` can be used to annotate the return type of a user-defined
type guard function. ``TypeGuard`` only accepts a single type argument.
At runtime, functions marked this way should return a boolean.
``TypeGuard`` aims to benefit *type narrowing* -- a technique used by static
type checkers to determine a more precise type of an expression within a
program's code flow. Usually type narrowing is done by analyzing
conditional code flow and applying the narrowing to a block of code. The
conditional expression here is sometimes referred to as a "type guard".
Sometimes it would be convenient to use a user-defined boolean function
as a type guard. Such a function should use ``TypeGuard[...]`` as its
return type to alert static type checkers to this intention.
Using ``-> TypeGuard`` tells the static type checker that for a given
function:
1. The return value is a boolean.
2. If the return value is ``True``, the type of its argument
is the type inside ``TypeGuard``.
For example::
def is_str_list(val: list[object]) -> TypeGuard[list[str]]:
'''Determines whether all objects in the list are strings'''
return all(isinstance(x, str) for x in val)
def func1(val: list[object]):
if is_str_list(val):
# Type of ``val`` is narrowed to ``list[str]``.
print(" ".join(val))
else:
# Type of ``val`` remains as ``list[object]``.
print("Not a list of strings!")
Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower
form of ``TypeA`` (it can even be a wider form) and this may lead to
type-unsafe results. The main reason is to allow for things like
narrowing ``list[object]`` to ``list[str]`` even though the latter is not
a subtype of the former, since ``list`` is invariant. The responsibility of
writing type-safe type guards is left to the user.
``TypeGuard`` also works with type variables. For more information, see
PEP 647 (User-Defined Type Guards).
Union = typing.Union
Union type; Union[X, Y] means either X or Y.
On Python 3.10 and higher, the | operator
can also be used to denote unions;
X | Y means the same thing to the type checker as Union[X, Y].
To define a union, use e.g. Union[int, str]. Details:
- The arguments must be types and there must be at least one.
- None as an argument is a special case and is replaced by
type(None).
- Unions of unions are flattened, e.g.::
assert Union[Union[int, str], float] == Union[int, str, float]
- Unions of a single argument vanish, e.g.::
assert Union[int] == int # The constructor actually returns int
- Redundant arguments are skipped, e.g.::
assert Union[int, str, int] == Union[int, str]
- When comparing unions, the argument order is ignored, e.g.::
assert Union[int, str] == Union[str, int]
- You cannot subclass or instantiate a union.
- You can use Optional[X] as a shorthand for Union[X, None].
__annotations__ = {'DispatchedDataBackendReturnType': typing.TypeAlias...
pandas_nullable_mapper = {'Float32': 'float', 'Float64': 'float', 'Int...
pandas_pyarrow_mapper = {'bool[pyarrow]': 'i', 'double[pyarrow]': 'flo...
FILE
c:\users\youse\appdata\local\programs\python\python312\lib\site-packages\xgboost\data.py
xgboost.get_config
Python Library Documentation: function get_config in module xgboost.config
get_config() -> Dict[str, Any]
Get current values of the global configuration.
Global configuration consists of a collection of parameters that can be applied in the
global scope. See :ref:`global_config` for the full list of parameters supported in
the global configuration.
.. versionadded:: 1.4.0
Returns
-------
args: Dict[str, Any]
The list of global parameters and their values
Example
-------
.. code-block:: python
import xgboost as xgb
# Show all messages, including ones pertaining to debugging
xgb.set_config(verbosity=2)
# Get current value of global configuration
# This is a dict containing all parameters in the global configuration,
# including 'verbosity'
config = xgb.get_config()
assert config['verbosity'] == 2
# Example of using the context manager xgb.config_context().
# The context manager will restore the previous value of the global
# configuration upon exiting.
with xgb.config_context(verbosity=0):
# Suppress warning caused by model generated with XGBoost version < 1.0.0
bst = xgb.Booster(model_file='./old_model.bin')
assert xgb.get_config()['verbosity'] == 2 # old value restored
Nested configuration context is also supported:
Example
-------
.. code-block:: python
with xgb.config_context(verbosity=3):
assert xgb.get_config()["verbosity"] == 3
with xgb.config_context(verbosity=2):
assert xgb.get_config()["verbosity"] == 2
xgb.set_config(verbosity=2)
assert xgb.get_config()["verbosity"] == 2
with xgb.config_context(verbosity=3):
assert xgb.get_config()["verbosity"] == 3
xgboost.interpret
Python Library Documentation: module xgboost.interpret in xgboost
NAME
xgboost.interpret - Interpretability functions for XGBoost models.
FUNCTIONS
shap_values(model: object, X: Union[xgboost.core.DMatrix, Any], *, X_background: Union[xgboost.core.DMatrix, Any, NoneType] = None, output_margin: bool = False, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None, missing: Union[float, numpy.float32, numpy.float64, NoneType] = None, validate_features: bool = True) -> Tuple[numpy.ndarray, numpy.ndarray]
Return SHAP values for an XGBoost model.
.. warning::
This function is still working in progress.
This function accepts either a :py:class:`xgboost.Booster` or an sklearn-style
XGBoost model and returns feature contributions together with the separated
bias term.
Parameters
----------
model :
XGBoost booster or sklearn-style XGBoost model.
X :
Input data.
X_background :
Background data for interventional SHAP values. This is reserved for a
future implementation and is currently unsupported.
output_margin :
Accepted for API compatibility. SHAP contributions currently correspond
to the model margin.
iteration_range :
Specifies which layer of trees are used in prediction.
missing :
Value in array-like ``X`` to treat as missing. When None, use the
model's missing value if available, otherwise ``np.nan``. This must not
be specified when ``X`` is already a DMatrix.
validate_features :
Validate feature names between the model and input data.
Returns
-------
values, bias :
``values`` contains feature SHAP values with the bias term removed.
``bias`` contains the separated bias term. For multi-target models, the
output shape follows the corresponding prediction shape with the final
feature dimension split into ``values`` and ``bias``.
Notes
-----
To use GPU algorithms, configure the model before calling this function, for
example with ``booster.set_param({"device": "cuda"})``.
DATA
__all__ = ['shap_values']
FILE
c:\users\youse\appdata\local\programs\python\python312\lib\site-packages\xgboost\interpret.py
xgboost.libpath
Python Library Documentation: module xgboost.libpath in xgboost
NAME
xgboost.libpath - Find the path to xgboost dynamic library files.
CLASSES
builtins.Exception(builtins.BaseException)
XGBoostLibraryNotFound
class XGBoostLibraryNotFound(builtins.Exception)
| Error thrown by when xgboost is not found
|
| Method resolution order:
| XGBoostLibraryNotFound
| builtins.Exception
| builtins.BaseException
| builtins.object
|
| Data descriptors defined here:
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Methods inherited from builtins.Exception:
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| ----------------------------------------------------------------------
| Static methods inherited from builtins.Exception:
|
| __new__(*args, **kwargs) class method of builtins.Exception
| Create and return a new object. See help(type) for accurate signature.
|
| ----------------------------------------------------------------------
| Methods inherited from builtins.BaseException:
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __reduce__(...)
| Helper for pickle.
|
| __repr__(self, /)
| Return repr(self).
|
| __setstate__(...)
|
| __str__(self, /)
| Return str(self).
|
| add_note(...)
| Exception.add_note(note) --
| add a note to the exception
|
| with_traceback(...)
| Exception.with_traceback(tb) --
| set self.__traceback__ to tb and return self.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from builtins.BaseException:
|
| __cause__
| exception cause
|
| __context__
| exception context
|
| __dict__
|
| __suppress_context__
|
| __traceback__
|
| args
FUNCTIONS
find_lib_path() -> List[str]
Find the path to xgboost dynamic library files.
Returns
-------
lib_path
List of all found library path to xgboost
is_sphinx_build() -> bool
`XGBOOST_BUILD_DOC` is used by the sphinx conf.py to skip building the C++ code.
DATA
List = typing.List
A generic version of list.
FILE
c:\users\youse\appdata\local\programs\python\python312\lib\site-packages\xgboost\libpath.py
xgboost.objective
Python Library Documentation: module xgboost.objective in xgboost
NAME
xgboost.objective
DESCRIPTION
Experimental support for a new objective interface with target dimension
reduction.
.. warning::
Do not use this module unless you want to participate in development.
.. versionadded:: 3.2.0
CLASSES
abc.ABC(builtins.object)
Objective
TreeObjective
class Objective(abc.ABC)
| Base class for custom objective functions.
|
| .. warning::
|
| Do not use this class unless you want to participate in development.
|
| .. versionadded:: 3.2.0
|
| Method resolution order:
| Objective
| abc.ABC
| builtins.object
|
| Methods defined here:
|
| __call__(self, iteration: int, y_pred: Any, dtrain: 'DMatrix') -> Tuple[Any, Any]
| Call self as a function.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset({'__call__'})
|
| __annotations__ = {}
class TreeObjective(Objective)
| Base class for tree-specific custom objective functions.
|
| .. warning::
|
| Do not use this class unless you want to participate in development.
|
| .. versionadded:: 3.2.0
|
| Method resolution order:
| TreeObjective
| Objective
| abc.ABC
| builtins.object
|
| Methods defined here:
|
| split_grad(self, iteration: int, grad: Any, hess: Any) -> Optional[Tuple[Any, Any]]
| Provide a different gradient type for finding tree structures.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset({'__call__'})
|
| __annotations__ = {}
|
| ----------------------------------------------------------------------
| Methods inherited from Objective:
|
| __call__(self, iteration: int, y_pred: Any, dtrain: 'DMatrix') -> Tuple[Any, Any]
| Call self as a function.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from Objective:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
DATA
NumpyOrCupy = typing.Union[numpy.ndarray, typing.Any]
TYPE_CHECKING = False
Tuple = typing.Tuple
Deprecated alias to builtins.tuple.
Tuple[X, Y] is the cross-product type of X and Y.
Example: Tuple[T1, T2] is a tuple of two elements corresponding
to type variables T1 and T2. Tuple[int, float, str] is a tuple
of an int, a float and a string.
To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
FILE
c:\users\youse\appdata\local\programs\python\python312\lib\site-packages\xgboost\objective.py
xgboost.plot_importance
Python Library Documentation: function plot_importance in module xgboost.plotting
plot_importance(booster: Union[xgboost.sklearn.XGBModel, xgboost.core.Booster, dict], *, ax: Optional[Any] = None, height: float = 0.2, xlim: Optional[tuple] = None, ylim: Optional[tuple] = None, title: str = 'Feature importance', xlabel: str = 'Importance score', ylabel: str = 'Features', fmap: Union[str, os.PathLike] = '', importance_type: str = 'weight', max_num_features: Optional[int] = None, grid: bool = True, show_values: bool = True, values_format: str = '{v}', **kwargs: Any) -> Any
Plot importance based on fitted trees.
Parameters
----------
booster :
Booster or XGBModel instance, or dict taken by Booster.get_fscore()
ax : matplotlib Axes
Target axes instance. If None, new figure and axes will be created.
grid :
Turn the axes grids on or off. Default is True (On).
importance_type :
How the importance is calculated: either "weight", "gain", or "cover"
* "weight" is the number of times a feature appears in a tree
* "gain" is the average gain of splits which use the feature
* "cover" is the average coverage of splits which use the feature
where coverage is defined as the number of samples affected by the split
max_num_features :
Maximum number of top features displayed on plot. If None, all features will be
displayed.
height :
Bar height, passed to ax.barh()
xlim :
Tuple passed to axes.xlim()
ylim :
Tuple passed to axes.ylim()
title :
Axes title. To disable, pass None.
xlabel :
X axis title label. To disable, pass None.
ylabel :
Y axis title label. To disable, pass None.
fmap :
The name of feature map file.
show_values :
Show values on plot. To disable, pass False.
values_format :
Format string for values. "v" will be replaced by the value of the feature
importance. e.g. Pass "{v:.2f}" in order to limit the number of digits after
the decimal point to two, for each value printed on the graph.
kwargs :
Other keywords passed to ax.barh()
Returns
-------
ax : matplotlib Axes
xgboost.plot_tree
Python Library Documentation: function plot_tree in module xgboost.plotting
plot_tree(booster: Union[xgboost.core.Booster, xgboost.sklearn.XGBModel], *, fmap: Union[str, os.PathLike] = '', num_trees: Optional[int] = None, rankdir: Optional[str] = None, ax: Optional[Any] = None, with_stats: bool = False, tree_idx: int = 0, **kwargs: Any) -> Any
Plot specified tree.
Parameters
----------
booster :
Booster or XGBModel instance
fmap: str (optional)
The name of feature map file
num_trees :
.. deprecated:: 3.0
rankdir : str, default "TB"
Passed to graphviz via graph_attr
ax : matplotlib Axes, default None
Target axes instance. If None, new figure and axes will be created.
with_stats :
.. versionadded:: 3.0
See :py:func:`to_graphviz`.
tree_idx :
.. versionadded:: 3.0
See :py:func:`to_graphviz`.
kwargs :
Other keywords passed to :py:func:`to_graphviz`
Returns
-------
ax : matplotlib Axes
xgboost.plotting
Python Library Documentation: module xgboost.plotting in xgboost
NAME
xgboost.plotting - Plotting Library.
FUNCTIONS
plot_importance(booster: Union[xgboost.sklearn.XGBModel, xgboost.core.Booster, dict], *, ax: Optional[Any] = None, height: float = 0.2, xlim: Optional[tuple] = None, ylim: Optional[tuple] = None, title: str = 'Feature importance', xlabel: str = 'Importance score', ylabel: str = 'Features', fmap: Union[str, os.PathLike] = '', importance_type: str = 'weight', max_num_features: Optional[int] = None, grid: bool = True, show_values: bool = True, values_format: str = '{v}', **kwargs: Any) -> Any
Plot importance based on fitted trees.
Parameters
----------
booster :
Booster or XGBModel instance, or dict taken by Booster.get_fscore()
ax : matplotlib Axes
Target axes instance. If None, new figure and axes will be created.
grid :
Turn the axes grids on or off. Default is True (On).
importance_type :
How the importance is calculated: either "weight", "gain", or "cover"
* "weight" is the number of times a feature appears in a tree
* "gain" is the average gain of splits which use the feature
* "cover" is the average coverage of splits which use the feature
where coverage is defined as the number of samples affected by the split
max_num_features :
Maximum number of top features displayed on plot. If None, all features will be
displayed.
height :
Bar height, passed to ax.barh()
xlim :
Tuple passed to axes.xlim()
ylim :
Tuple passed to axes.ylim()
title :
Axes title. To disable, pass None.
xlabel :
X axis title label. To disable, pass None.
ylabel :
Y axis title label. To disable, pass None.
fmap :
The name of feature map file.
show_values :
Show values on plot. To disable, pass False.
values_format :
Format string for values. "v" will be replaced by the value of the feature
importance. e.g. Pass "{v:.2f}" in order to limit the number of digits after
the decimal point to two, for each value printed on the graph.
kwargs :
Other keywords passed to ax.barh()
Returns
-------
ax : matplotlib Axes
plot_tree(booster: Union[xgboost.core.Booster, xgboost.sklearn.XGBModel], *, fmap: Union[str, os.PathLike] = '', num_trees: Optional[int] = None, rankdir: Optional[str] = None, ax: Optional[Any] = None, with_stats: bool = False, tree_idx: int = 0, **kwargs: Any) -> Any
Plot specified tree.
Parameters
----------
booster :
Booster or XGBModel instance
fmap: str (optional)
The name of feature map file
num_trees :
.. deprecated:: 3.0
rankdir : str, default "TB"
Passed to graphviz via graph_attr
ax : matplotlib Axes, default None
Target axes instance. If None, new figure and axes will be created.
with_stats :
.. versionadded:: 3.0
See :py:func:`to_graphviz`.
tree_idx :
.. versionadded:: 3.0
See :py:func:`to_graphviz`.
kwargs :
Other keywords passed to :py:func:`to_graphviz`
Returns
-------
ax : matplotlib Axes
to_graphviz(booster: Union[xgboost.core.Booster, xgboost.sklearn.XGBModel], *, fmap: Union[str, os.PathLike] = '', num_trees: Optional[int] = None, rankdir: Optional[str] = None, yes_color: Optional[str] = None, no_color: Optional[str] = None, condition_node_params: Optional[dict] = None, leaf_node_params: Optional[dict] = None, with_stats: bool = False, tree_idx: int = 0, **kwargs: Any) -> Any
Convert specified tree to graphviz instance. IPython can automatically plot
the returned graphviz instance. Otherwise, you should call .render() method
of the returned graphviz instance.
Parameters
----------
booster :
Booster or XGBModel instance
fmap :
The name of feature map file
num_trees :
.. deprecated:: 3.0
Specify the ordinal number of target tree
rankdir :
Passed to graphviz via graph_attr
yes_color :
Edge color when meets the node condition.
no_color :
Edge color when doesn't meet the node condition.
condition_node_params :
Condition node configuration for for graphviz. Example:
.. code-block:: python
{'shape': 'box',
'style': 'filled,rounded',
'fillcolor': '#78bceb'}
leaf_node_params :
Leaf node configuration for graphviz. Example:
.. code-block:: python
{'shape': 'box',
'style': 'filled',
'fillcolor': '#e48038'}
with_stats :
.. versionadded:: 3.0
Controls whether the split statistics should be included.
tree_idx :
.. versionadded:: 3.0
Specify the ordinal index of target tree.
kwargs :
Other keywords passed to graphviz graph_attr, e.g. ``graph [ {key} = {value} ]``
Returns
-------
graph: graphviz.Source
DATA
Optional = typing.Optional
Optional[X] is equivalent to Union[X, None].
PathLike = typing.Union[str, os.PathLike]
Union = typing.Union
Union type; Union[X, Y] means either X or Y.
On Python 3.10 and higher, the | operator
can also be used to denote unions;
X | Y means the same thing to the type checker as Union[X, Y].
To define a union, use e.g. Union[int, str]. Details:
- The arguments must be types and there must be at least one.
- None as an argument is a special case and is replaced by
type(None).
- Unions of unions are flattened, e.g.::
assert Union[Union[int, str], float] == Union[int, str, float]
- Unions of a single argument vanish, e.g.::
assert Union[int] == int # The constructor actually returns int
- Redundant arguments are skipped, e.g.::
assert Union[int, str, int] == Union[int, str]
- When comparing unions, the argument order is ignored, e.g.::
assert Union[int, str] == Union[str, int]
- You cannot subclass or instantiate a union.
- You can use Optional[X] as a shorthand for Union[X, None].
FILE
c:\users\youse\appdata\local\programs\python\python312\lib\site-packages\xgboost\plotting.py
xgboost.set_config
Python Library Documentation: function set_config in module xgboost.config
set_config(**new_config: Any) -> None
Set global configuration.
Global configuration consists of a collection of parameters that can be applied in the
global scope. See :ref:`global_config` for the full list of parameters supported in
the global configuration.
.. versionadded:: 1.4.0
Parameters
----------
new_config: Dict[str, Any]
Keyword arguments representing the parameters and their values
Example
-------
.. code-block:: python
import xgboost as xgb
# Show all messages, including ones pertaining to debugging
xgb.set_config(verbosity=2)
# Get current value of global configuration
# This is a dict containing all parameters in the global configuration,
# including 'verbosity'
config = xgb.get_config()
assert config['verbosity'] == 2
# Example of using the context manager xgb.config_context().
# The context manager will restore the previous value of the global
# configuration upon exiting.
with xgb.config_context(verbosity=0):
# Suppress warning caused by model generated with XGBoost version < 1.0.0
bst = xgb.Booster(model_file='./old_model.bin')
assert xgb.get_config()['verbosity'] == 2 # old value restored
Nested configuration context is also supported:
Example
-------
.. code-block:: python
with xgb.config_context(verbosity=3):
assert xgb.get_config()["verbosity"] == 3
with xgb.config_context(verbosity=2):
assert xgb.get_config()["verbosity"] == 2
xgb.set_config(verbosity=2)
assert xgb.get_config()["verbosity"] == 2
with xgb.config_context(verbosity=3):
assert xgb.get_config()["verbosity"] == 3
xgboost.sklearn
Python Library Documentation: module xgboost.sklearn in xgboost
NAME
xgboost.sklearn - Scikit-Learn Wrapper interface for XGBoost.
CLASSES
builtins.object
XGBRankerMixIn
XGBRanker(XGBRankerMixIn, XGBModel)
sklearn.base.BaseEstimator(sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin, sklearn.utils._metadata_requests._MetadataRequester)
XGBModel
XGBClassifier(sklearn.base.ClassifierMixin, XGBModel)
XGBRFClassifier
XGBRegressor(sklearn.base.RegressorMixin, XGBModel)
XGBRFRegressor
sklearn.base.ClassifierMixin(builtins.object)
XGBClassifier(sklearn.base.ClassifierMixin, XGBModel)
XGBRFClassifier
sklearn.base.RegressorMixin(builtins.object)
XGBRegressor(sklearn.base.RegressorMixin, XGBModel)
XGBRFRegressor
class XGBClassifier(sklearn.base.ClassifierMixin, XGBModel)
| XGBClassifier(*, objective: Union[str, xgboost.sklearn._SklObjWProto, Callable[[Any, Any], Tuple[numpy.ndarray, numpy.ndarray]], NoneType] = 'binary:logistic', **kwargs: Any) -> None
|
| Implementation of the scikit-learn API for XGBoost classification.
| See :doc:`/python/sklearn_estimator` for more information.
|
| Parameters
| ----------
|
| n_estimators : Optional[int]
| Number of boosting rounds.
|
| max_depth : typing.Optional[int]
|
| Maximum tree depth for base learners.
|
| max_leaves : typing.Optional[int]
|
| Maximum number of leaves; 0 indicates no limit.
|
| max_bin : typing.Optional[int]
|
| If using histogram-based algorithm, maximum number of bins per feature
|
| grow_policy : typing.Optional[str]
|
| Tree growing policy.
|
| - depthwise: Favors splitting at nodes closest to the node,
| - lossguide: Favors splitting at nodes with highest loss change.
|
| learning_rate : typing.Optional[float]
|
| Boosting learning rate (xgb's "eta")
|
| verbosity : typing.Optional[int]
|
| The degree of verbosity. Valid values are 0 (silent) - 3 (debug).
|
| objective : typing.Union[str, xgboost.sklearn._SklObjWProto, typing.Callable[[typing.Any, typing.Any], typing.Tuple[numpy.ndarray, numpy.ndarray]], NoneType]
|
| Specify the learning task and the corresponding learning objective or a custom
| objective function to be used.
|
| For custom objective, see :doc:`/tutorials/custom_metric_obj` and
| :ref:`custom-obj-metric` for more information, along with the end note for
| function signatures.
|
| booster: typing.Optional[str]
|
| Specify which booster to use: ``gbtree``, ``gblinear`` or ``dart``.
|
| .. deprecated:: 3.3.0
|
| ``gblinear`` is deprecated and support will be removed in a future release.
|
| tree_method : typing.Optional[str]
|
| Specify which tree method to use. Default to auto. If this parameter is set to
| default, XGBoost will choose the most conservative option available. It's
| recommended to study this option from the parameters document :doc:`tree method
| </treemethod>`
|
| n_jobs : typing.Optional[int]
|
| Number of parallel threads used to run xgboost. When used with other
| Scikit-Learn algorithms like grid search, you may choose which algorithm to
| parallelize and balance the threads. Creating thread contention will
| significantly slow down both algorithms.
|
| gamma : typing.Optional[float]
|
| (min_split_loss) Minimum loss reduction required to make a further partition on
| a leaf node of the tree.
|
| min_child_weight : typing.Optional[float]
|
| Minimum sum of instance weight(hessian) needed in a child.
|
| max_delta_step : typing.Optional[float]
|
| Maximum delta step we allow each tree's weight estimation to be.
|
| subsample : typing.Optional[float]
|
| Subsample ratio of the training instance.
|
| sampling_method : typing.Optional[str]
|
| Sampling method. Used only by the GPU version of ``hist`` tree method.
|
| - ``uniform``: Select random training instances uniformly.
| - ``gradient_based``: Select random training instances with higher probability
| when the gradient and hessian are larger. (cf. CatBoost)
|
| colsample_bytree : typing.Optional[float]
|
| Subsample ratio of columns when constructing each tree.
|
| colsample_bylevel : typing.Optional[float]
|
| Subsample ratio of columns for each level.
|
| colsample_bynode : typing.Optional[float]
|
| Subsample ratio of columns for each split.
|
| reg_alpha : typing.Optional[float]
|
| L1 regularization term on weights (xgb's alpha).
|
| reg_lambda : typing.Optional[float]
|
| L2 regularization term on weights (xgb's lambda).
|
| scale_pos_weight : typing.Optional[float]
| Balancing of positive and negative weights.
|
| base_score : typing.Union[float, typing.List[float], NoneType]
|
| The initial prediction score of all instances, global bias.
|
| random_state : typing.Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType]
|
| Random number seed.
|
| .. note::
|
| Using gblinear booster with shotgun updater is nondeterministic as
| it uses Hogwild algorithm.
|
| missing : float
|
| Value in the data which needs to be present as a missing value. Default to
| :py:data:`numpy.nan`.
|
| num_parallel_tree: typing.Optional[int]
|
| Used for boosting random forest.
|
| monotone_constraints : typing.Union[typing.Dict[str, int], str, NoneType]
|
| Constraint of variable monotonicity. See :doc:`tutorial </tutorials/monotonic>`
| for more information.
|
| interaction_constraints : typing.Union[str, typing.List[typing.Tuple[str]], NoneType]
|
| Constraints for interaction representing permitted interactions. The
| constraints must be specified in the form of a nested list, e.g. ``[[0, 1], [2,
| 3, 4]]``, where each inner list is a group of indices of features that are
| allowed to interact with each other. See :doc:`tutorial
| </tutorials/feature_interaction_constraint>` for more information
|
| importance_type: typing.Optional[str]
|
| The feature importance type for the feature_importances\_ property:
|
| * For tree model, it's either "gain", "weight", "cover", "total_gain" or
| "total_cover".
| * For linear model, only "weight" is defined and it's the normalized
| coefficients without bias.
|
| device : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| Device ordinal, available options are `cpu`, `cuda`, and `gpu`.
|
| validate_parameters : typing.Optional[bool]
|
| Give warnings for unknown parameter.
|
| enable_categorical : bool
|
| See the same parameter of :py:class:`DMatrix` for details.
|
| feature_types : typing.Optional[typing.Sequence[str]]
|
| .. versionadded:: 1.7.0
|
| Used for specifying feature types without constructing a dataframe. See
| the :py:class:`DMatrix` for details.
|
| feature_weights : Optional[ArrayLike]
|
| Weight for each feature, defines the probability of each feature being selected
| when colsample is being used. All values must be greater than 0, otherwise a
| `ValueError` is thrown.
|
| max_cat_to_onehot : Optional[int]
|
| .. versionadded:: 1.6.0
|
| .. note:: This parameter is experimental
|
| A threshold for deciding whether XGBoost should use one-hot encoding based split
| for categorical data. When number of categories is lesser than the threshold
| then one-hot encoding is chosen, otherwise the categories will be partitioned
| into children nodes. Also, `enable_categorical` needs to be set to have
| categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| max_cat_threshold : typing.Optional[int]
|
| .. versionadded:: 1.7.0
|
| .. note:: This parameter is experimental
|
| Maximum number of categories considered for each split. Used only by
| partition-based splits for preventing over-fitting. Also, `enable_categorical`
| needs to be set to have categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| multi_strategy : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| .. note:: This parameter is working-in-progress.
|
| The strategy used for training multi-target models, including multi-target
| regression and multi-class classification. See :doc:`/tutorials/multioutput` for
| more information.
|
| - ``one_output_per_tree``: One model for each target.
| - ``multi_output_tree``: Use multi-target trees.
|
| eval_metric : typing.Union[str, typing.List[typing.Union[str, typing.Callable]], typing.Callable, NoneType]
|
| .. versionadded:: 1.6.0
|
| Metric used for monitoring the training result and early stopping. It can be a
| string or list of strings as names of predefined metric in XGBoost (See
| :doc:`/parameter`), one of the metrics in :py:mod:`sklearn.metrics`, or any
| other user defined metric that looks like `sklearn.metrics`.
|
| If custom objective is also provided, then custom metric should implement the
| corresponding reverse link function.
|
| Unlike the `scoring` parameter commonly used in scikit-learn, when a callable
| object is provided, it's assumed to be a cost function and by default XGBoost
| will minimize the result during early stopping.
|
| For advanced usage on Early stopping like directly choosing to maximize instead
| of minimize, see :py:obj:`xgboost.callback.EarlyStopping`.
|
| See :doc:`/tutorials/custom_metric_obj` and :ref:`custom-obj-metric` for more
| information.
|
| .. code-block:: python
|
| from sklearn.datasets import load_diabetes
| from sklearn.metrics import mean_absolute_error
| X, y = load_diabetes(return_X_y=True)
| reg = xgb.XGBRegressor(
| tree_method="hist",
| eval_metric=mean_absolute_error,
| )
| reg.fit(X, y, eval_set=[(X, y)])
|
| early_stopping_rounds : typing.Optional[int]
|
| .. versionadded:: 1.6.0
|
| - Activates early stopping. Validation metric needs to improve at least once in
| every **early_stopping_rounds** round(s) to continue training. Requires at
| least one item in **eval_set** in :py:meth:`fit`.
|
| - If early stopping occurs, the model will have two additional attributes:
| :py:attr:`best_score` and :py:attr:`best_iteration`. These are used by the
| :py:meth:`predict` and :py:meth:`apply` methods to determine the optimal
| number of trees during inference. If users want to access the full model
| (including trees built after early stopping), they can specify the
| `iteration_range` in these inference methods. In addition, other utilities
| like model plotting can also use the entire model.
|
| - If you prefer to discard the trees after `best_iteration`, consider using the
| callback function :py:class:`xgboost.callback.EarlyStopping`.
|
| - If there's more than one item in **eval_set**, the last entry will be used for
| early stopping. If there's more than one metric in **eval_metric**, the last
| metric will be used for early stopping.
|
| callbacks : typing.Optional[typing.List[xgboost.callback.TrainingCallback]]
|
| List of callback functions that are applied at end of each iteration.
| It is possible to use predefined callbacks by using
| :ref:`Callback API <callback_api>`.
|
| .. note::
|
| States in callback are not preserved during training, which means callback
| objects can not be reused for multiple training sessions without
| reinitialization or deepcopy.
|
| .. code-block:: python
|
| for params in parameters_grid:
| # be sure to (re)initialize the callbacks before each run
| callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
| reg = xgboost.XGBRegressor(**params, callbacks=callbacks)
| reg.fit(X, y)
|
| kwargs : typing.Optional[typing.Any]
|
| Keyword arguments for XGBoost Booster object. Full documentation of parameters
| can be found :doc:`here </parameter>`.
| Attempting to set a parameter via the constructor args and \*\*kwargs
| dict simultaneously will result in a TypeError.
|
| .. note:: \*\*kwargs unsupported by scikit-learn
|
| \*\*kwargs is unsupported by scikit-learn. We do not guarantee
| that parameters passed via this argument will interact properly
| with scikit-learn.
|
| .. note:: Custom objective function
|
| A custom objective function can be provided for the ``objective``
| parameter. In this case, it should have the signature ``objective(y_true,
| y_pred) -> [grad, hess]`` or ``objective(y_true, y_pred, *, sample_weight)
| -> [grad, hess]``:
|
| y_true: array_like of shape [n_samples]
| The target values
| y_pred: array_like of shape [n_samples]
| The predicted values
| sample_weight :
| Optional sample weights.
|
| grad: array_like of shape [n_samples]
| The value of the gradient for each sample point.
| hess: array_like of shape [n_samples]
| The value of the second derivative for each sample point
|
| Note that, if the custom objective produces negative values for
| the Hessian, these will be clipped. If the objective is non-convex,
| one might also consider using the expected Hessian (Fisher
| information) instead.
|
| Method resolution order:
| XGBClassifier
| sklearn.base.ClassifierMixin
| XGBModel
| sklearn.base.BaseEstimator
| sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin
| sklearn.utils._metadata_requests._MetadataRequester
| builtins.object
|
| Methods defined here:
|
| __init__(self, *, objective: Union[str, xgboost.sklearn._SklObjWProto, Callable[[Any, Any], Tuple[numpy.ndarray, numpy.ndarray]], NoneType] = 'binary:logistic', **kwargs: Any) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| __sklearn_tags__(self) -> object
|
| fit(self, X: Any, y: Any, *, sample_weight: Optional[Any] = None, base_margin: Optional[Any] = None, eval_set: Optional[Sequence[Tuple[Any, Any]]] = None, verbose: Union[bool, int, NoneType] = True, xgb_model: Union[xgboost.core.Booster, str, xgboost.sklearn.XGBModel, NoneType] = None, sample_weight_eval_set: Optional[Sequence[Any]] = None, base_margin_eval_set: Optional[Sequence[Any]] = None, feature_weights: Optional[Any] = None) -> 'XGBClassifier'
| Fit gradient boosting classifier.
|
| Note that calling ``fit()`` multiple times will cause the model object to be
| re-fit from scratch. To resume training from a previous checkpoint, explicitly
| pass ``xgb_model`` argument.
|
| Parameters
| ----------
| X :
| Input feature matrix. See :ref:`py-data` for a list of supported types.
|
| When the ``tree_method`` is set to ``hist``, internally, the
| :py:class:`QuantileDMatrix` will be used instead of the :py:class:`DMatrix`
| for conserving memory. However, this has performance implications when the
| device of input data is not matched with algorithm. For instance, if the
| input is a numpy array on CPU but ``cuda`` is used for training, then the
| data is first processed on CPU then transferred to GPU.
| y :
| Labels
| sample_weight :
| instance weights
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| eval_set :
| A list of (X, y) tuple pairs to use as validation sets, for which
| metrics will be computed.
| Validation metrics will help us track the performance of the model.
|
| verbose :
| If `verbose` is True and an evaluation set is used, the evaluation metric
| measured on the validation set is printed to stdout at each boosting stage.
| If `verbose` is an integer, the evaluation metric is printed at each
| `verbose` boosting stage. The last boosting stage / the boosting stage found
| by using `early_stopping_rounds` is also printed.
| xgb_model :
| file name of stored XGBoost model or 'Booster' instance XGBoost model to be
| loaded before training (allows training continuation).
| sample_weight_eval_set :
| A list of the form [L_1, L_2, ..., L_n], where each L_i is an array like
| object storing instance weights for the i-th validation set.
| base_margin_eval_set :
| A list of the form [M_1, M_2, ..., M_n], where each M_i is an array like
| object storing base margin for the i-th validation set.
| feature_weights :
|
| .. deprecated:: 3.0.0
|
| Use `feature_weights` in :py:meth:`__init__` or :py:meth:`set_params`
| instead.
|
| predict(self, X: Any, *, output_margin: bool = False, validate_features: bool = True, base_margin: Optional[Any] = None, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> Any
| Predict with `X`. If the model is trained with early stopping, then
| :py:attr:`best_iteration` is used automatically. The estimator uses
| `inplace_predict` by default and falls back to using :py:class:`DMatrix` if
| devices between the data and the estimator don't match.
|
| .. note:: This function is only thread safe for `gbtree` and `dart`.
|
| Parameters
| ----------
| X :
| Data to predict with. See :ref:`py-data` for a list of supported types.
| output_margin :
| Whether to output the raw untransformed margin value.
| validate_features :
| When this is True, validate that the Booster's and data's feature_names are
| identical. Otherwise, it is assumed that the feature_names are the same.
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying ``iteration_range=(10,
| 20)``, then only the forests built during [10, 20) (half open set) rounds
| are used in this prediction.
|
| .. versionadded:: 1.4.0
|
| Returns
| -------
| prediction
|
| predict_proba(self, X: Any, validate_features: bool = True, base_margin: Optional[Any] = None, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> numpy.ndarray
| Predict the probability of each `X` example being of a given class. If the
| model is trained with early stopping, then :py:attr:`best_iteration` is used
| automatically. The estimator uses `inplace_predict` by default and falls back to
| using :py:class:`DMatrix` if devices between the data and the estimator don't
| match.
|
| .. note:: This function is only thread safe for `gbtree` and `dart`.
|
| Parameters
| ----------
| X :
| Feature matrix. See :ref:`py-data` for a list of supported types.
| validate_features :
| When this is True, validate that the Booster's and data's feature_names are
| identical. Otherwise, it is assumed that the feature_names are the same.
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying `iteration_range=(10,
| 20)`, then only the forests built during [10, 20) (half open set) rounds are
| used in this prediction.
|
| Returns
| -------
| prediction :
| a numpy array of shape array-like of shape (n_samples, n_classes) with the
| probability of each data example being of a given class.
|
| set_fit_request(self: xgboost.sklearn.XGBClassifier, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', base_margin_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', feature_weights: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', verbose: Union[bool, NoneType, str] = '$UNCHANGED$', xgb_model: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBClassifier from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``fit`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``fit`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``fit``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``fit``.
|
| base_margin_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin_eval_set`` parameter in ``fit``.
|
| eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``eval_set`` parameter in ``fit``.
|
| feature_weights : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``feature_weights`` parameter in ``fit``.
|
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``fit``.
|
| sample_weight_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight_eval_set`` parameter in ``fit``.
|
| verbose : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``verbose`` parameter in ``fit``.
|
| xgb_model : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``xgb_model`` parameter in ``fit``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_predict_proba_request(self: xgboost.sklearn.XGBClassifier, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', iteration_range: Union[bool, NoneType, str] = '$UNCHANGED$', validate_features: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBClassifier from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``predict_proba`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``predict_proba`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``predict_proba``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``predict_proba``.
|
| iteration_range : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``iteration_range`` parameter in ``predict_proba``.
|
| validate_features : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``validate_features`` parameter in ``predict_proba``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_predict_request(self: xgboost.sklearn.XGBClassifier, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', iteration_range: Union[bool, NoneType, str] = '$UNCHANGED$', output_margin: Union[bool, NoneType, str] = '$UNCHANGED$', validate_features: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBClassifier from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``predict`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``predict`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``predict``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``predict``.
|
| iteration_range : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``iteration_range`` parameter in ``predict``.
|
| output_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``output_margin`` parameter in ``predict``.
|
| validate_features : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``validate_features`` parameter in ``predict``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_score_request(self: xgboost.sklearn.XGBClassifier, *, sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBClassifier from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``score`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``score`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``score``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``score``.
|
| Returns
| -------
| self : object
| The updated object.
|
| ----------------------------------------------------------------------
| Readonly properties defined here:
|
| classes_
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.ClassifierMixin:
|
| score(self, X, y, sample_weight=None)
| Return the mean accuracy on the given test data and labels.
|
| In multi-label classification, this is the subset accuracy
| which is a harsh metric since you require for each sample that
| each label set be correctly predicted.
|
| Parameters
| ----------
| X : array-like of shape (n_samples, n_features)
| Test samples.
|
| y : array-like of shape (n_samples,) or (n_samples, n_outputs)
| True labels for `X`.
|
| sample_weight : array-like of shape (n_samples,), default=None
| Sample weights.
|
| Returns
| -------
| score : float
| Mean accuracy of ``self.predict(X)`` w.r.t. `y`.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from sklearn.base.ClassifierMixin:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Methods inherited from XGBModel:
|
| __sklearn_is_fitted__(self) -> bool
|
| apply(self, X: Any, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> numpy.ndarray
| Return the predicted leaf every tree for each sample. If the model is trained
| with early stopping, then :py:attr:`best_iteration` is used automatically.
|
| Parameters
| ----------
| X :
| Input features matrix. See :ref:`py-data` for a list of supported types.
|
| iteration_range :
| See :py:meth:`predict`.
|
| Returns
| -------
| X_leaves : array_like, shape=[n_samples, n_trees]
| For each datapoint x in X and for each tree, return the index of the
| leaf x ends up in. Leaves are numbered within
| ``[0; 2**(self.max_depth+1))``, possibly with gaps in the numbering.
|
| evals_result(self) -> Dict[str, Dict[str, List[float]]]
| Return the evaluation results.
|
| If **eval_set** is passed to the :py:meth:`fit` function, you can call
| ``evals_result()`` to get evaluation results for all passed **eval_sets**. When
| **eval_metric** is also passed to the :py:meth:`fit` function, the
| **evals_result** will contain the **eval_metrics** passed to the :py:meth:`fit`
| function.
|
| The returned evaluation result is a dictionary:
|
| .. code-block:: python
|
| {'validation_0': {'logloss': ['0.604835', '0.531479']},
| 'validation_1': {'logloss': ['0.41965', '0.17686']}}
|
| Returns
| -------
| evals_result
|
| get_booster(self) -> xgboost.core.Booster
| Get the underlying xgboost Booster of this model.
|
| This will raise an exception when fit was not called
|
| Returns
| -------
| booster : a xgboost booster of underlying model
|
| get_num_boosting_rounds(self) -> int
| Gets the number of xgboost boosting rounds.
|
| get_params(self, deep: bool = True) -> Dict[str, Any]
| Get parameters.
|
| get_xgb_params(self) -> Dict[str, Any]
| Get xgboost specific parameters.
|
| load_model(self, fname: Union[os.PathLike[~AnyStr], bytearray, str]) -> None
| Load the model from a file or a bytearray.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| model.load_model("model.json")
|
| # or
| model.save_model("model.ubj")
| model.load_model("model.ubj")
|
| # or
| buf = model.save_raw()
| model.load_model(buf)
|
| Parameters
| ----------
| fname :
| Input file name or memory buffer(see also save_raw)
|
| save_model(self, fname: Union[str, os.PathLike]) -> None
| Save the model to a file.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| # or
| model.save_model("model.ubj")
|
| Parameters
| ----------
| fname :
| Output file name
|
| set_params(self, **params: Any) -> 'XGBModel'
| Set the parameters of this estimator. Modification of the sklearn method to
| allow unknown kwargs. This allows using the full range of xgboost
| parameters that are not defined as member variables in sklearn grid
| search.
|
| Returns
| -------
| self
|
| ----------------------------------------------------------------------
| Readonly properties inherited from XGBModel:
|
| best_iteration
| The best iteration obtained by early stopping. This attribute is 0-based,
| for instance if the best iteration is the first round, then best_iteration is 0.
|
| best_score
| The best score obtained by early stopping.
|
| coef_
| Coefficients property
|
| .. note:: Coefficients are defined only for linear learners
|
| Coefficients are only defined when the linear model is chosen as
| base learner (`booster=gblinear`). It is not defined for other base
| learner types, such as tree learners (`booster=gbtree`).
|
| Returns
| -------
| coef_ : array of shape ``[n_features]`` or ``[n_classes, n_features]``
|
| feature_importances_
| Feature importances property, return depends on `importance_type`
| parameter. When model trained with multi-class/multi-label/multi-target dataset,
| the feature importance is "averaged" over all targets. The "average" is defined
| based on the importance type. For instance, if the importance type is
| "total_gain", then the score is sum of loss change for each split from all
| trees.
|
| Returns
| -------
| feature_importances_ : array of shape ``[n_features]`` except for multi-class
| linear model, which returns an array with shape `(n_features, n_classes)`
|
| feature_names_in_
| Names of features seen during :py:meth:`fit`. Defined only when `X` has
| feature names that are all strings.
|
| intercept_
| Intercept (bias) property
|
| For tree-based model, the returned value is the `base_score`.
|
| Returns
| -------
| intercept_ : array of shape ``(1,)`` or ``[n_classes]``
|
| n_features_in_
| Number of features seen during :py:meth:`fit`.
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.BaseEstimator:
|
| __getstate__(self)
| Helper for pickle.
|
| __repr__(self, N_CHAR_MAX=700)
| Return repr(self).
|
| __setstate__(self, state)
|
| __sklearn_clone__(self)
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| get_metadata_routing(self)
| Get metadata routing of this object.
|
| Please check :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| Returns
| -------
| routing : MetadataRequest
| A :class:`~sklearn.utils.metadata_routing.MetadataRequest` encapsulating
| routing information.
|
| ----------------------------------------------------------------------
| Class methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| __init_subclass__(**kwargs)
| Set the ``set_{method}_request`` methods.
|
| This uses PEP-487 [1]_ to set the ``set_{method}_request`` methods. It
| looks for the information available in the set default values which are
| set using ``__metadata_request__*`` class attributes, or inferred
| from method signatures.
|
| The ``__metadata_request__*`` class attributes are used when a method
| does not explicitly accept a metadata through its arguments or if the
| developer would like to specify a request value for those metadata
| which are different from the default ``None``.
|
| References
| ----------
| .. [1] https://www.python.org/dev/peps/pep-0487
class XGBModel(sklearn.base.BaseEstimator)
| XGBModel(*, max_depth: Optional[int] = None, max_leaves: Optional[int] = None, max_bin: Optional[int] = None, grow_policy: Optional[str] = None, learning_rate: Optional[float] = None, n_estimators: Optional[int] = None, verbosity: Optional[int] = None, objective: Union[str, xgboost.sklearn._SklObjWProto, Callable[[Any, Any], Tuple[numpy.ndarray, numpy.ndarray]], NoneType] = None, booster: Optional[str] = None, tree_method: Optional[str] = None, n_jobs: Optional[int] = None, gamma: Optional[float] = None, min_child_weight: Optional[float] = None, max_delta_step: Optional[float] = None, subsample: Optional[float] = None, sampling_method: Optional[str] = None, colsample_bytree: Optional[float] = None, colsample_bylevel: Optional[float] = None, colsample_bynode: Optional[float] = None, reg_alpha: Optional[float] = None, reg_lambda: Optional[float] = None, scale_pos_weight: Optional[float] = None, base_score: Union[float, List[float], NoneType] = None, random_state: Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType] = None, missing: float = nan, num_parallel_tree: Optional[int] = None, monotone_constraints: Union[Dict[str, int], str, NoneType] = None, interaction_constraints: Union[str, Sequence[Sequence[str]], NoneType] = None, importance_type: Optional[str] = None, device: Optional[str] = None, validate_parameters: Optional[bool] = None, enable_categorical: bool = True, feature_types: Optional[Sequence[str]] = None, feature_weights: Optional[Any] = None, max_cat_to_onehot: Optional[int] = None, max_cat_threshold: Optional[int] = None, multi_strategy: Optional[str] = None, eval_metric: Union[str, List[Union[str, Callable]], Callable, NoneType] = None, early_stopping_rounds: Optional[int] = None, callbacks: Optional[List[xgboost.callback.TrainingCallback]] = None, **kwargs: Any) -> None
|
| Implementation of the Scikit-Learn API for XGBoost.
| See :doc:`/python/sklearn_estimator` for more information.
|
| Parameters
| ----------
|
| n_estimators : typing.Optional[int]
| Number of gradient boosted trees. Equivalent to number of boosting
| rounds.
|
| max_depth : typing.Optional[int]
|
| Maximum tree depth for base learners.
|
| max_leaves : typing.Optional[int]
|
| Maximum number of leaves; 0 indicates no limit.
|
| max_bin : typing.Optional[int]
|
| If using histogram-based algorithm, maximum number of bins per feature
|
| grow_policy : typing.Optional[str]
|
| Tree growing policy.
|
| - depthwise: Favors splitting at nodes closest to the node,
| - lossguide: Favors splitting at nodes with highest loss change.
|
| learning_rate : typing.Optional[float]
|
| Boosting learning rate (xgb's "eta")
|
| verbosity : typing.Optional[int]
|
| The degree of verbosity. Valid values are 0 (silent) - 3 (debug).
|
| objective : typing.Union[str, xgboost.sklearn._SklObjWProto, typing.Callable[[typing.Any, typing.Any], typing.Tuple[numpy.ndarray, numpy.ndarray]], NoneType]
|
| Specify the learning task and the corresponding learning objective or a custom
| objective function to be used.
|
| For custom objective, see :doc:`/tutorials/custom_metric_obj` and
| :ref:`custom-obj-metric` for more information, along with the end note for
| function signatures.
|
| booster: typing.Optional[str]
|
| Specify which booster to use: ``gbtree``, ``gblinear`` or ``dart``.
|
| .. deprecated:: 3.3.0
|
| ``gblinear`` is deprecated and support will be removed in a future release.
|
| tree_method : typing.Optional[str]
|
| Specify which tree method to use. Default to auto. If this parameter is set to
| default, XGBoost will choose the most conservative option available. It's
| recommended to study this option from the parameters document :doc:`tree method
| </treemethod>`
|
| n_jobs : typing.Optional[int]
|
| Number of parallel threads used to run xgboost. When used with other
| Scikit-Learn algorithms like grid search, you may choose which algorithm to
| parallelize and balance the threads. Creating thread contention will
| significantly slow down both algorithms.
|
| gamma : typing.Optional[float]
|
| (min_split_loss) Minimum loss reduction required to make a further partition on
| a leaf node of the tree.
|
| min_child_weight : typing.Optional[float]
|
| Minimum sum of instance weight(hessian) needed in a child.
|
| max_delta_step : typing.Optional[float]
|
| Maximum delta step we allow each tree's weight estimation to be.
|
| subsample : typing.Optional[float]
|
| Subsample ratio of the training instance.
|
| sampling_method : typing.Optional[str]
|
| Sampling method. Used only by the GPU version of ``hist`` tree method.
|
| - ``uniform``: Select random training instances uniformly.
| - ``gradient_based``: Select random training instances with higher probability
| when the gradient and hessian are larger. (cf. CatBoost)
|
| colsample_bytree : typing.Optional[float]
|
| Subsample ratio of columns when constructing each tree.
|
| colsample_bylevel : typing.Optional[float]
|
| Subsample ratio of columns for each level.
|
| colsample_bynode : typing.Optional[float]
|
| Subsample ratio of columns for each split.
|
| reg_alpha : typing.Optional[float]
|
| L1 regularization term on weights (xgb's alpha).
|
| reg_lambda : typing.Optional[float]
|
| L2 regularization term on weights (xgb's lambda).
|
| scale_pos_weight : typing.Optional[float]
| Balancing of positive and negative weights.
|
| base_score : typing.Union[float, typing.List[float], NoneType]
|
| The initial prediction score of all instances, global bias.
|
| random_state : typing.Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType]
|
| Random number seed.
|
| .. note::
|
| Using gblinear booster with shotgun updater is nondeterministic as
| it uses Hogwild algorithm.
|
| missing : float
|
| Value in the data which needs to be present as a missing value. Default to
| :py:data:`numpy.nan`.
|
| num_parallel_tree: typing.Optional[int]
|
| Used for boosting random forest.
|
| monotone_constraints : typing.Union[typing.Dict[str, int], str, NoneType]
|
| Constraint of variable monotonicity. See :doc:`tutorial </tutorials/monotonic>`
| for more information.
|
| interaction_constraints : typing.Union[str, typing.List[typing.Tuple[str]], NoneType]
|
| Constraints for interaction representing permitted interactions. The
| constraints must be specified in the form of a nested list, e.g. ``[[0, 1], [2,
| 3, 4]]``, where each inner list is a group of indices of features that are
| allowed to interact with each other. See :doc:`tutorial
| </tutorials/feature_interaction_constraint>` for more information
|
| importance_type: typing.Optional[str]
|
| The feature importance type for the feature_importances\_ property:
|
| * For tree model, it's either "gain", "weight", "cover", "total_gain" or
| "total_cover".
| * For linear model, only "weight" is defined and it's the normalized
| coefficients without bias.
|
| device : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| Device ordinal, available options are `cpu`, `cuda`, and `gpu`.
|
| validate_parameters : typing.Optional[bool]
|
| Give warnings for unknown parameter.
|
| enable_categorical : bool
|
| See the same parameter of :py:class:`DMatrix` for details.
|
| feature_types : typing.Optional[typing.Sequence[str]]
|
| .. versionadded:: 1.7.0
|
| Used for specifying feature types without constructing a dataframe. See
| the :py:class:`DMatrix` for details.
|
| feature_weights : Optional[ArrayLike]
|
| Weight for each feature, defines the probability of each feature being selected
| when colsample is being used. All values must be greater than 0, otherwise a
| `ValueError` is thrown.
|
| max_cat_to_onehot : Optional[int]
|
| .. versionadded:: 1.6.0
|
| .. note:: This parameter is experimental
|
| A threshold for deciding whether XGBoost should use one-hot encoding based split
| for categorical data. When number of categories is lesser than the threshold
| then one-hot encoding is chosen, otherwise the categories will be partitioned
| into children nodes. Also, `enable_categorical` needs to be set to have
| categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| max_cat_threshold : typing.Optional[int]
|
| .. versionadded:: 1.7.0
|
| .. note:: This parameter is experimental
|
| Maximum number of categories considered for each split. Used only by
| partition-based splits for preventing over-fitting. Also, `enable_categorical`
| needs to be set to have categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| multi_strategy : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| .. note:: This parameter is working-in-progress.
|
| The strategy used for training multi-target models, including multi-target
| regression and multi-class classification. See :doc:`/tutorials/multioutput` for
| more information.
|
| - ``one_output_per_tree``: One model for each target.
| - ``multi_output_tree``: Use multi-target trees.
|
| eval_metric : typing.Union[str, typing.List[typing.Union[str, typing.Callable]], typing.Callable, NoneType]
|
| .. versionadded:: 1.6.0
|
| Metric used for monitoring the training result and early stopping. It can be a
| string or list of strings as names of predefined metric in XGBoost (See
| :doc:`/parameter`), one of the metrics in :py:mod:`sklearn.metrics`, or any
| other user defined metric that looks like `sklearn.metrics`.
|
| If custom objective is also provided, then custom metric should implement the
| corresponding reverse link function.
|
| Unlike the `scoring` parameter commonly used in scikit-learn, when a callable
| object is provided, it's assumed to be a cost function and by default XGBoost
| will minimize the result during early stopping.
|
| For advanced usage on Early stopping like directly choosing to maximize instead
| of minimize, see :py:obj:`xgboost.callback.EarlyStopping`.
|
| See :doc:`/tutorials/custom_metric_obj` and :ref:`custom-obj-metric` for more
| information.
|
| .. code-block:: python
|
| from sklearn.datasets import load_diabetes
| from sklearn.metrics import mean_absolute_error
| X, y = load_diabetes(return_X_y=True)
| reg = xgb.XGBRegressor(
| tree_method="hist",
| eval_metric=mean_absolute_error,
| )
| reg.fit(X, y, eval_set=[(X, y)])
|
| early_stopping_rounds : typing.Optional[int]
|
| .. versionadded:: 1.6.0
|
| - Activates early stopping. Validation metric needs to improve at least once in
| every **early_stopping_rounds** round(s) to continue training. Requires at
| least one item in **eval_set** in :py:meth:`fit`.
|
| - If early stopping occurs, the model will have two additional attributes:
| :py:attr:`best_score` and :py:attr:`best_iteration`. These are used by the
| :py:meth:`predict` and :py:meth:`apply` methods to determine the optimal
| number of trees during inference. If users want to access the full model
| (including trees built after early stopping), they can specify the
| `iteration_range` in these inference methods. In addition, other utilities
| like model plotting can also use the entire model.
|
| - If you prefer to discard the trees after `best_iteration`, consider using the
| callback function :py:class:`xgboost.callback.EarlyStopping`.
|
| - If there's more than one item in **eval_set**, the last entry will be used for
| early stopping. If there's more than one metric in **eval_metric**, the last
| metric will be used for early stopping.
|
| callbacks : typing.Optional[typing.List[xgboost.callback.TrainingCallback]]
|
| List of callback functions that are applied at end of each iteration.
| It is possible to use predefined callbacks by using
| :ref:`Callback API <callback_api>`.
|
| .. note::
|
| States in callback are not preserved during training, which means callback
| objects can not be reused for multiple training sessions without
| reinitialization or deepcopy.
|
| .. code-block:: python
|
| for params in parameters_grid:
| # be sure to (re)initialize the callbacks before each run
| callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
| reg = xgboost.XGBRegressor(**params, callbacks=callbacks)
| reg.fit(X, y)
|
| kwargs : typing.Optional[typing.Any]
|
| Keyword arguments for XGBoost Booster object. Full documentation of parameters
| can be found :doc:`here </parameter>`.
| Attempting to set a parameter via the constructor args and \*\*kwargs
| dict simultaneously will result in a TypeError.
|
| .. note:: \*\*kwargs unsupported by scikit-learn
|
| \*\*kwargs is unsupported by scikit-learn. We do not guarantee
| that parameters passed via this argument will interact properly
| with scikit-learn.
|
| .. note:: Custom objective function
|
| A custom objective function can be provided for the ``objective``
| parameter. In this case, it should have the signature ``objective(y_true,
| y_pred) -> [grad, hess]`` or ``objective(y_true, y_pred, *, sample_weight)
| -> [grad, hess]``:
|
| y_true: array_like of shape [n_samples]
| The target values
| y_pred: array_like of shape [n_samples]
| The predicted values
| sample_weight :
| Optional sample weights.
|
| grad: array_like of shape [n_samples]
| The value of the gradient for each sample point.
| hess: array_like of shape [n_samples]
| The value of the second derivative for each sample point
|
| Note that, if the custom objective produces negative values for
| the Hessian, these will be clipped. If the objective is non-convex,
| one might also consider using the expected Hessian (Fisher
| information) instead.
|
| Method resolution order:
| XGBModel
| sklearn.base.BaseEstimator
| sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin
| sklearn.utils._metadata_requests._MetadataRequester
| builtins.object
|
| Methods defined here:
|
| __init__(self, *, max_depth: Optional[int] = None, max_leaves: Optional[int] = None, max_bin: Optional[int] = None, grow_policy: Optional[str] = None, learning_rate: Optional[float] = None, n_estimators: Optional[int] = None, verbosity: Optional[int] = None, objective: Union[str, xgboost.sklearn._SklObjWProto, Callable[[Any, Any], Tuple[numpy.ndarray, numpy.ndarray]], NoneType] = None, booster: Optional[str] = None, tree_method: Optional[str] = None, n_jobs: Optional[int] = None, gamma: Optional[float] = None, min_child_weight: Optional[float] = None, max_delta_step: Optional[float] = None, subsample: Optional[float] = None, sampling_method: Optional[str] = None, colsample_bytree: Optional[float] = None, colsample_bylevel: Optional[float] = None, colsample_bynode: Optional[float] = None, reg_alpha: Optional[float] = None, reg_lambda: Optional[float] = None, scale_pos_weight: Optional[float] = None, base_score: Union[float, List[float], NoneType] = None, random_state: Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType] = None, missing: float = nan, num_parallel_tree: Optional[int] = None, monotone_constraints: Union[Dict[str, int], str, NoneType] = None, interaction_constraints: Union[str, Sequence[Sequence[str]], NoneType] = None, importance_type: Optional[str] = None, device: Optional[str] = None, validate_parameters: Optional[bool] = None, enable_categorical: bool = True, feature_types: Optional[Sequence[str]] = None, feature_weights: Optional[Any] = None, max_cat_to_onehot: Optional[int] = None, max_cat_threshold: Optional[int] = None, multi_strategy: Optional[str] = None, eval_metric: Union[str, List[Union[str, Callable]], Callable, NoneType] = None, early_stopping_rounds: Optional[int] = None, callbacks: Optional[List[xgboost.callback.TrainingCallback]] = None, **kwargs: Any) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| __sklearn_is_fitted__(self) -> bool
|
| __sklearn_tags__(self) -> object
|
| apply(self, X: Any, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> numpy.ndarray
| Return the predicted leaf every tree for each sample. If the model is trained
| with early stopping, then :py:attr:`best_iteration` is used automatically.
|
| Parameters
| ----------
| X :
| Input features matrix. See :ref:`py-data` for a list of supported types.
|
| iteration_range :
| See :py:meth:`predict`.
|
| Returns
| -------
| X_leaves : array_like, shape=[n_samples, n_trees]
| For each datapoint x in X and for each tree, return the index of the
| leaf x ends up in. Leaves are numbered within
| ``[0; 2**(self.max_depth+1))``, possibly with gaps in the numbering.
|
| evals_result(self) -> Dict[str, Dict[str, List[float]]]
| Return the evaluation results.
|
| If **eval_set** is passed to the :py:meth:`fit` function, you can call
| ``evals_result()`` to get evaluation results for all passed **eval_sets**. When
| **eval_metric** is also passed to the :py:meth:`fit` function, the
| **evals_result** will contain the **eval_metrics** passed to the :py:meth:`fit`
| function.
|
| The returned evaluation result is a dictionary:
|
| .. code-block:: python
|
| {'validation_0': {'logloss': ['0.604835', '0.531479']},
| 'validation_1': {'logloss': ['0.41965', '0.17686']}}
|
| Returns
| -------
| evals_result
|
| fit(self, X: Any, y: Any, *, sample_weight: Optional[Any] = None, base_margin: Optional[Any] = None, eval_set: Optional[Sequence[Tuple[Any, Any]]] = None, verbose: Union[bool, int, NoneType] = True, xgb_model: Union[xgboost.core.Booster, ForwardRef('XGBModel'), str, NoneType] = None, sample_weight_eval_set: Optional[Sequence[Any]] = None, base_margin_eval_set: Optional[Sequence[Any]] = None, feature_weights: Optional[Any] = None) -> 'XGBModel'
| Fit gradient boosting model.
|
| Note that calling ``fit()`` multiple times will cause the model object to be
| re-fit from scratch. To resume training from a previous checkpoint, explicitly
| pass ``xgb_model`` argument.
|
| Parameters
| ----------
| X :
| Input feature matrix. See :ref:`py-data` for a list of supported types.
|
| When the ``tree_method`` is set to ``hist``, internally, the
| :py:class:`QuantileDMatrix` will be used instead of the :py:class:`DMatrix`
| for conserving memory. However, this has performance implications when the
| device of input data is not matched with algorithm. For instance, if the
| input is a numpy array on CPU but ``cuda`` is used for training, then the
| data is first processed on CPU then transferred to GPU.
| y :
| Labels
| sample_weight :
| instance weights
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| eval_set :
| A list of (X, y) tuple pairs to use as validation sets, for which
| metrics will be computed.
| Validation metrics will help us track the performance of the model.
|
| verbose :
| If `verbose` is True and an evaluation set is used, the evaluation metric
| measured on the validation set is printed to stdout at each boosting stage.
| If `verbose` is an integer, the evaluation metric is printed at each
| `verbose` boosting stage. The last boosting stage / the boosting stage found
| by using `early_stopping_rounds` is also printed.
| xgb_model :
| file name of stored XGBoost model or 'Booster' instance XGBoost model to be
| loaded before training (allows training continuation).
| sample_weight_eval_set :
| A list of the form [L_1, L_2, ..., L_n], where each L_i is an array like
| object storing instance weights for the i-th validation set.
| base_margin_eval_set :
| A list of the form [M_1, M_2, ..., M_n], where each M_i is an array like
| object storing base margin for the i-th validation set.
| feature_weights :
|
| .. deprecated:: 3.0.0
|
| Use `feature_weights` in :py:meth:`__init__` or :py:meth:`set_params`
| instead.
|
| get_booster(self) -> xgboost.core.Booster
| Get the underlying xgboost Booster of this model.
|
| This will raise an exception when fit was not called
|
| Returns
| -------
| booster : a xgboost booster of underlying model
|
| get_num_boosting_rounds(self) -> int
| Gets the number of xgboost boosting rounds.
|
| get_params(self, deep: bool = True) -> Dict[str, Any]
| Get parameters.
|
| get_xgb_params(self) -> Dict[str, Any]
| Get xgboost specific parameters.
|
| load_model(self, fname: Union[os.PathLike[~AnyStr], bytearray, str]) -> None
| Load the model from a file or a bytearray.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| model.load_model("model.json")
|
| # or
| model.save_model("model.ubj")
| model.load_model("model.ubj")
|
| # or
| buf = model.save_raw()
| model.load_model(buf)
|
| Parameters
| ----------
| fname :
| Input file name or memory buffer(see also save_raw)
|
| predict(self, X: Any, *, output_margin: bool = False, validate_features: bool = True, base_margin: Optional[Any] = None, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> Any
| Predict with `X`. If the model is trained with early stopping, then
| :py:attr:`best_iteration` is used automatically. The estimator uses
| `inplace_predict` by default and falls back to using :py:class:`DMatrix` if
| devices between the data and the estimator don't match.
|
| .. note:: This function is only thread safe for `gbtree` and `dart`.
|
| Parameters
| ----------
| X :
| Data to predict with. See :ref:`py-data` for a list of supported types.
| output_margin :
| Whether to output the raw untransformed margin value.
| validate_features :
| When this is True, validate that the Booster's and data's feature_names are
| identical. Otherwise, it is assumed that the feature_names are the same.
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying ``iteration_range=(10,
| 20)``, then only the forests built during [10, 20) (half open set) rounds
| are used in this prediction.
|
| .. versionadded:: 1.4.0
|
| Returns
| -------
| prediction
|
| save_model(self, fname: Union[str, os.PathLike]) -> None
| Save the model to a file.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| # or
| model.save_model("model.ubj")
|
| Parameters
| ----------
| fname :
| Output file name
|
| set_fit_request(self: xgboost.sklearn.XGBModel, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', base_margin_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', feature_weights: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', verbose: Union[bool, NoneType, str] = '$UNCHANGED$', xgb_model: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBModel from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``fit`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``fit`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``fit``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``fit``.
|
| base_margin_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin_eval_set`` parameter in ``fit``.
|
| eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``eval_set`` parameter in ``fit``.
|
| feature_weights : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``feature_weights`` parameter in ``fit``.
|
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``fit``.
|
| sample_weight_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight_eval_set`` parameter in ``fit``.
|
| verbose : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``verbose`` parameter in ``fit``.
|
| xgb_model : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``xgb_model`` parameter in ``fit``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_params(self, **params: Any) -> 'XGBModel'
| Set the parameters of this estimator. Modification of the sklearn method to
| allow unknown kwargs. This allows using the full range of xgboost
| parameters that are not defined as member variables in sklearn grid
| search.
|
| Returns
| -------
| self
|
| set_predict_request(self: xgboost.sklearn.XGBModel, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', iteration_range: Union[bool, NoneType, str] = '$UNCHANGED$', output_margin: Union[bool, NoneType, str] = '$UNCHANGED$', validate_features: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBModel from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``predict`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``predict`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``predict``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``predict``.
|
| iteration_range : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``iteration_range`` parameter in ``predict``.
|
| output_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``output_margin`` parameter in ``predict``.
|
| validate_features : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``validate_features`` parameter in ``predict``.
|
| Returns
| -------
| self : object
| The updated object.
|
| ----------------------------------------------------------------------
| Readonly properties defined here:
|
| best_iteration
| The best iteration obtained by early stopping. This attribute is 0-based,
| for instance if the best iteration is the first round, then best_iteration is 0.
|
| best_score
| The best score obtained by early stopping.
|
| coef_
| Coefficients property
|
| .. note:: Coefficients are defined only for linear learners
|
| Coefficients are only defined when the linear model is chosen as
| base learner (`booster=gblinear`). It is not defined for other base
| learner types, such as tree learners (`booster=gbtree`).
|
| Returns
| -------
| coef_ : array of shape ``[n_features]`` or ``[n_classes, n_features]``
|
| feature_importances_
| Feature importances property, return depends on `importance_type`
| parameter. When model trained with multi-class/multi-label/multi-target dataset,
| the feature importance is "averaged" over all targets. The "average" is defined
| based on the importance type. For instance, if the importance type is
| "total_gain", then the score is sum of loss change for each split from all
| trees.
|
| Returns
| -------
| feature_importances_ : array of shape ``[n_features]`` except for multi-class
| linear model, which returns an array with shape `(n_features, n_classes)`
|
| feature_names_in_
| Names of features seen during :py:meth:`fit`. Defined only when `X` has
| feature names that are all strings.
|
| intercept_
| Intercept (bias) property
|
| For tree-based model, the returned value is the `base_score`.
|
| Returns
| -------
| intercept_ : array of shape ``(1,)`` or ``[n_classes]``
|
| n_features_in_
| Number of features seen during :py:meth:`fit`.
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.BaseEstimator:
|
| __getstate__(self)
| Helper for pickle.
|
| __repr__(self, N_CHAR_MAX=700)
| Return repr(self).
|
| __setstate__(self, state)
|
| __sklearn_clone__(self)
|
| ----------------------------------------------------------------------
| Data descriptors inherited from sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| get_metadata_routing(self)
| Get metadata routing of this object.
|
| Please check :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| Returns
| -------
| routing : MetadataRequest
| A :class:`~sklearn.utils.metadata_routing.MetadataRequest` encapsulating
| routing information.
|
| ----------------------------------------------------------------------
| Class methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| __init_subclass__(**kwargs)
| Set the ``set_{method}_request`` methods.
|
| This uses PEP-487 [1]_ to set the ``set_{method}_request`` methods. It
| looks for the information available in the set default values which are
| set using ``__metadata_request__*`` class attributes, or inferred
| from method signatures.
|
| The ``__metadata_request__*`` class attributes are used when a method
| does not explicitly accept a metadata through its arguments or if the
| developer would like to specify a request value for those metadata
| which are different from the default ``None``.
|
| References
| ----------
| .. [1] https://www.python.org/dev/peps/pep-0487
class XGBRFClassifier(XGBClassifier)
| XGBRFClassifier(*, learning_rate: float = 1.0, subsample: float = 0.8, colsample_bynode: float = 0.8, reg_lambda: float = 1e-05, **kwargs: Any)
|
| scikit-learn API for XGBoost random forest classification.
| See :doc:`/python/sklearn_estimator` for more information.
|
| Parameters
| ----------
|
| n_estimators : Optional[int]
| Number of trees in random forest to fit.
|
| max_depth : typing.Optional[int]
|
| Maximum tree depth for base learners.
|
| max_leaves : typing.Optional[int]
|
| Maximum number of leaves; 0 indicates no limit.
|
| max_bin : typing.Optional[int]
|
| If using histogram-based algorithm, maximum number of bins per feature
|
| grow_policy : typing.Optional[str]
|
| Tree growing policy.
|
| - depthwise: Favors splitting at nodes closest to the node,
| - lossguide: Favors splitting at nodes with highest loss change.
|
| learning_rate : typing.Optional[float]
|
| Boosting learning rate (xgb's "eta")
|
| verbosity : typing.Optional[int]
|
| The degree of verbosity. Valid values are 0 (silent) - 3 (debug).
|
| objective : typing.Union[str, xgboost.sklearn._SklObjWProto, typing.Callable[[typing.Any, typing.Any], typing.Tuple[numpy.ndarray, numpy.ndarray]], NoneType]
|
| Specify the learning task and the corresponding learning objective or a custom
| objective function to be used.
|
| For custom objective, see :doc:`/tutorials/custom_metric_obj` and
| :ref:`custom-obj-metric` for more information, along with the end note for
| function signatures.
|
| booster: typing.Optional[str]
|
| Specify which booster to use: ``gbtree``, ``gblinear`` or ``dart``.
|
| .. deprecated:: 3.3.0
|
| ``gblinear`` is deprecated and support will be removed in a future release.
|
| tree_method : typing.Optional[str]
|
| Specify which tree method to use. Default to auto. If this parameter is set to
| default, XGBoost will choose the most conservative option available. It's
| recommended to study this option from the parameters document :doc:`tree method
| </treemethod>`
|
| n_jobs : typing.Optional[int]
|
| Number of parallel threads used to run xgboost. When used with other
| Scikit-Learn algorithms like grid search, you may choose which algorithm to
| parallelize and balance the threads. Creating thread contention will
| significantly slow down both algorithms.
|
| gamma : typing.Optional[float]
|
| (min_split_loss) Minimum loss reduction required to make a further partition on
| a leaf node of the tree.
|
| min_child_weight : typing.Optional[float]
|
| Minimum sum of instance weight(hessian) needed in a child.
|
| max_delta_step : typing.Optional[float]
|
| Maximum delta step we allow each tree's weight estimation to be.
|
| subsample : typing.Optional[float]
|
| Subsample ratio of the training instance.
|
| sampling_method : typing.Optional[str]
|
| Sampling method. Used only by the GPU version of ``hist`` tree method.
|
| - ``uniform``: Select random training instances uniformly.
| - ``gradient_based``: Select random training instances with higher probability
| when the gradient and hessian are larger. (cf. CatBoost)
|
| colsample_bytree : typing.Optional[float]
|
| Subsample ratio of columns when constructing each tree.
|
| colsample_bylevel : typing.Optional[float]
|
| Subsample ratio of columns for each level.
|
| colsample_bynode : typing.Optional[float]
|
| Subsample ratio of columns for each split.
|
| reg_alpha : typing.Optional[float]
|
| L1 regularization term on weights (xgb's alpha).
|
| reg_lambda : typing.Optional[float]
|
| L2 regularization term on weights (xgb's lambda).
|
| scale_pos_weight : typing.Optional[float]
| Balancing of positive and negative weights.
|
| base_score : typing.Union[float, typing.List[float], NoneType]
|
| The initial prediction score of all instances, global bias.
|
| random_state : typing.Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType]
|
| Random number seed.
|
| .. note::
|
| Using gblinear booster with shotgun updater is nondeterministic as
| it uses Hogwild algorithm.
|
| missing : float
|
| Value in the data which needs to be present as a missing value. Default to
| :py:data:`numpy.nan`.
|
| num_parallel_tree: typing.Optional[int]
|
| Used for boosting random forest.
|
| monotone_constraints : typing.Union[typing.Dict[str, int], str, NoneType]
|
| Constraint of variable monotonicity. See :doc:`tutorial </tutorials/monotonic>`
| for more information.
|
| interaction_constraints : typing.Union[str, typing.List[typing.Tuple[str]], NoneType]
|
| Constraints for interaction representing permitted interactions. The
| constraints must be specified in the form of a nested list, e.g. ``[[0, 1], [2,
| 3, 4]]``, where each inner list is a group of indices of features that are
| allowed to interact with each other. See :doc:`tutorial
| </tutorials/feature_interaction_constraint>` for more information
|
| importance_type: typing.Optional[str]
|
| The feature importance type for the feature_importances\_ property:
|
| * For tree model, it's either "gain", "weight", "cover", "total_gain" or
| "total_cover".
| * For linear model, only "weight" is defined and it's the normalized
| coefficients without bias.
|
| device : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| Device ordinal, available options are `cpu`, `cuda`, and `gpu`.
|
| validate_parameters : typing.Optional[bool]
|
| Give warnings for unknown parameter.
|
| enable_categorical : bool
|
| See the same parameter of :py:class:`DMatrix` for details.
|
| feature_types : typing.Optional[typing.Sequence[str]]
|
| .. versionadded:: 1.7.0
|
| Used for specifying feature types without constructing a dataframe. See
| the :py:class:`DMatrix` for details.
|
| feature_weights : Optional[ArrayLike]
|
| Weight for each feature, defines the probability of each feature being selected
| when colsample is being used. All values must be greater than 0, otherwise a
| `ValueError` is thrown.
|
| max_cat_to_onehot : Optional[int]
|
| .. versionadded:: 1.6.0
|
| .. note:: This parameter is experimental
|
| A threshold for deciding whether XGBoost should use one-hot encoding based split
| for categorical data. When number of categories is lesser than the threshold
| then one-hot encoding is chosen, otherwise the categories will be partitioned
| into children nodes. Also, `enable_categorical` needs to be set to have
| categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| max_cat_threshold : typing.Optional[int]
|
| .. versionadded:: 1.7.0
|
| .. note:: This parameter is experimental
|
| Maximum number of categories considered for each split. Used only by
| partition-based splits for preventing over-fitting. Also, `enable_categorical`
| needs to be set to have categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| multi_strategy : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| .. note:: This parameter is working-in-progress.
|
| The strategy used for training multi-target models, including multi-target
| regression and multi-class classification. See :doc:`/tutorials/multioutput` for
| more information.
|
| - ``one_output_per_tree``: One model for each target.
| - ``multi_output_tree``: Use multi-target trees.
|
| eval_metric : typing.Union[str, typing.List[typing.Union[str, typing.Callable]], typing.Callable, NoneType]
|
| .. versionadded:: 1.6.0
|
| Metric used for monitoring the training result and early stopping. It can be a
| string or list of strings as names of predefined metric in XGBoost (See
| :doc:`/parameter`), one of the metrics in :py:mod:`sklearn.metrics`, or any
| other user defined metric that looks like `sklearn.metrics`.
|
| If custom objective is also provided, then custom metric should implement the
| corresponding reverse link function.
|
| Unlike the `scoring` parameter commonly used in scikit-learn, when a callable
| object is provided, it's assumed to be a cost function and by default XGBoost
| will minimize the result during early stopping.
|
| For advanced usage on Early stopping like directly choosing to maximize instead
| of minimize, see :py:obj:`xgboost.callback.EarlyStopping`.
|
| See :doc:`/tutorials/custom_metric_obj` and :ref:`custom-obj-metric` for more
| information.
|
| .. code-block:: python
|
| from sklearn.datasets import load_diabetes
| from sklearn.metrics import mean_absolute_error
| X, y = load_diabetes(return_X_y=True)
| reg = xgb.XGBRegressor(
| tree_method="hist",
| eval_metric=mean_absolute_error,
| )
| reg.fit(X, y, eval_set=[(X, y)])
|
| early_stopping_rounds : typing.Optional[int]
|
| .. versionadded:: 1.6.0
|
| - Activates early stopping. Validation metric needs to improve at least once in
| every **early_stopping_rounds** round(s) to continue training. Requires at
| least one item in **eval_set** in :py:meth:`fit`.
|
| - If early stopping occurs, the model will have two additional attributes:
| :py:attr:`best_score` and :py:attr:`best_iteration`. These are used by the
| :py:meth:`predict` and :py:meth:`apply` methods to determine the optimal
| number of trees during inference. If users want to access the full model
| (including trees built after early stopping), they can specify the
| `iteration_range` in these inference methods. In addition, other utilities
| like model plotting can also use the entire model.
|
| - If you prefer to discard the trees after `best_iteration`, consider using the
| callback function :py:class:`xgboost.callback.EarlyStopping`.
|
| - If there's more than one item in **eval_set**, the last entry will be used for
| early stopping. If there's more than one metric in **eval_metric**, the last
| metric will be used for early stopping.
|
| callbacks : typing.Optional[typing.List[xgboost.callback.TrainingCallback]]
|
| List of callback functions that are applied at end of each iteration.
| It is possible to use predefined callbacks by using
| :ref:`Callback API <callback_api>`.
|
| .. note::
|
| States in callback are not preserved during training, which means callback
| objects can not be reused for multiple training sessions without
| reinitialization or deepcopy.
|
| .. code-block:: python
|
| for params in parameters_grid:
| # be sure to (re)initialize the callbacks before each run
| callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
| reg = xgboost.XGBRegressor(**params, callbacks=callbacks)
| reg.fit(X, y)
|
| kwargs : typing.Optional[typing.Any]
|
| Keyword arguments for XGBoost Booster object. Full documentation of parameters
| can be found :doc:`here </parameter>`.
| Attempting to set a parameter via the constructor args and \*\*kwargs
| dict simultaneously will result in a TypeError.
|
| .. note:: \*\*kwargs unsupported by scikit-learn
|
| \*\*kwargs is unsupported by scikit-learn. We do not guarantee
| that parameters passed via this argument will interact properly
| with scikit-learn.
|
| .. note:: Custom objective function
|
| A custom objective function can be provided for the ``objective``
| parameter. In this case, it should have the signature ``objective(y_true,
| y_pred) -> [grad, hess]`` or ``objective(y_true, y_pred, *, sample_weight)
| -> [grad, hess]``:
|
| y_true: array_like of shape [n_samples]
| The target values
| y_pred: array_like of shape [n_samples]
| The predicted values
| sample_weight :
| Optional sample weights.
|
| grad: array_like of shape [n_samples]
| The value of the gradient for each sample point.
| hess: array_like of shape [n_samples]
| The value of the second derivative for each sample point
|
| Note that, if the custom objective produces negative values for
| the Hessian, these will be clipped. If the objective is non-convex,
| one might also consider using the expected Hessian (Fisher
| information) instead.
|
| Method resolution order:
| XGBRFClassifier
| XGBClassifier
| sklearn.base.ClassifierMixin
| XGBModel
| sklearn.base.BaseEstimator
| sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin
| sklearn.utils._metadata_requests._MetadataRequester
| builtins.object
|
| Methods defined here:
|
| __init__(self, *, learning_rate: float = 1.0, subsample: float = 0.8, colsample_bynode: float = 0.8, reg_lambda: float = 1e-05, **kwargs: Any)
| Initialize self. See help(type(self)) for accurate signature.
|
| fit(self, X: Any, y: Any, *, sample_weight: Optional[Any] = None, base_margin: Optional[Any] = None, eval_set: Optional[Sequence[Tuple[Any, Any]]] = None, verbose: Union[bool, int, NoneType] = True, xgb_model: Union[xgboost.core.Booster, str, xgboost.sklearn.XGBModel, NoneType] = None, sample_weight_eval_set: Optional[Sequence[Any]] = None, base_margin_eval_set: Optional[Sequence[Any]] = None, feature_weights: Optional[Any] = None) -> 'XGBRFClassifier'
| Fit gradient boosting classifier.
|
| Note that calling ``fit()`` multiple times will cause the model object to be
| re-fit from scratch. To resume training from a previous checkpoint, explicitly
| pass ``xgb_model`` argument.
|
| Parameters
| ----------
| X :
| Input feature matrix. See :ref:`py-data` for a list of supported types.
|
| When the ``tree_method`` is set to ``hist``, internally, the
| :py:class:`QuantileDMatrix` will be used instead of the :py:class:`DMatrix`
| for conserving memory. However, this has performance implications when the
| device of input data is not matched with algorithm. For instance, if the
| input is a numpy array on CPU but ``cuda`` is used for training, then the
| data is first processed on CPU then transferred to GPU.
| y :
| Labels
| sample_weight :
| instance weights
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| eval_set :
| A list of (X, y) tuple pairs to use as validation sets, for which
| metrics will be computed.
| Validation metrics will help us track the performance of the model.
|
| verbose :
| If `verbose` is True and an evaluation set is used, the evaluation metric
| measured on the validation set is printed to stdout at each boosting stage.
| If `verbose` is an integer, the evaluation metric is printed at each
| `verbose` boosting stage. The last boosting stage / the boosting stage found
| by using `early_stopping_rounds` is also printed.
| xgb_model :
| file name of stored XGBoost model or 'Booster' instance XGBoost model to be
| loaded before training (allows training continuation).
| sample_weight_eval_set :
| A list of the form [L_1, L_2, ..., L_n], where each L_i is an array like
| object storing instance weights for the i-th validation set.
| base_margin_eval_set :
| A list of the form [M_1, M_2, ..., M_n], where each M_i is an array like
| object storing base margin for the i-th validation set.
| feature_weights :
|
| .. deprecated:: 3.0.0
|
| Use `feature_weights` in :py:meth:`__init__` or :py:meth:`set_params`
| instead.
|
| get_num_boosting_rounds(self) -> int
| Gets the number of xgboost boosting rounds.
|
| get_xgb_params(self) -> Dict[str, Any]
| Get xgboost specific parameters.
|
| set_fit_request(self: xgboost.sklearn.XGBRFClassifier, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', base_margin_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', feature_weights: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', verbose: Union[bool, NoneType, str] = '$UNCHANGED$', xgb_model: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRFClassifier from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``fit`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``fit`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``fit``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``fit``.
|
| base_margin_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin_eval_set`` parameter in ``fit``.
|
| eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``eval_set`` parameter in ``fit``.
|
| feature_weights : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``feature_weights`` parameter in ``fit``.
|
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``fit``.
|
| sample_weight_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight_eval_set`` parameter in ``fit``.
|
| verbose : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``verbose`` parameter in ``fit``.
|
| xgb_model : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``xgb_model`` parameter in ``fit``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_predict_proba_request(self: xgboost.sklearn.XGBRFClassifier, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', iteration_range: Union[bool, NoneType, str] = '$UNCHANGED$', validate_features: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRFClassifier from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``predict_proba`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``predict_proba`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``predict_proba``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``predict_proba``.
|
| iteration_range : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``iteration_range`` parameter in ``predict_proba``.
|
| validate_features : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``validate_features`` parameter in ``predict_proba``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_predict_request(self: xgboost.sklearn.XGBRFClassifier, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', iteration_range: Union[bool, NoneType, str] = '$UNCHANGED$', output_margin: Union[bool, NoneType, str] = '$UNCHANGED$', validate_features: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRFClassifier from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``predict`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``predict`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``predict``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``predict``.
|
| iteration_range : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``iteration_range`` parameter in ``predict``.
|
| output_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``output_margin`` parameter in ``predict``.
|
| validate_features : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``validate_features`` parameter in ``predict``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_score_request(self: xgboost.sklearn.XGBRFClassifier, *, sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRFClassifier from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``score`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``score`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``score``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``score``.
|
| Returns
| -------
| self : object
| The updated object.
|
| ----------------------------------------------------------------------
| Methods inherited from XGBClassifier:
|
| __sklearn_tags__(self) -> object
|
| predict(self, X: Any, *, output_margin: bool = False, validate_features: bool = True, base_margin: Optional[Any] = None, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> Any
| Predict with `X`. If the model is trained with early stopping, then
| :py:attr:`best_iteration` is used automatically. The estimator uses
| `inplace_predict` by default and falls back to using :py:class:`DMatrix` if
| devices between the data and the estimator don't match.
|
| .. note:: This function is only thread safe for `gbtree` and `dart`.
|
| Parameters
| ----------
| X :
| Data to predict with. See :ref:`py-data` for a list of supported types.
| output_margin :
| Whether to output the raw untransformed margin value.
| validate_features :
| When this is True, validate that the Booster's and data's feature_names are
| identical. Otherwise, it is assumed that the feature_names are the same.
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying ``iteration_range=(10,
| 20)``, then only the forests built during [10, 20) (half open set) rounds
| are used in this prediction.
|
| .. versionadded:: 1.4.0
|
| Returns
| -------
| prediction
|
| predict_proba(self, X: Any, validate_features: bool = True, base_margin: Optional[Any] = None, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> numpy.ndarray
| Predict the probability of each `X` example being of a given class. If the
| model is trained with early stopping, then :py:attr:`best_iteration` is used
| automatically. The estimator uses `inplace_predict` by default and falls back to
| using :py:class:`DMatrix` if devices between the data and the estimator don't
| match.
|
| .. note:: This function is only thread safe for `gbtree` and `dart`.
|
| Parameters
| ----------
| X :
| Feature matrix. See :ref:`py-data` for a list of supported types.
| validate_features :
| When this is True, validate that the Booster's and data's feature_names are
| identical. Otherwise, it is assumed that the feature_names are the same.
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying `iteration_range=(10,
| 20)`, then only the forests built during [10, 20) (half open set) rounds are
| used in this prediction.
|
| Returns
| -------
| prediction :
| a numpy array of shape array-like of shape (n_samples, n_classes) with the
| probability of each data example being of a given class.
|
| ----------------------------------------------------------------------
| Readonly properties inherited from XGBClassifier:
|
| classes_
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.ClassifierMixin:
|
| score(self, X, y, sample_weight=None)
| Return the mean accuracy on the given test data and labels.
|
| In multi-label classification, this is the subset accuracy
| which is a harsh metric since you require for each sample that
| each label set be correctly predicted.
|
| Parameters
| ----------
| X : array-like of shape (n_samples, n_features)
| Test samples.
|
| y : array-like of shape (n_samples,) or (n_samples, n_outputs)
| True labels for `X`.
|
| sample_weight : array-like of shape (n_samples,), default=None
| Sample weights.
|
| Returns
| -------
| score : float
| Mean accuracy of ``self.predict(X)`` w.r.t. `y`.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from sklearn.base.ClassifierMixin:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Methods inherited from XGBModel:
|
| __sklearn_is_fitted__(self) -> bool
|
| apply(self, X: Any, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> numpy.ndarray
| Return the predicted leaf every tree for each sample. If the model is trained
| with early stopping, then :py:attr:`best_iteration` is used automatically.
|
| Parameters
| ----------
| X :
| Input features matrix. See :ref:`py-data` for a list of supported types.
|
| iteration_range :
| See :py:meth:`predict`.
|
| Returns
| -------
| X_leaves : array_like, shape=[n_samples, n_trees]
| For each datapoint x in X and for each tree, return the index of the
| leaf x ends up in. Leaves are numbered within
| ``[0; 2**(self.max_depth+1))``, possibly with gaps in the numbering.
|
| evals_result(self) -> Dict[str, Dict[str, List[float]]]
| Return the evaluation results.
|
| If **eval_set** is passed to the :py:meth:`fit` function, you can call
| ``evals_result()`` to get evaluation results for all passed **eval_sets**. When
| **eval_metric** is also passed to the :py:meth:`fit` function, the
| **evals_result** will contain the **eval_metrics** passed to the :py:meth:`fit`
| function.
|
| The returned evaluation result is a dictionary:
|
| .. code-block:: python
|
| {'validation_0': {'logloss': ['0.604835', '0.531479']},
| 'validation_1': {'logloss': ['0.41965', '0.17686']}}
|
| Returns
| -------
| evals_result
|
| get_booster(self) -> xgboost.core.Booster
| Get the underlying xgboost Booster of this model.
|
| This will raise an exception when fit was not called
|
| Returns
| -------
| booster : a xgboost booster of underlying model
|
| get_params(self, deep: bool = True) -> Dict[str, Any]
| Get parameters.
|
| load_model(self, fname: Union[os.PathLike[~AnyStr], bytearray, str]) -> None
| Load the model from a file or a bytearray.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| model.load_model("model.json")
|
| # or
| model.save_model("model.ubj")
| model.load_model("model.ubj")
|
| # or
| buf = model.save_raw()
| model.load_model(buf)
|
| Parameters
| ----------
| fname :
| Input file name or memory buffer(see also save_raw)
|
| save_model(self, fname: Union[str, os.PathLike]) -> None
| Save the model to a file.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| # or
| model.save_model("model.ubj")
|
| Parameters
| ----------
| fname :
| Output file name
|
| set_params(self, **params: Any) -> 'XGBModel'
| Set the parameters of this estimator. Modification of the sklearn method to
| allow unknown kwargs. This allows using the full range of xgboost
| parameters that are not defined as member variables in sklearn grid
| search.
|
| Returns
| -------
| self
|
| ----------------------------------------------------------------------
| Readonly properties inherited from XGBModel:
|
| best_iteration
| The best iteration obtained by early stopping. This attribute is 0-based,
| for instance if the best iteration is the first round, then best_iteration is 0.
|
| best_score
| The best score obtained by early stopping.
|
| coef_
| Coefficients property
|
| .. note:: Coefficients are defined only for linear learners
|
| Coefficients are only defined when the linear model is chosen as
| base learner (`booster=gblinear`). It is not defined for other base
| learner types, such as tree learners (`booster=gbtree`).
|
| Returns
| -------
| coef_ : array of shape ``[n_features]`` or ``[n_classes, n_features]``
|
| feature_importances_
| Feature importances property, return depends on `importance_type`
| parameter. When model trained with multi-class/multi-label/multi-target dataset,
| the feature importance is "averaged" over all targets. The "average" is defined
| based on the importance type. For instance, if the importance type is
| "total_gain", then the score is sum of loss change for each split from all
| trees.
|
| Returns
| -------
| feature_importances_ : array of shape ``[n_features]`` except for multi-class
| linear model, which returns an array with shape `(n_features, n_classes)`
|
| feature_names_in_
| Names of features seen during :py:meth:`fit`. Defined only when `X` has
| feature names that are all strings.
|
| intercept_
| Intercept (bias) property
|
| For tree-based model, the returned value is the `base_score`.
|
| Returns
| -------
| intercept_ : array of shape ``(1,)`` or ``[n_classes]``
|
| n_features_in_
| Number of features seen during :py:meth:`fit`.
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.BaseEstimator:
|
| __getstate__(self)
| Helper for pickle.
|
| __repr__(self, N_CHAR_MAX=700)
| Return repr(self).
|
| __setstate__(self, state)
|
| __sklearn_clone__(self)
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| get_metadata_routing(self)
| Get metadata routing of this object.
|
| Please check :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| Returns
| -------
| routing : MetadataRequest
| A :class:`~sklearn.utils.metadata_routing.MetadataRequest` encapsulating
| routing information.
|
| ----------------------------------------------------------------------
| Class methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| __init_subclass__(**kwargs)
| Set the ``set_{method}_request`` methods.
|
| This uses PEP-487 [1]_ to set the ``set_{method}_request`` methods. It
| looks for the information available in the set default values which are
| set using ``__metadata_request__*`` class attributes, or inferred
| from method signatures.
|
| The ``__metadata_request__*`` class attributes are used when a method
| does not explicitly accept a metadata through its arguments or if the
| developer would like to specify a request value for those metadata
| which are different from the default ``None``.
|
| References
| ----------
| .. [1] https://www.python.org/dev/peps/pep-0487
class XGBRFRegressor(XGBRegressor)
| XGBRFRegressor(*, learning_rate: float = 1.0, subsample: float = 0.8, colsample_bynode: float = 0.8, reg_lambda: float = 1e-05, **kwargs: Any) -> None
|
| scikit-learn API for XGBoost random forest regression.
| See :doc:`/python/sklearn_estimator` for more information.
|
| Parameters
| ----------
|
| n_estimators : Optional[int]
| Number of trees in random forest to fit.
|
| max_depth : typing.Optional[int]
|
| Maximum tree depth for base learners.
|
| max_leaves : typing.Optional[int]
|
| Maximum number of leaves; 0 indicates no limit.
|
| max_bin : typing.Optional[int]
|
| If using histogram-based algorithm, maximum number of bins per feature
|
| grow_policy : typing.Optional[str]
|
| Tree growing policy.
|
| - depthwise: Favors splitting at nodes closest to the node,
| - lossguide: Favors splitting at nodes with highest loss change.
|
| learning_rate : typing.Optional[float]
|
| Boosting learning rate (xgb's "eta")
|
| verbosity : typing.Optional[int]
|
| The degree of verbosity. Valid values are 0 (silent) - 3 (debug).
|
| objective : typing.Union[str, xgboost.sklearn._SklObjWProto, typing.Callable[[typing.Any, typing.Any], typing.Tuple[numpy.ndarray, numpy.ndarray]], NoneType]
|
| Specify the learning task and the corresponding learning objective or a custom
| objective function to be used.
|
| For custom objective, see :doc:`/tutorials/custom_metric_obj` and
| :ref:`custom-obj-metric` for more information, along with the end note for
| function signatures.
|
| booster: typing.Optional[str]
|
| Specify which booster to use: ``gbtree``, ``gblinear`` or ``dart``.
|
| .. deprecated:: 3.3.0
|
| ``gblinear`` is deprecated and support will be removed in a future release.
|
| tree_method : typing.Optional[str]
|
| Specify which tree method to use. Default to auto. If this parameter is set to
| default, XGBoost will choose the most conservative option available. It's
| recommended to study this option from the parameters document :doc:`tree method
| </treemethod>`
|
| n_jobs : typing.Optional[int]
|
| Number of parallel threads used to run xgboost. When used with other
| Scikit-Learn algorithms like grid search, you may choose which algorithm to
| parallelize and balance the threads. Creating thread contention will
| significantly slow down both algorithms.
|
| gamma : typing.Optional[float]
|
| (min_split_loss) Minimum loss reduction required to make a further partition on
| a leaf node of the tree.
|
| min_child_weight : typing.Optional[float]
|
| Minimum sum of instance weight(hessian) needed in a child.
|
| max_delta_step : typing.Optional[float]
|
| Maximum delta step we allow each tree's weight estimation to be.
|
| subsample : typing.Optional[float]
|
| Subsample ratio of the training instance.
|
| sampling_method : typing.Optional[str]
|
| Sampling method. Used only by the GPU version of ``hist`` tree method.
|
| - ``uniform``: Select random training instances uniformly.
| - ``gradient_based``: Select random training instances with higher probability
| when the gradient and hessian are larger. (cf. CatBoost)
|
| colsample_bytree : typing.Optional[float]
|
| Subsample ratio of columns when constructing each tree.
|
| colsample_bylevel : typing.Optional[float]
|
| Subsample ratio of columns for each level.
|
| colsample_bynode : typing.Optional[float]
|
| Subsample ratio of columns for each split.
|
| reg_alpha : typing.Optional[float]
|
| L1 regularization term on weights (xgb's alpha).
|
| reg_lambda : typing.Optional[float]
|
| L2 regularization term on weights (xgb's lambda).
|
| scale_pos_weight : typing.Optional[float]
| Balancing of positive and negative weights.
|
| base_score : typing.Union[float, typing.List[float], NoneType]
|
| The initial prediction score of all instances, global bias.
|
| random_state : typing.Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType]
|
| Random number seed.
|
| .. note::
|
| Using gblinear booster with shotgun updater is nondeterministic as
| it uses Hogwild algorithm.
|
| missing : float
|
| Value in the data which needs to be present as a missing value. Default to
| :py:data:`numpy.nan`.
|
| num_parallel_tree: typing.Optional[int]
|
| Used for boosting random forest.
|
| monotone_constraints : typing.Union[typing.Dict[str, int], str, NoneType]
|
| Constraint of variable monotonicity. See :doc:`tutorial </tutorials/monotonic>`
| for more information.
|
| interaction_constraints : typing.Union[str, typing.List[typing.Tuple[str]], NoneType]
|
| Constraints for interaction representing permitted interactions. The
| constraints must be specified in the form of a nested list, e.g. ``[[0, 1], [2,
| 3, 4]]``, where each inner list is a group of indices of features that are
| allowed to interact with each other. See :doc:`tutorial
| </tutorials/feature_interaction_constraint>` for more information
|
| importance_type: typing.Optional[str]
|
| The feature importance type for the feature_importances\_ property:
|
| * For tree model, it's either "gain", "weight", "cover", "total_gain" or
| "total_cover".
| * For linear model, only "weight" is defined and it's the normalized
| coefficients without bias.
|
| device : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| Device ordinal, available options are `cpu`, `cuda`, and `gpu`.
|
| validate_parameters : typing.Optional[bool]
|
| Give warnings for unknown parameter.
|
| enable_categorical : bool
|
| See the same parameter of :py:class:`DMatrix` for details.
|
| feature_types : typing.Optional[typing.Sequence[str]]
|
| .. versionadded:: 1.7.0
|
| Used for specifying feature types without constructing a dataframe. See
| the :py:class:`DMatrix` for details.
|
| feature_weights : Optional[ArrayLike]
|
| Weight for each feature, defines the probability of each feature being selected
| when colsample is being used. All values must be greater than 0, otherwise a
| `ValueError` is thrown.
|
| max_cat_to_onehot : Optional[int]
|
| .. versionadded:: 1.6.0
|
| .. note:: This parameter is experimental
|
| A threshold for deciding whether XGBoost should use one-hot encoding based split
| for categorical data. When number of categories is lesser than the threshold
| then one-hot encoding is chosen, otherwise the categories will be partitioned
| into children nodes. Also, `enable_categorical` needs to be set to have
| categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| max_cat_threshold : typing.Optional[int]
|
| .. versionadded:: 1.7.0
|
| .. note:: This parameter is experimental
|
| Maximum number of categories considered for each split. Used only by
| partition-based splits for preventing over-fitting. Also, `enable_categorical`
| needs to be set to have categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| multi_strategy : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| .. note:: This parameter is working-in-progress.
|
| The strategy used for training multi-target models, including multi-target
| regression and multi-class classification. See :doc:`/tutorials/multioutput` for
| more information.
|
| - ``one_output_per_tree``: One model for each target.
| - ``multi_output_tree``: Use multi-target trees.
|
| eval_metric : typing.Union[str, typing.List[typing.Union[str, typing.Callable]], typing.Callable, NoneType]
|
| .. versionadded:: 1.6.0
|
| Metric used for monitoring the training result and early stopping. It can be a
| string or list of strings as names of predefined metric in XGBoost (See
| :doc:`/parameter`), one of the metrics in :py:mod:`sklearn.metrics`, or any
| other user defined metric that looks like `sklearn.metrics`.
|
| If custom objective is also provided, then custom metric should implement the
| corresponding reverse link function.
|
| Unlike the `scoring` parameter commonly used in scikit-learn, when a callable
| object is provided, it's assumed to be a cost function and by default XGBoost
| will minimize the result during early stopping.
|
| For advanced usage on Early stopping like directly choosing to maximize instead
| of minimize, see :py:obj:`xgboost.callback.EarlyStopping`.
|
| See :doc:`/tutorials/custom_metric_obj` and :ref:`custom-obj-metric` for more
| information.
|
| .. code-block:: python
|
| from sklearn.datasets import load_diabetes
| from sklearn.metrics import mean_absolute_error
| X, y = load_diabetes(return_X_y=True)
| reg = xgb.XGBRegressor(
| tree_method="hist",
| eval_metric=mean_absolute_error,
| )
| reg.fit(X, y, eval_set=[(X, y)])
|
| early_stopping_rounds : typing.Optional[int]
|
| .. versionadded:: 1.6.0
|
| - Activates early stopping. Validation metric needs to improve at least once in
| every **early_stopping_rounds** round(s) to continue training. Requires at
| least one item in **eval_set** in :py:meth:`fit`.
|
| - If early stopping occurs, the model will have two additional attributes:
| :py:attr:`best_score` and :py:attr:`best_iteration`. These are used by the
| :py:meth:`predict` and :py:meth:`apply` methods to determine the optimal
| number of trees during inference. If users want to access the full model
| (including trees built after early stopping), they can specify the
| `iteration_range` in these inference methods. In addition, other utilities
| like model plotting can also use the entire model.
|
| - If you prefer to discard the trees after `best_iteration`, consider using the
| callback function :py:class:`xgboost.callback.EarlyStopping`.
|
| - If there's more than one item in **eval_set**, the last entry will be used for
| early stopping. If there's more than one metric in **eval_metric**, the last
| metric will be used for early stopping.
|
| callbacks : typing.Optional[typing.List[xgboost.callback.TrainingCallback]]
|
| List of callback functions that are applied at end of each iteration.
| It is possible to use predefined callbacks by using
| :ref:`Callback API <callback_api>`.
|
| .. note::
|
| States in callback are not preserved during training, which means callback
| objects can not be reused for multiple training sessions without
| reinitialization or deepcopy.
|
| .. code-block:: python
|
| for params in parameters_grid:
| # be sure to (re)initialize the callbacks before each run
| callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
| reg = xgboost.XGBRegressor(**params, callbacks=callbacks)
| reg.fit(X, y)
|
| kwargs : typing.Optional[typing.Any]
|
| Keyword arguments for XGBoost Booster object. Full documentation of parameters
| can be found :doc:`here </parameter>`.
| Attempting to set a parameter via the constructor args and \*\*kwargs
| dict simultaneously will result in a TypeError.
|
| .. note:: \*\*kwargs unsupported by scikit-learn
|
| \*\*kwargs is unsupported by scikit-learn. We do not guarantee
| that parameters passed via this argument will interact properly
| with scikit-learn.
|
| .. note:: Custom objective function
|
| A custom objective function can be provided for the ``objective``
| parameter. In this case, it should have the signature ``objective(y_true,
| y_pred) -> [grad, hess]`` or ``objective(y_true, y_pred, *, sample_weight)
| -> [grad, hess]``:
|
| y_true: array_like of shape [n_samples]
| The target values
| y_pred: array_like of shape [n_samples]
| The predicted values
| sample_weight :
| Optional sample weights.
|
| grad: array_like of shape [n_samples]
| The value of the gradient for each sample point.
| hess: array_like of shape [n_samples]
| The value of the second derivative for each sample point
|
| Note that, if the custom objective produces negative values for
| the Hessian, these will be clipped. If the objective is non-convex,
| one might also consider using the expected Hessian (Fisher
| information) instead.
|
| Method resolution order:
| XGBRFRegressor
| XGBRegressor
| sklearn.base.RegressorMixin
| XGBModel
| sklearn.base.BaseEstimator
| sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin
| sklearn.utils._metadata_requests._MetadataRequester
| builtins.object
|
| Methods defined here:
|
| __init__(self, *, learning_rate: float = 1.0, subsample: float = 0.8, colsample_bynode: float = 0.8, reg_lambda: float = 1e-05, **kwargs: Any) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| fit(self, X: Any, y: Any, *, sample_weight: Optional[Any] = None, base_margin: Optional[Any] = None, eval_set: Optional[Sequence[Tuple[Any, Any]]] = None, verbose: Union[bool, int, NoneType] = True, xgb_model: Union[xgboost.core.Booster, str, xgboost.sklearn.XGBModel, NoneType] = None, sample_weight_eval_set: Optional[Sequence[Any]] = None, base_margin_eval_set: Optional[Sequence[Any]] = None, feature_weights: Optional[Any] = None) -> 'XGBRFRegressor'
| Fit gradient boosting model.
|
| Note that calling ``fit()`` multiple times will cause the model object to be
| re-fit from scratch. To resume training from a previous checkpoint, explicitly
| pass ``xgb_model`` argument.
|
| Parameters
| ----------
| X :
| Input feature matrix. See :ref:`py-data` for a list of supported types.
|
| When the ``tree_method`` is set to ``hist``, internally, the
| :py:class:`QuantileDMatrix` will be used instead of the :py:class:`DMatrix`
| for conserving memory. However, this has performance implications when the
| device of input data is not matched with algorithm. For instance, if the
| input is a numpy array on CPU but ``cuda`` is used for training, then the
| data is first processed on CPU then transferred to GPU.
| y :
| Labels
| sample_weight :
| instance weights
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| eval_set :
| A list of (X, y) tuple pairs to use as validation sets, for which
| metrics will be computed.
| Validation metrics will help us track the performance of the model.
|
| verbose :
| If `verbose` is True and an evaluation set is used, the evaluation metric
| measured on the validation set is printed to stdout at each boosting stage.
| If `verbose` is an integer, the evaluation metric is printed at each
| `verbose` boosting stage. The last boosting stage / the boosting stage found
| by using `early_stopping_rounds` is also printed.
| xgb_model :
| file name of stored XGBoost model or 'Booster' instance XGBoost model to be
| loaded before training (allows training continuation).
| sample_weight_eval_set :
| A list of the form [L_1, L_2, ..., L_n], where each L_i is an array like
| object storing instance weights for the i-th validation set.
| base_margin_eval_set :
| A list of the form [M_1, M_2, ..., M_n], where each M_i is an array like
| object storing base margin for the i-th validation set.
| feature_weights :
|
| .. deprecated:: 3.0.0
|
| Use `feature_weights` in :py:meth:`__init__` or :py:meth:`set_params`
| instead.
|
| get_num_boosting_rounds(self) -> int
| Gets the number of xgboost boosting rounds.
|
| get_xgb_params(self) -> Dict[str, Any]
| Get xgboost specific parameters.
|
| set_fit_request(self: xgboost.sklearn.XGBRFRegressor, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', base_margin_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', feature_weights: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', verbose: Union[bool, NoneType, str] = '$UNCHANGED$', xgb_model: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRFRegressor from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``fit`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``fit`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``fit``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``fit``.
|
| base_margin_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin_eval_set`` parameter in ``fit``.
|
| eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``eval_set`` parameter in ``fit``.
|
| feature_weights : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``feature_weights`` parameter in ``fit``.
|
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``fit``.
|
| sample_weight_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight_eval_set`` parameter in ``fit``.
|
| verbose : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``verbose`` parameter in ``fit``.
|
| xgb_model : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``xgb_model`` parameter in ``fit``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_predict_request(self: xgboost.sklearn.XGBRFRegressor, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', iteration_range: Union[bool, NoneType, str] = '$UNCHANGED$', output_margin: Union[bool, NoneType, str] = '$UNCHANGED$', validate_features: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRFRegressor from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``predict`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``predict`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``predict``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``predict``.
|
| iteration_range : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``iteration_range`` parameter in ``predict``.
|
| output_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``output_margin`` parameter in ``predict``.
|
| validate_features : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``validate_features`` parameter in ``predict``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_score_request(self: xgboost.sklearn.XGBRFRegressor, *, sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRFRegressor from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``score`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``score`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``score``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``score``.
|
| Returns
| -------
| self : object
| The updated object.
|
| ----------------------------------------------------------------------
| Methods inherited from XGBRegressor:
|
| __sklearn_tags__(self) -> object
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.RegressorMixin:
|
| score(self, X, y, sample_weight=None)
| Return the coefficient of determination of the prediction.
|
| The coefficient of determination :math:`R^2` is defined as
| :math:`(1 - \frac{u}{v})`, where :math:`u` is the residual
| sum of squares ``((y_true - y_pred)** 2).sum()`` and :math:`v`
| is the total sum of squares ``((y_true - y_true.mean()) ** 2).sum()``.
| The best possible score is 1.0 and it can be negative (because the
| model can be arbitrarily worse). A constant model that always predicts
| the expected value of `y`, disregarding the input features, would get
| a :math:`R^2` score of 0.0.
|
| Parameters
| ----------
| X : array-like of shape (n_samples, n_features)
| Test samples. For some estimators this may be a precomputed
| kernel matrix or a list of generic objects instead with shape
| ``(n_samples, n_samples_fitted)``, where ``n_samples_fitted``
| is the number of samples used in the fitting for the estimator.
|
| y : array-like of shape (n_samples,) or (n_samples, n_outputs)
| True values for `X`.
|
| sample_weight : array-like of shape (n_samples,), default=None
| Sample weights.
|
| Returns
| -------
| score : float
| :math:`R^2` of ``self.predict(X)`` w.r.t. `y`.
|
| Notes
| -----
| The :math:`R^2` score used when calling ``score`` on a regressor uses
| ``multioutput='uniform_average'`` from version 0.23 to keep consistent
| with default value of :func:`~sklearn.metrics.r2_score`.
| This influences the ``score`` method of all the multioutput
| regressors (except for
| :class:`~sklearn.multioutput.MultiOutputRegressor`).
|
| ----------------------------------------------------------------------
| Data descriptors inherited from sklearn.base.RegressorMixin:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Methods inherited from XGBModel:
|
| __sklearn_is_fitted__(self) -> bool
|
| apply(self, X: Any, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> numpy.ndarray
| Return the predicted leaf every tree for each sample. If the model is trained
| with early stopping, then :py:attr:`best_iteration` is used automatically.
|
| Parameters
| ----------
| X :
| Input features matrix. See :ref:`py-data` for a list of supported types.
|
| iteration_range :
| See :py:meth:`predict`.
|
| Returns
| -------
| X_leaves : array_like, shape=[n_samples, n_trees]
| For each datapoint x in X and for each tree, return the index of the
| leaf x ends up in. Leaves are numbered within
| ``[0; 2**(self.max_depth+1))``, possibly with gaps in the numbering.
|
| evals_result(self) -> Dict[str, Dict[str, List[float]]]
| Return the evaluation results.
|
| If **eval_set** is passed to the :py:meth:`fit` function, you can call
| ``evals_result()`` to get evaluation results for all passed **eval_sets**. When
| **eval_metric** is also passed to the :py:meth:`fit` function, the
| **evals_result** will contain the **eval_metrics** passed to the :py:meth:`fit`
| function.
|
| The returned evaluation result is a dictionary:
|
| .. code-block:: python
|
| {'validation_0': {'logloss': ['0.604835', '0.531479']},
| 'validation_1': {'logloss': ['0.41965', '0.17686']}}
|
| Returns
| -------
| evals_result
|
| get_booster(self) -> xgboost.core.Booster
| Get the underlying xgboost Booster of this model.
|
| This will raise an exception when fit was not called
|
| Returns
| -------
| booster : a xgboost booster of underlying model
|
| get_params(self, deep: bool = True) -> Dict[str, Any]
| Get parameters.
|
| load_model(self, fname: Union[os.PathLike[~AnyStr], bytearray, str]) -> None
| Load the model from a file or a bytearray.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| model.load_model("model.json")
|
| # or
| model.save_model("model.ubj")
| model.load_model("model.ubj")
|
| # or
| buf = model.save_raw()
| model.load_model(buf)
|
| Parameters
| ----------
| fname :
| Input file name or memory buffer(see also save_raw)
|
| predict(self, X: Any, *, output_margin: bool = False, validate_features: bool = True, base_margin: Optional[Any] = None, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> Any
| Predict with `X`. If the model is trained with early stopping, then
| :py:attr:`best_iteration` is used automatically. The estimator uses
| `inplace_predict` by default and falls back to using :py:class:`DMatrix` if
| devices between the data and the estimator don't match.
|
| .. note:: This function is only thread safe for `gbtree` and `dart`.
|
| Parameters
| ----------
| X :
| Data to predict with. See :ref:`py-data` for a list of supported types.
| output_margin :
| Whether to output the raw untransformed margin value.
| validate_features :
| When this is True, validate that the Booster's and data's feature_names are
| identical. Otherwise, it is assumed that the feature_names are the same.
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying ``iteration_range=(10,
| 20)``, then only the forests built during [10, 20) (half open set) rounds
| are used in this prediction.
|
| .. versionadded:: 1.4.0
|
| Returns
| -------
| prediction
|
| save_model(self, fname: Union[str, os.PathLike]) -> None
| Save the model to a file.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| # or
| model.save_model("model.ubj")
|
| Parameters
| ----------
| fname :
| Output file name
|
| set_params(self, **params: Any) -> 'XGBModel'
| Set the parameters of this estimator. Modification of the sklearn method to
| allow unknown kwargs. This allows using the full range of xgboost
| parameters that are not defined as member variables in sklearn grid
| search.
|
| Returns
| -------
| self
|
| ----------------------------------------------------------------------
| Readonly properties inherited from XGBModel:
|
| best_iteration
| The best iteration obtained by early stopping. This attribute is 0-based,
| for instance if the best iteration is the first round, then best_iteration is 0.
|
| best_score
| The best score obtained by early stopping.
|
| coef_
| Coefficients property
|
| .. note:: Coefficients are defined only for linear learners
|
| Coefficients are only defined when the linear model is chosen as
| base learner (`booster=gblinear`). It is not defined for other base
| learner types, such as tree learners (`booster=gbtree`).
|
| Returns
| -------
| coef_ : array of shape ``[n_features]`` or ``[n_classes, n_features]``
|
| feature_importances_
| Feature importances property, return depends on `importance_type`
| parameter. When model trained with multi-class/multi-label/multi-target dataset,
| the feature importance is "averaged" over all targets. The "average" is defined
| based on the importance type. For instance, if the importance type is
| "total_gain", then the score is sum of loss change for each split from all
| trees.
|
| Returns
| -------
| feature_importances_ : array of shape ``[n_features]`` except for multi-class
| linear model, which returns an array with shape `(n_features, n_classes)`
|
| feature_names_in_
| Names of features seen during :py:meth:`fit`. Defined only when `X` has
| feature names that are all strings.
|
| intercept_
| Intercept (bias) property
|
| For tree-based model, the returned value is the `base_score`.
|
| Returns
| -------
| intercept_ : array of shape ``(1,)`` or ``[n_classes]``
|
| n_features_in_
| Number of features seen during :py:meth:`fit`.
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.BaseEstimator:
|
| __getstate__(self)
| Helper for pickle.
|
| __repr__(self, N_CHAR_MAX=700)
| Return repr(self).
|
| __setstate__(self, state)
|
| __sklearn_clone__(self)
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| get_metadata_routing(self)
| Get metadata routing of this object.
|
| Please check :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| Returns
| -------
| routing : MetadataRequest
| A :class:`~sklearn.utils.metadata_routing.MetadataRequest` encapsulating
| routing information.
|
| ----------------------------------------------------------------------
| Class methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| __init_subclass__(**kwargs)
| Set the ``set_{method}_request`` methods.
|
| This uses PEP-487 [1]_ to set the ``set_{method}_request`` methods. It
| looks for the information available in the set default values which are
| set using ``__metadata_request__*`` class attributes, or inferred
| from method signatures.
|
| The ``__metadata_request__*`` class attributes are used when a method
| does not explicitly accept a metadata through its arguments or if the
| developer would like to specify a request value for those metadata
| which are different from the default ``None``.
|
| References
| ----------
| .. [1] https://www.python.org/dev/peps/pep-0487
class XGBRanker(XGBRankerMixIn, XGBModel)
| XGBRanker(*, objective: str = 'rank:ndcg', **kwargs: Any)
|
| Implementation of the Scikit-Learn API for XGBoost Ranking.
|
| See :doc:`Learning to Rank </tutorials/learning_to_rank>` for an introducion.
|
|
| See :doc:`/python/sklearn_estimator` for more information.
|
| Parameters
| ----------
|
| n_estimators : typing.Optional[int]
| Number of gradient boosted trees. Equivalent to number of boosting
| rounds.
|
| max_depth : typing.Optional[int]
|
| Maximum tree depth for base learners.
|
| max_leaves : typing.Optional[int]
|
| Maximum number of leaves; 0 indicates no limit.
|
| max_bin : typing.Optional[int]
|
| If using histogram-based algorithm, maximum number of bins per feature
|
| grow_policy : typing.Optional[str]
|
| Tree growing policy.
|
| - depthwise: Favors splitting at nodes closest to the node,
| - lossguide: Favors splitting at nodes with highest loss change.
|
| learning_rate : typing.Optional[float]
|
| Boosting learning rate (xgb's "eta")
|
| verbosity : typing.Optional[int]
|
| The degree of verbosity. Valid values are 0 (silent) - 3 (debug).
|
| objective : typing.Union[str, xgboost.sklearn._SklObjWProto, typing.Callable[[typing.Any, typing.Any], typing.Tuple[numpy.ndarray, numpy.ndarray]], NoneType]
|
| Specify the learning task and the corresponding learning objective or a custom
| objective function to be used.
|
| For custom objective, see :doc:`/tutorials/custom_metric_obj` and
| :ref:`custom-obj-metric` for more information, along with the end note for
| function signatures.
|
| booster: typing.Optional[str]
|
| Specify which booster to use: ``gbtree``, ``gblinear`` or ``dart``.
|
| .. deprecated:: 3.3.0
|
| ``gblinear`` is deprecated and support will be removed in a future release.
|
| tree_method : typing.Optional[str]
|
| Specify which tree method to use. Default to auto. If this parameter is set to
| default, XGBoost will choose the most conservative option available. It's
| recommended to study this option from the parameters document :doc:`tree method
| </treemethod>`
|
| n_jobs : typing.Optional[int]
|
| Number of parallel threads used to run xgboost. When used with other
| Scikit-Learn algorithms like grid search, you may choose which algorithm to
| parallelize and balance the threads. Creating thread contention will
| significantly slow down both algorithms.
|
| gamma : typing.Optional[float]
|
| (min_split_loss) Minimum loss reduction required to make a further partition on
| a leaf node of the tree.
|
| min_child_weight : typing.Optional[float]
|
| Minimum sum of instance weight(hessian) needed in a child.
|
| max_delta_step : typing.Optional[float]
|
| Maximum delta step we allow each tree's weight estimation to be.
|
| subsample : typing.Optional[float]
|
| Subsample ratio of the training instance.
|
| sampling_method : typing.Optional[str]
|
| Sampling method. Used only by the GPU version of ``hist`` tree method.
|
| - ``uniform``: Select random training instances uniformly.
| - ``gradient_based``: Select random training instances with higher probability
| when the gradient and hessian are larger. (cf. CatBoost)
|
| colsample_bytree : typing.Optional[float]
|
| Subsample ratio of columns when constructing each tree.
|
| colsample_bylevel : typing.Optional[float]
|
| Subsample ratio of columns for each level.
|
| colsample_bynode : typing.Optional[float]
|
| Subsample ratio of columns for each split.
|
| reg_alpha : typing.Optional[float]
|
| L1 regularization term on weights (xgb's alpha).
|
| reg_lambda : typing.Optional[float]
|
| L2 regularization term on weights (xgb's lambda).
|
| scale_pos_weight : typing.Optional[float]
| Balancing of positive and negative weights.
|
| base_score : typing.Union[float, typing.List[float], NoneType]
|
| The initial prediction score of all instances, global bias.
|
| random_state : typing.Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType]
|
| Random number seed.
|
| .. note::
|
| Using gblinear booster with shotgun updater is nondeterministic as
| it uses Hogwild algorithm.
|
| missing : float
|
| Value in the data which needs to be present as a missing value. Default to
| :py:data:`numpy.nan`.
|
| num_parallel_tree: typing.Optional[int]
|
| Used for boosting random forest.
|
| monotone_constraints : typing.Union[typing.Dict[str, int], str, NoneType]
|
| Constraint of variable monotonicity. See :doc:`tutorial </tutorials/monotonic>`
| for more information.
|
| interaction_constraints : typing.Union[str, typing.List[typing.Tuple[str]], NoneType]
|
| Constraints for interaction representing permitted interactions. The
| constraints must be specified in the form of a nested list, e.g. ``[[0, 1], [2,
| 3, 4]]``, where each inner list is a group of indices of features that are
| allowed to interact with each other. See :doc:`tutorial
| </tutorials/feature_interaction_constraint>` for more information
|
| importance_type: typing.Optional[str]
|
| The feature importance type for the feature_importances\_ property:
|
| * For tree model, it's either "gain", "weight", "cover", "total_gain" or
| "total_cover".
| * For linear model, only "weight" is defined and it's the normalized
| coefficients without bias.
|
| device : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| Device ordinal, available options are `cpu`, `cuda`, and `gpu`.
|
| validate_parameters : typing.Optional[bool]
|
| Give warnings for unknown parameter.
|
| enable_categorical : bool
|
| See the same parameter of :py:class:`DMatrix` for details.
|
| feature_types : typing.Optional[typing.Sequence[str]]
|
| .. versionadded:: 1.7.0
|
| Used for specifying feature types without constructing a dataframe. See
| the :py:class:`DMatrix` for details.
|
| feature_weights : Optional[ArrayLike]
|
| Weight for each feature, defines the probability of each feature being selected
| when colsample is being used. All values must be greater than 0, otherwise a
| `ValueError` is thrown.
|
| max_cat_to_onehot : Optional[int]
|
| .. versionadded:: 1.6.0
|
| .. note:: This parameter is experimental
|
| A threshold for deciding whether XGBoost should use one-hot encoding based split
| for categorical data. When number of categories is lesser than the threshold
| then one-hot encoding is chosen, otherwise the categories will be partitioned
| into children nodes. Also, `enable_categorical` needs to be set to have
| categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| max_cat_threshold : typing.Optional[int]
|
| .. versionadded:: 1.7.0
|
| .. note:: This parameter is experimental
|
| Maximum number of categories considered for each split. Used only by
| partition-based splits for preventing over-fitting. Also, `enable_categorical`
| needs to be set to have categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| multi_strategy : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| .. note:: This parameter is working-in-progress.
|
| The strategy used for training multi-target models, including multi-target
| regression and multi-class classification. See :doc:`/tutorials/multioutput` for
| more information.
|
| - ``one_output_per_tree``: One model for each target.
| - ``multi_output_tree``: Use multi-target trees.
|
| eval_metric : typing.Union[str, typing.List[typing.Union[str, typing.Callable]], typing.Callable, NoneType]
|
| .. versionadded:: 1.6.0
|
| Metric used for monitoring the training result and early stopping. It can be a
| string or list of strings as names of predefined metric in XGBoost (See
| :doc:`/parameter`), one of the metrics in :py:mod:`sklearn.metrics`, or any
| other user defined metric that looks like `sklearn.metrics`.
|
| If custom objective is also provided, then custom metric should implement the
| corresponding reverse link function.
|
| Unlike the `scoring` parameter commonly used in scikit-learn, when a callable
| object is provided, it's assumed to be a cost function and by default XGBoost
| will minimize the result during early stopping.
|
| For advanced usage on Early stopping like directly choosing to maximize instead
| of minimize, see :py:obj:`xgboost.callback.EarlyStopping`.
|
| See :doc:`/tutorials/custom_metric_obj` and :ref:`custom-obj-metric` for more
| information.
|
| .. code-block:: python
|
| from sklearn.datasets import load_diabetes
| from sklearn.metrics import mean_absolute_error
| X, y = load_diabetes(return_X_y=True)
| reg = xgb.XGBRegressor(
| tree_method="hist",
| eval_metric=mean_absolute_error,
| )
| reg.fit(X, y, eval_set=[(X, y)])
|
| early_stopping_rounds : typing.Optional[int]
|
| .. versionadded:: 1.6.0
|
| - Activates early stopping. Validation metric needs to improve at least once in
| every **early_stopping_rounds** round(s) to continue training. Requires at
| least one item in **eval_set** in :py:meth:`fit`.
|
| - If early stopping occurs, the model will have two additional attributes:
| :py:attr:`best_score` and :py:attr:`best_iteration`. These are used by the
| :py:meth:`predict` and :py:meth:`apply` methods to determine the optimal
| number of trees during inference. If users want to access the full model
| (including trees built after early stopping), they can specify the
| `iteration_range` in these inference methods. In addition, other utilities
| like model plotting can also use the entire model.
|
| - If you prefer to discard the trees after `best_iteration`, consider using the
| callback function :py:class:`xgboost.callback.EarlyStopping`.
|
| - If there's more than one item in **eval_set**, the last entry will be used for
| early stopping. If there's more than one metric in **eval_metric**, the last
| metric will be used for early stopping.
|
| callbacks : typing.Optional[typing.List[xgboost.callback.TrainingCallback]]
|
| List of callback functions that are applied at end of each iteration.
| It is possible to use predefined callbacks by using
| :ref:`Callback API <callback_api>`.
|
| .. note::
|
| States in callback are not preserved during training, which means callback
| objects can not be reused for multiple training sessions without
| reinitialization or deepcopy.
|
| .. code-block:: python
|
| for params in parameters_grid:
| # be sure to (re)initialize the callbacks before each run
| callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
| reg = xgboost.XGBRegressor(**params, callbacks=callbacks)
| reg.fit(X, y)
|
| kwargs : typing.Optional[typing.Any]
|
| Keyword arguments for XGBoost Booster object. Full documentation of parameters
| can be found :doc:`here </parameter>`.
| Attempting to set a parameter via the constructor args and \*\*kwargs
| dict simultaneously will result in a TypeError.
|
| .. note:: \*\*kwargs unsupported by scikit-learn
|
| \*\*kwargs is unsupported by scikit-learn. We do not guarantee
| that parameters passed via this argument will interact properly
| with scikit-learn.
|
| .. note::
|
| A custom objective function is currently not supported by XGBRanker.
|
| .. note::
|
| Query group information is only required for ranking training but not
| prediction. Multiple groups can be predicted on a single call to
| :py:meth:`predict`.
|
| When fitting the model with the `group` parameter, your data need to be sorted
| by the query group first. `group` is an array that contains the size of each
| query group.
|
| Similarly, when fitting the model with the `qid` parameter, the data should be
| sorted according to query index and `qid` is an array that contains the query
| index for each training sample.
|
| For example, if your original data look like:
|
| +-------+-----------+---------------+
| | qid | label | features |
| +-------+-----------+---------------+
| | 1 | 0 | x_1 |
| +-------+-----------+---------------+
| | 1 | 1 | x_2 |
| +-------+-----------+---------------+
| | 1 | 0 | x_3 |
| +-------+-----------+---------------+
| | 2 | 0 | x_4 |
| +-------+-----------+---------------+
| | 2 | 1 | x_5 |
| +-------+-----------+---------------+
| | 2 | 1 | x_6 |
| +-------+-----------+---------------+
| | 2 | 1 | x_7 |
| +-------+-----------+---------------+
|
| then :py:meth:`fit` method can be called with either `group` array as ``[3, 4]``
| or with `qid` as ``[1, 1, 1, 2, 2, 2, 2]``, that is the qid column. Also, the
| `qid` can be a special column of input `X` instead of a separated parameter, see
| :py:meth:`fit` for more info.
|
| Method resolution order:
| XGBRanker
| XGBRankerMixIn
| XGBModel
| sklearn.base.BaseEstimator
| sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin
| sklearn.utils._metadata_requests._MetadataRequester
| builtins.object
|
| Methods defined here:
|
| __init__(self, *, objective: str = 'rank:ndcg', **kwargs: Any)
| Initialize self. See help(type(self)) for accurate signature.
|
| apply(self, X: Any, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> Any
| Return the predicted leaf every tree for each sample. If the model is trained
| with early stopping, then :py:attr:`best_iteration` is used automatically.
|
| Parameters
| ----------
| X :
| Input features matrix. See :ref:`py-data` for a list of supported types.
|
| iteration_range :
| See :py:meth:`predict`.
|
| Returns
| -------
| X_leaves : array_like, shape=[n_samples, n_trees]
| For each datapoint x in X and for each tree, return the index of the
| leaf x ends up in. Leaves are numbered within
| ``[0; 2**(self.max_depth+1))``, possibly with gaps in the numbering.
|
| fit(self, X: Any, y: Any, *, group: Optional[Any] = None, qid: Optional[Any] = None, sample_weight: Optional[Any] = None, base_margin: Optional[Any] = None, eval_set: Optional[Sequence[Tuple[Any, Any]]] = None, eval_group: Optional[Sequence[Any]] = None, eval_qid: Optional[Sequence[Any]] = None, verbose: Union[bool, int, NoneType] = True, xgb_model: Union[xgboost.core.Booster, str, xgboost.sklearn.XGBModel, NoneType] = None, sample_weight_eval_set: Optional[Sequence[Any]] = None, base_margin_eval_set: Optional[Sequence[Any]] = None, feature_weights: Optional[Any] = None) -> 'XGBRanker'
| Fit gradient boosting ranker
|
| Note that calling ``fit()`` multiple times will cause the model object to be
| re-fit from scratch. To resume training from a previous checkpoint, explicitly
| pass ``xgb_model`` argument.
|
| Parameters
| ----------
| X :
| Feature matrix. See :ref:`py-data` for a list of supported types.
|
| When this is a :py:class:`pandas.DataFrame` or a :py:class:`cudf.DataFrame`,
| it may contain a special column called ``qid`` for specifying the query
| index. Using a special column is the same as using the `qid` parameter,
| except for being compatible with sklearn utility functions like
| :py:func:`sklearn.model_selection.cross_validation`. The same convention
| applies to the :py:meth:`XGBRanker.score` and :py:meth:`XGBRanker.predict`.
|
| +-----+----------------+----------------+
| | qid | feat_0 | feat_1 |
| +-----+----------------+----------------+
| | 0 | :math:`x_{00}` | :math:`x_{01}` |
| +-----+----------------+----------------+
| | 1 | :math:`x_{10}` | :math:`x_{11}` |
| +-----+----------------+----------------+
| | 1 | :math:`x_{20}` | :math:`x_{21}` |
| +-----+----------------+----------------+
|
| When the ``tree_method`` is set to ``hist``, internally, the
| :py:class:`QuantileDMatrix` will be used instead of the :py:class:`DMatrix`
| for conserving memory. However, this has performance implications when the
| device of input data is not matched with algorithm. For instance, if the
| input is a numpy array on CPU but ``cuda`` is used for training, then the
| data is first processed on CPU then transferred to GPU.
| y :
| Labels
| group :
| Size of each query group of training data. Should have as many elements as
| the query groups in the training data. If this is set to None, then user
| must provide qid.
| qid :
| Query ID for each training sample. Should have the size of n_samples. If
| this is set to None, then user must provide group or a special column in X.
| sample_weight :
| Query group weights
|
| .. note:: Weights are per-group for ranking tasks
|
| In ranking task, one weight is assigned to each query group/id (not each
| data point). This is because we only care about the relative ordering of
| data points within each group, so it doesn't make sense to assign
| weights to individual data points.
|
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| eval_set :
| A list of (X, y) tuple pairs to use as validation sets, for which
| metrics will be computed.
| Validation metrics will help us track the performance of the model.
| eval_group :
| A list in which ``eval_group[i]`` is the list containing the sizes of all
| query groups in the ``i``-th pair in **eval_set**.
| eval_qid :
| A list in which ``eval_qid[i]`` is the array containing query ID of ``i``-th
| pair in **eval_set**. The special column convention in `X` applies to
| validation datasets as well.
|
| verbose :
| If `verbose` is True and an evaluation set is used, the evaluation metric
| measured on the validation set is printed to stdout at each boosting stage.
| If `verbose` is an integer, the evaluation metric is printed at each
| `verbose` boosting stage. The last boosting stage / the boosting stage found
| by using `early_stopping_rounds` is also printed.
| xgb_model :
| file name of stored XGBoost model or 'Booster' instance XGBoost model to be
| loaded before training (allows training continuation).
| sample_weight_eval_set :
| A list of the form [L_1, L_2, ..., L_n], where each L_i is a list of
| group weights on the i-th validation set.
|
| .. note:: Weights are per-group for ranking tasks
|
| In ranking task, one weight is assigned to each query group (not each
| data point). This is because we only care about the relative ordering of
| data points within each group, so it doesn't make sense to assign
| weights to individual data points.
| base_margin_eval_set :
| A list of the form [M_1, M_2, ..., M_n], where each M_i is an array like
| object storing base margin for the i-th validation set.
| feature_weights :
| Weight for each feature, defines the probability of each feature being
| selected when colsample is being used. All values must be greater than 0,
| otherwise a `ValueError` is thrown.
|
| predict(self, X: Any, *, output_margin: bool = False, validate_features: bool = True, base_margin: Optional[Any] = None, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> Any
| Predict with `X`. If the model is trained with early stopping, then
| :py:attr:`best_iteration` is used automatically. The estimator uses
| `inplace_predict` by default and falls back to using :py:class:`DMatrix` if
| devices between the data and the estimator don't match.
|
| .. note:: This function is only thread safe for `gbtree` and `dart`.
|
| Parameters
| ----------
| X :
| Data to predict with. See :ref:`py-data` for a list of supported types.
| output_margin :
| Whether to output the raw untransformed margin value.
| validate_features :
| When this is True, validate that the Booster's and data's feature_names are
| identical. Otherwise, it is assumed that the feature_names are the same.
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying ``iteration_range=(10,
| 20)``, then only the forests built during [10, 20) (half open set) rounds
| are used in this prediction.
|
| .. versionadded:: 1.4.0
|
| Returns
| -------
| prediction
|
| score(self, X: Any, y: Any) -> float
| Evaluate score for data using the last evaluation metric. If the model is
| trained with early stopping, then :py:attr:`best_iteration` is used
| automatically.
|
| Parameters
| ----------
| X : Union[pd.DataFrame, cudf.DataFrame]
| Feature matrix. A DataFrame with a special `qid` column.
|
| y :
| Labels
|
| Returns
| -------
| score :
| The result of the first evaluation metric for the ranker.
|
| set_fit_request(self: xgboost.sklearn.XGBRanker, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', base_margin_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', eval_group: Union[bool, NoneType, str] = '$UNCHANGED$', eval_qid: Union[bool, NoneType, str] = '$UNCHANGED$', eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', feature_weights: Union[bool, NoneType, str] = '$UNCHANGED$', group: Union[bool, NoneType, str] = '$UNCHANGED$', qid: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', verbose: Union[bool, NoneType, str] = '$UNCHANGED$', xgb_model: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRanker from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``fit`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``fit`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``fit``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``fit``.
|
| base_margin_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin_eval_set`` parameter in ``fit``.
|
| eval_group : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``eval_group`` parameter in ``fit``.
|
| eval_qid : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``eval_qid`` parameter in ``fit``.
|
| eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``eval_set`` parameter in ``fit``.
|
| feature_weights : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``feature_weights`` parameter in ``fit``.
|
| group : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``group`` parameter in ``fit``.
|
| qid : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``qid`` parameter in ``fit``.
|
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``fit``.
|
| sample_weight_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight_eval_set`` parameter in ``fit``.
|
| verbose : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``verbose`` parameter in ``fit``.
|
| xgb_model : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``xgb_model`` parameter in ``fit``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_predict_request(self: xgboost.sklearn.XGBRanker, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', iteration_range: Union[bool, NoneType, str] = '$UNCHANGED$', output_margin: Union[bool, NoneType, str] = '$UNCHANGED$', validate_features: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRanker from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``predict`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``predict`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``predict``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``predict``.
|
| iteration_range : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``iteration_range`` parameter in ``predict``.
|
| output_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``output_margin`` parameter in ``predict``.
|
| validate_features : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``validate_features`` parameter in ``predict``.
|
| Returns
| -------
| self : object
| The updated object.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from XGBRankerMixIn:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Methods inherited from XGBModel:
|
| __sklearn_is_fitted__(self) -> bool
|
| __sklearn_tags__(self) -> object
|
| evals_result(self) -> Dict[str, Dict[str, List[float]]]
| Return the evaluation results.
|
| If **eval_set** is passed to the :py:meth:`fit` function, you can call
| ``evals_result()`` to get evaluation results for all passed **eval_sets**. When
| **eval_metric** is also passed to the :py:meth:`fit` function, the
| **evals_result** will contain the **eval_metrics** passed to the :py:meth:`fit`
| function.
|
| The returned evaluation result is a dictionary:
|
| .. code-block:: python
|
| {'validation_0': {'logloss': ['0.604835', '0.531479']},
| 'validation_1': {'logloss': ['0.41965', '0.17686']}}
|
| Returns
| -------
| evals_result
|
| get_booster(self) -> xgboost.core.Booster
| Get the underlying xgboost Booster of this model.
|
| This will raise an exception when fit was not called
|
| Returns
| -------
| booster : a xgboost booster of underlying model
|
| get_num_boosting_rounds(self) -> int
| Gets the number of xgboost boosting rounds.
|
| get_params(self, deep: bool = True) -> Dict[str, Any]
| Get parameters.
|
| get_xgb_params(self) -> Dict[str, Any]
| Get xgboost specific parameters.
|
| load_model(self, fname: Union[os.PathLike[~AnyStr], bytearray, str]) -> None
| Load the model from a file or a bytearray.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| model.load_model("model.json")
|
| # or
| model.save_model("model.ubj")
| model.load_model("model.ubj")
|
| # or
| buf = model.save_raw()
| model.load_model(buf)
|
| Parameters
| ----------
| fname :
| Input file name or memory buffer(see also save_raw)
|
| save_model(self, fname: Union[str, os.PathLike]) -> None
| Save the model to a file.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| # or
| model.save_model("model.ubj")
|
| Parameters
| ----------
| fname :
| Output file name
|
| set_params(self, **params: Any) -> 'XGBModel'
| Set the parameters of this estimator. Modification of the sklearn method to
| allow unknown kwargs. This allows using the full range of xgboost
| parameters that are not defined as member variables in sklearn grid
| search.
|
| Returns
| -------
| self
|
| ----------------------------------------------------------------------
| Readonly properties inherited from XGBModel:
|
| best_iteration
| The best iteration obtained by early stopping. This attribute is 0-based,
| for instance if the best iteration is the first round, then best_iteration is 0.
|
| best_score
| The best score obtained by early stopping.
|
| coef_
| Coefficients property
|
| .. note:: Coefficients are defined only for linear learners
|
| Coefficients are only defined when the linear model is chosen as
| base learner (`booster=gblinear`). It is not defined for other base
| learner types, such as tree learners (`booster=gbtree`).
|
| Returns
| -------
| coef_ : array of shape ``[n_features]`` or ``[n_classes, n_features]``
|
| feature_importances_
| Feature importances property, return depends on `importance_type`
| parameter. When model trained with multi-class/multi-label/multi-target dataset,
| the feature importance is "averaged" over all targets. The "average" is defined
| based on the importance type. For instance, if the importance type is
| "total_gain", then the score is sum of loss change for each split from all
| trees.
|
| Returns
| -------
| feature_importances_ : array of shape ``[n_features]`` except for multi-class
| linear model, which returns an array with shape `(n_features, n_classes)`
|
| feature_names_in_
| Names of features seen during :py:meth:`fit`. Defined only when `X` has
| feature names that are all strings.
|
| intercept_
| Intercept (bias) property
|
| For tree-based model, the returned value is the `base_score`.
|
| Returns
| -------
| intercept_ : array of shape ``(1,)`` or ``[n_classes]``
|
| n_features_in_
| Number of features seen during :py:meth:`fit`.
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.BaseEstimator:
|
| __getstate__(self)
| Helper for pickle.
|
| __repr__(self, N_CHAR_MAX=700)
| Return repr(self).
|
| __setstate__(self, state)
|
| __sklearn_clone__(self)
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| get_metadata_routing(self)
| Get metadata routing of this object.
|
| Please check :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| Returns
| -------
| routing : MetadataRequest
| A :class:`~sklearn.utils.metadata_routing.MetadataRequest` encapsulating
| routing information.
|
| ----------------------------------------------------------------------
| Class methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| __init_subclass__(**kwargs)
| Set the ``set_{method}_request`` methods.
|
| This uses PEP-487 [1]_ to set the ``set_{method}_request`` methods. It
| looks for the information available in the set default values which are
| set using ``__metadata_request__*`` class attributes, or inferred
| from method signatures.
|
| The ``__metadata_request__*`` class attributes are used when a method
| does not explicitly accept a metadata through its arguments or if the
| developer would like to specify a request value for those metadata
| which are different from the default ``None``.
|
| References
| ----------
| .. [1] https://www.python.org/dev/peps/pep-0487
class XGBRankerMixIn(builtins.object)
| MixIn for ranking, defines the _estimator_type usually defined in scikit-learn
| base classes.
|
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
class XGBRegressor(sklearn.base.RegressorMixin, XGBModel)
| XGBRegressor(*, objective: Union[str, xgboost.sklearn._SklObjWProto, Callable[[Any, Any], Tuple[numpy.ndarray, numpy.ndarray]], NoneType] = 'reg:squarederror', **kwargs: Any) -> None
|
| Implementation of the scikit-learn API for XGBoost regression.
| See :doc:`/python/sklearn_estimator` for more information.
|
| Parameters
| ----------
|
| n_estimators : typing.Optional[int]
| Number of gradient boosted trees. Equivalent to number of boosting
| rounds.
|
| max_depth : typing.Optional[int]
|
| Maximum tree depth for base learners.
|
| max_leaves : typing.Optional[int]
|
| Maximum number of leaves; 0 indicates no limit.
|
| max_bin : typing.Optional[int]
|
| If using histogram-based algorithm, maximum number of bins per feature
|
| grow_policy : typing.Optional[str]
|
| Tree growing policy.
|
| - depthwise: Favors splitting at nodes closest to the node,
| - lossguide: Favors splitting at nodes with highest loss change.
|
| learning_rate : typing.Optional[float]
|
| Boosting learning rate (xgb's "eta")
|
| verbosity : typing.Optional[int]
|
| The degree of verbosity. Valid values are 0 (silent) - 3 (debug).
|
| objective : typing.Union[str, xgboost.sklearn._SklObjWProto, typing.Callable[[typing.Any, typing.Any], typing.Tuple[numpy.ndarray, numpy.ndarray]], NoneType]
|
| Specify the learning task and the corresponding learning objective or a custom
| objective function to be used.
|
| For custom objective, see :doc:`/tutorials/custom_metric_obj` and
| :ref:`custom-obj-metric` for more information, along with the end note for
| function signatures.
|
| booster: typing.Optional[str]
|
| Specify which booster to use: ``gbtree``, ``gblinear`` or ``dart``.
|
| .. deprecated:: 3.3.0
|
| ``gblinear`` is deprecated and support will be removed in a future release.
|
| tree_method : typing.Optional[str]
|
| Specify which tree method to use. Default to auto. If this parameter is set to
| default, XGBoost will choose the most conservative option available. It's
| recommended to study this option from the parameters document :doc:`tree method
| </treemethod>`
|
| n_jobs : typing.Optional[int]
|
| Number of parallel threads used to run xgboost. When used with other
| Scikit-Learn algorithms like grid search, you may choose which algorithm to
| parallelize and balance the threads. Creating thread contention will
| significantly slow down both algorithms.
|
| gamma : typing.Optional[float]
|
| (min_split_loss) Minimum loss reduction required to make a further partition on
| a leaf node of the tree.
|
| min_child_weight : typing.Optional[float]
|
| Minimum sum of instance weight(hessian) needed in a child.
|
| max_delta_step : typing.Optional[float]
|
| Maximum delta step we allow each tree's weight estimation to be.
|
| subsample : typing.Optional[float]
|
| Subsample ratio of the training instance.
|
| sampling_method : typing.Optional[str]
|
| Sampling method. Used only by the GPU version of ``hist`` tree method.
|
| - ``uniform``: Select random training instances uniformly.
| - ``gradient_based``: Select random training instances with higher probability
| when the gradient and hessian are larger. (cf. CatBoost)
|
| colsample_bytree : typing.Optional[float]
|
| Subsample ratio of columns when constructing each tree.
|
| colsample_bylevel : typing.Optional[float]
|
| Subsample ratio of columns for each level.
|
| colsample_bynode : typing.Optional[float]
|
| Subsample ratio of columns for each split.
|
| reg_alpha : typing.Optional[float]
|
| L1 regularization term on weights (xgb's alpha).
|
| reg_lambda : typing.Optional[float]
|
| L2 regularization term on weights (xgb's lambda).
|
| scale_pos_weight : typing.Optional[float]
| Balancing of positive and negative weights.
|
| base_score : typing.Union[float, typing.List[float], NoneType]
|
| The initial prediction score of all instances, global bias.
|
| random_state : typing.Union[numpy.random.mtrand.RandomState, numpy.random._generator.Generator, int, NoneType]
|
| Random number seed.
|
| .. note::
|
| Using gblinear booster with shotgun updater is nondeterministic as
| it uses Hogwild algorithm.
|
| missing : float
|
| Value in the data which needs to be present as a missing value. Default to
| :py:data:`numpy.nan`.
|
| num_parallel_tree: typing.Optional[int]
|
| Used for boosting random forest.
|
| monotone_constraints : typing.Union[typing.Dict[str, int], str, NoneType]
|
| Constraint of variable monotonicity. See :doc:`tutorial </tutorials/monotonic>`
| for more information.
|
| interaction_constraints : typing.Union[str, typing.List[typing.Tuple[str]], NoneType]
|
| Constraints for interaction representing permitted interactions. The
| constraints must be specified in the form of a nested list, e.g. ``[[0, 1], [2,
| 3, 4]]``, where each inner list is a group of indices of features that are
| allowed to interact with each other. See :doc:`tutorial
| </tutorials/feature_interaction_constraint>` for more information
|
| importance_type: typing.Optional[str]
|
| The feature importance type for the feature_importances\_ property:
|
| * For tree model, it's either "gain", "weight", "cover", "total_gain" or
| "total_cover".
| * For linear model, only "weight" is defined and it's the normalized
| coefficients without bias.
|
| device : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| Device ordinal, available options are `cpu`, `cuda`, and `gpu`.
|
| validate_parameters : typing.Optional[bool]
|
| Give warnings for unknown parameter.
|
| enable_categorical : bool
|
| See the same parameter of :py:class:`DMatrix` for details.
|
| feature_types : typing.Optional[typing.Sequence[str]]
|
| .. versionadded:: 1.7.0
|
| Used for specifying feature types without constructing a dataframe. See
| the :py:class:`DMatrix` for details.
|
| feature_weights : Optional[ArrayLike]
|
| Weight for each feature, defines the probability of each feature being selected
| when colsample is being used. All values must be greater than 0, otherwise a
| `ValueError` is thrown.
|
| max_cat_to_onehot : Optional[int]
|
| .. versionadded:: 1.6.0
|
| .. note:: This parameter is experimental
|
| A threshold for deciding whether XGBoost should use one-hot encoding based split
| for categorical data. When number of categories is lesser than the threshold
| then one-hot encoding is chosen, otherwise the categories will be partitioned
| into children nodes. Also, `enable_categorical` needs to be set to have
| categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| max_cat_threshold : typing.Optional[int]
|
| .. versionadded:: 1.7.0
|
| .. note:: This parameter is experimental
|
| Maximum number of categories considered for each split. Used only by
| partition-based splits for preventing over-fitting. Also, `enable_categorical`
| needs to be set to have categorical feature support. See :doc:`Categorical Data
| </tutorials/categorical>` and :ref:`cat-param` for details.
|
| multi_strategy : typing.Optional[str]
|
| .. versionadded:: 2.0.0
|
| .. note:: This parameter is working-in-progress.
|
| The strategy used for training multi-target models, including multi-target
| regression and multi-class classification. See :doc:`/tutorials/multioutput` for
| more information.
|
| - ``one_output_per_tree``: One model for each target.
| - ``multi_output_tree``: Use multi-target trees.
|
| eval_metric : typing.Union[str, typing.List[typing.Union[str, typing.Callable]], typing.Callable, NoneType]
|
| .. versionadded:: 1.6.0
|
| Metric used for monitoring the training result and early stopping. It can be a
| string or list of strings as names of predefined metric in XGBoost (See
| :doc:`/parameter`), one of the metrics in :py:mod:`sklearn.metrics`, or any
| other user defined metric that looks like `sklearn.metrics`.
|
| If custom objective is also provided, then custom metric should implement the
| corresponding reverse link function.
|
| Unlike the `scoring` parameter commonly used in scikit-learn, when a callable
| object is provided, it's assumed to be a cost function and by default XGBoost
| will minimize the result during early stopping.
|
| For advanced usage on Early stopping like directly choosing to maximize instead
| of minimize, see :py:obj:`xgboost.callback.EarlyStopping`.
|
| See :doc:`/tutorials/custom_metric_obj` and :ref:`custom-obj-metric` for more
| information.
|
| .. code-block:: python
|
| from sklearn.datasets import load_diabetes
| from sklearn.metrics import mean_absolute_error
| X, y = load_diabetes(return_X_y=True)
| reg = xgb.XGBRegressor(
| tree_method="hist",
| eval_metric=mean_absolute_error,
| )
| reg.fit(X, y, eval_set=[(X, y)])
|
| early_stopping_rounds : typing.Optional[int]
|
| .. versionadded:: 1.6.0
|
| - Activates early stopping. Validation metric needs to improve at least once in
| every **early_stopping_rounds** round(s) to continue training. Requires at
| least one item in **eval_set** in :py:meth:`fit`.
|
| - If early stopping occurs, the model will have two additional attributes:
| :py:attr:`best_score` and :py:attr:`best_iteration`. These are used by the
| :py:meth:`predict` and :py:meth:`apply` methods to determine the optimal
| number of trees during inference. If users want to access the full model
| (including trees built after early stopping), they can specify the
| `iteration_range` in these inference methods. In addition, other utilities
| like model plotting can also use the entire model.
|
| - If you prefer to discard the trees after `best_iteration`, consider using the
| callback function :py:class:`xgboost.callback.EarlyStopping`.
|
| - If there's more than one item in **eval_set**, the last entry will be used for
| early stopping. If there's more than one metric in **eval_metric**, the last
| metric will be used for early stopping.
|
| callbacks : typing.Optional[typing.List[xgboost.callback.TrainingCallback]]
|
| List of callback functions that are applied at end of each iteration.
| It is possible to use predefined callbacks by using
| :ref:`Callback API <callback_api>`.
|
| .. note::
|
| States in callback are not preserved during training, which means callback
| objects can not be reused for multiple training sessions without
| reinitialization or deepcopy.
|
| .. code-block:: python
|
| for params in parameters_grid:
| # be sure to (re)initialize the callbacks before each run
| callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
| reg = xgboost.XGBRegressor(**params, callbacks=callbacks)
| reg.fit(X, y)
|
| kwargs : typing.Optional[typing.Any]
|
| Keyword arguments for XGBoost Booster object. Full documentation of parameters
| can be found :doc:`here </parameter>`.
| Attempting to set a parameter via the constructor args and \*\*kwargs
| dict simultaneously will result in a TypeError.
|
| .. note:: \*\*kwargs unsupported by scikit-learn
|
| \*\*kwargs is unsupported by scikit-learn. We do not guarantee
| that parameters passed via this argument will interact properly
| with scikit-learn.
|
| .. note:: Custom objective function
|
| A custom objective function can be provided for the ``objective``
| parameter. In this case, it should have the signature ``objective(y_true,
| y_pred) -> [grad, hess]`` or ``objective(y_true, y_pred, *, sample_weight)
| -> [grad, hess]``:
|
| y_true: array_like of shape [n_samples]
| The target values
| y_pred: array_like of shape [n_samples]
| The predicted values
| sample_weight :
| Optional sample weights.
|
| grad: array_like of shape [n_samples]
| The value of the gradient for each sample point.
| hess: array_like of shape [n_samples]
| The value of the second derivative for each sample point
|
| Note that, if the custom objective produces negative values for
| the Hessian, these will be clipped. If the objective is non-convex,
| one might also consider using the expected Hessian (Fisher
| information) instead.
|
| Method resolution order:
| XGBRegressor
| sklearn.base.RegressorMixin
| XGBModel
| sklearn.base.BaseEstimator
| sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin
| sklearn.utils._metadata_requests._MetadataRequester
| builtins.object
|
| Methods defined here:
|
| __init__(self, *, objective: Union[str, xgboost.sklearn._SklObjWProto, Callable[[Any, Any], Tuple[numpy.ndarray, numpy.ndarray]], NoneType] = 'reg:squarederror', **kwargs: Any) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| __sklearn_tags__(self) -> object
|
| set_fit_request(self: xgboost.sklearn.XGBRegressor, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', base_margin_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', feature_weights: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$', sample_weight_eval_set: Union[bool, NoneType, str] = '$UNCHANGED$', verbose: Union[bool, NoneType, str] = '$UNCHANGED$', xgb_model: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRegressor from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``fit`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``fit`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``fit``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``fit``.
|
| base_margin_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin_eval_set`` parameter in ``fit``.
|
| eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``eval_set`` parameter in ``fit``.
|
| feature_weights : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``feature_weights`` parameter in ``fit``.
|
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``fit``.
|
| sample_weight_eval_set : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight_eval_set`` parameter in ``fit``.
|
| verbose : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``verbose`` parameter in ``fit``.
|
| xgb_model : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``xgb_model`` parameter in ``fit``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_predict_request(self: xgboost.sklearn.XGBRegressor, *, base_margin: Union[bool, NoneType, str] = '$UNCHANGED$', iteration_range: Union[bool, NoneType, str] = '$UNCHANGED$', output_margin: Union[bool, NoneType, str] = '$UNCHANGED$', validate_features: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRegressor from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``predict`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``predict`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``predict``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| base_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``base_margin`` parameter in ``predict``.
|
| iteration_range : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``iteration_range`` parameter in ``predict``.
|
| output_margin : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``output_margin`` parameter in ``predict``.
|
| validate_features : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``validate_features`` parameter in ``predict``.
|
| Returns
| -------
| self : object
| The updated object.
|
| set_score_request(self: xgboost.sklearn.XGBRegressor, *, sample_weight: Union[bool, NoneType, str] = '$UNCHANGED$') -> xgboost.sklearn.XGBRegressor from sklearn.utils._metadata_requests.RequestMethod.__get__.<locals>
| Request metadata passed to the ``score`` method.
|
| Note that this method is only relevant if
| ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
| Please see :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| The options for each parameter are:
|
| - ``True``: metadata is requested, and passed to ``score`` if provided. The request is ignored if metadata is not provided.
|
| - ``False``: metadata is not requested and the meta-estimator will not pass it to ``score``.
|
| - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
|
| - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.
|
| The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
| existing request. This allows you to change the request for some
| parameters and not others.
|
| .. versionadded:: 1.3
|
| .. note::
| This method is only relevant if this estimator is used as a
| sub-estimator of a meta-estimator, e.g. used inside a
| :class:`~sklearn.pipeline.Pipeline`. Otherwise it has no effect.
|
| Parameters
| ----------
| sample_weight : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
| Metadata routing for ``sample_weight`` parameter in ``score``.
|
| Returns
| -------
| self : object
| The updated object.
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.RegressorMixin:
|
| score(self, X, y, sample_weight=None)
| Return the coefficient of determination of the prediction.
|
| The coefficient of determination :math:`R^2` is defined as
| :math:`(1 - \frac{u}{v})`, where :math:`u` is the residual
| sum of squares ``((y_true - y_pred)** 2).sum()`` and :math:`v`
| is the total sum of squares ``((y_true - y_true.mean()) ** 2).sum()``.
| The best possible score is 1.0 and it can be negative (because the
| model can be arbitrarily worse). A constant model that always predicts
| the expected value of `y`, disregarding the input features, would get
| a :math:`R^2` score of 0.0.
|
| Parameters
| ----------
| X : array-like of shape (n_samples, n_features)
| Test samples. For some estimators this may be a precomputed
| kernel matrix or a list of generic objects instead with shape
| ``(n_samples, n_samples_fitted)``, where ``n_samples_fitted``
| is the number of samples used in the fitting for the estimator.
|
| y : array-like of shape (n_samples,) or (n_samples, n_outputs)
| True values for `X`.
|
| sample_weight : array-like of shape (n_samples,), default=None
| Sample weights.
|
| Returns
| -------
| score : float
| :math:`R^2` of ``self.predict(X)`` w.r.t. `y`.
|
| Notes
| -----
| The :math:`R^2` score used when calling ``score`` on a regressor uses
| ``multioutput='uniform_average'`` from version 0.23 to keep consistent
| with default value of :func:`~sklearn.metrics.r2_score`.
| This influences the ``score`` method of all the multioutput
| regressors (except for
| :class:`~sklearn.multioutput.MultiOutputRegressor`).
|
| ----------------------------------------------------------------------
| Data descriptors inherited from sklearn.base.RegressorMixin:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
|
| ----------------------------------------------------------------------
| Methods inherited from XGBModel:
|
| __sklearn_is_fitted__(self) -> bool
|
| apply(self, X: Any, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> numpy.ndarray
| Return the predicted leaf every tree for each sample. If the model is trained
| with early stopping, then :py:attr:`best_iteration` is used automatically.
|
| Parameters
| ----------
| X :
| Input features matrix. See :ref:`py-data` for a list of supported types.
|
| iteration_range :
| See :py:meth:`predict`.
|
| Returns
| -------
| X_leaves : array_like, shape=[n_samples, n_trees]
| For each datapoint x in X and for each tree, return the index of the
| leaf x ends up in. Leaves are numbered within
| ``[0; 2**(self.max_depth+1))``, possibly with gaps in the numbering.
|
| evals_result(self) -> Dict[str, Dict[str, List[float]]]
| Return the evaluation results.
|
| If **eval_set** is passed to the :py:meth:`fit` function, you can call
| ``evals_result()`` to get evaluation results for all passed **eval_sets**. When
| **eval_metric** is also passed to the :py:meth:`fit` function, the
| **evals_result** will contain the **eval_metrics** passed to the :py:meth:`fit`
| function.
|
| The returned evaluation result is a dictionary:
|
| .. code-block:: python
|
| {'validation_0': {'logloss': ['0.604835', '0.531479']},
| 'validation_1': {'logloss': ['0.41965', '0.17686']}}
|
| Returns
| -------
| evals_result
|
| fit(self, X: Any, y: Any, *, sample_weight: Optional[Any] = None, base_margin: Optional[Any] = None, eval_set: Optional[Sequence[Tuple[Any, Any]]] = None, verbose: Union[bool, int, NoneType] = True, xgb_model: Union[xgboost.core.Booster, ForwardRef('XGBModel'), str, NoneType] = None, sample_weight_eval_set: Optional[Sequence[Any]] = None, base_margin_eval_set: Optional[Sequence[Any]] = None, feature_weights: Optional[Any] = None) -> 'XGBModel'
| Fit gradient boosting model.
|
| Note that calling ``fit()`` multiple times will cause the model object to be
| re-fit from scratch. To resume training from a previous checkpoint, explicitly
| pass ``xgb_model`` argument.
|
| Parameters
| ----------
| X :
| Input feature matrix. See :ref:`py-data` for a list of supported types.
|
| When the ``tree_method`` is set to ``hist``, internally, the
| :py:class:`QuantileDMatrix` will be used instead of the :py:class:`DMatrix`
| for conserving memory. However, this has performance implications when the
| device of input data is not matched with algorithm. For instance, if the
| input is a numpy array on CPU but ``cuda`` is used for training, then the
| data is first processed on CPU then transferred to GPU.
| y :
| Labels
| sample_weight :
| instance weights
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| eval_set :
| A list of (X, y) tuple pairs to use as validation sets, for which
| metrics will be computed.
| Validation metrics will help us track the performance of the model.
|
| verbose :
| If `verbose` is True and an evaluation set is used, the evaluation metric
| measured on the validation set is printed to stdout at each boosting stage.
| If `verbose` is an integer, the evaluation metric is printed at each
| `verbose` boosting stage. The last boosting stage / the boosting stage found
| by using `early_stopping_rounds` is also printed.
| xgb_model :
| file name of stored XGBoost model or 'Booster' instance XGBoost model to be
| loaded before training (allows training continuation).
| sample_weight_eval_set :
| A list of the form [L_1, L_2, ..., L_n], where each L_i is an array like
| object storing instance weights for the i-th validation set.
| base_margin_eval_set :
| A list of the form [M_1, M_2, ..., M_n], where each M_i is an array like
| object storing base margin for the i-th validation set.
| feature_weights :
|
| .. deprecated:: 3.0.0
|
| Use `feature_weights` in :py:meth:`__init__` or :py:meth:`set_params`
| instead.
|
| get_booster(self) -> xgboost.core.Booster
| Get the underlying xgboost Booster of this model.
|
| This will raise an exception when fit was not called
|
| Returns
| -------
| booster : a xgboost booster of underlying model
|
| get_num_boosting_rounds(self) -> int
| Gets the number of xgboost boosting rounds.
|
| get_params(self, deep: bool = True) -> Dict[str, Any]
| Get parameters.
|
| get_xgb_params(self) -> Dict[str, Any]
| Get xgboost specific parameters.
|
| load_model(self, fname: Union[os.PathLike[~AnyStr], bytearray, str]) -> None
| Load the model from a file or a bytearray.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| model.load_model("model.json")
|
| # or
| model.save_model("model.ubj")
| model.load_model("model.ubj")
|
| # or
| buf = model.save_raw()
| model.load_model(buf)
|
| Parameters
| ----------
| fname :
| Input file name or memory buffer(see also save_raw)
|
| predict(self, X: Any, *, output_margin: bool = False, validate_features: bool = True, base_margin: Optional[Any] = None, iteration_range: Optional[Tuple[Union[int, numpy.integer], Union[int, numpy.integer]]] = None) -> Any
| Predict with `X`. If the model is trained with early stopping, then
| :py:attr:`best_iteration` is used automatically. The estimator uses
| `inplace_predict` by default and falls back to using :py:class:`DMatrix` if
| devices between the data and the estimator don't match.
|
| .. note:: This function is only thread safe for `gbtree` and `dart`.
|
| Parameters
| ----------
| X :
| Data to predict with. See :ref:`py-data` for a list of supported types.
| output_margin :
| Whether to output the raw untransformed margin value.
| validate_features :
| When this is True, validate that the Booster's and data's feature_names are
| identical. Otherwise, it is assumed that the feature_names are the same.
| base_margin :
| Global bias for each instance. See :doc:`/tutorials/intercept` for details.
| iteration_range :
| Specifies which layer of trees are used in prediction. For example, if a
| random forest is trained with 100 rounds. Specifying ``iteration_range=(10,
| 20)``, then only the forests built during [10, 20) (half open set) rounds
| are used in this prediction.
|
| .. versionadded:: 1.4.0
|
| Returns
| -------
| prediction
|
| save_model(self, fname: Union[str, os.PathLike]) -> None
| Save the model to a file.
|
| The model is saved in an XGBoost internal format which is universal among the
| various XGBoost interfaces. Auxiliary attributes of the Python Booster object
| (such as feature_names) are only saved when using JSON or UBJSON (default)
| format. Also, parameters that are not part of the model (like metrics,
| `max_depth`, etc) are not saved, see :doc:`Model IO </tutorials/saving_model>`
| for more info.
|
| .. code-block:: python
|
| model.save_model("model.json")
| # or
| model.save_model("model.ubj")
|
| Parameters
| ----------
| fname :
| Output file name
|
| set_params(self, **params: Any) -> 'XGBModel'
| Set the parameters of this estimator. Modification of the sklearn method to
| allow unknown kwargs. This allows using the full range of xgboost
| parameters that are not defined as member variables in sklearn grid
| search.
|
| Returns
| -------
| self
|
| ----------------------------------------------------------------------
| Readonly properties inherited from XGBModel:
|
| best_iteration
| The best iteration obtained by early stopping. This attribute is 0-based,
| for instance if the best iteration is the first round, then best_iteration is 0.
|
| best_score
| The best score obtained by early stopping.
|
| coef_
| Coefficients property
|
| .. note:: Coefficients are defined only for linear learners
|
| Coefficients are only defined when the linear model is chosen as
| base learner (`booster=gblinear`). It is not defined for other base
| learner types, such as tree learners (`booster=gbtree`).
|
| Returns
| -------
| coef_ : array of shape ``[n_features]`` or ``[n_classes, n_features]``
|
| feature_importances_
| Feature importances property, return depends on `importance_type`
| parameter. When model trained with multi-class/multi-label/multi-target dataset,
| the feature importance is "averaged" over all targets. The "average" is defined
| based on the importance type. For instance, if the importance type is
| "total_gain", then the score is sum of loss change for each split from all
| trees.
|
| Returns
| -------
| feature_importances_ : array of shape ``[n_features]`` except for multi-class
| linear model, which returns an array with shape `(n_features, n_classes)`
|
| feature_names_in_
| Names of features seen during :py:meth:`fit`. Defined only when `X` has
| feature names that are all strings.
|
| intercept_
| Intercept (bias) property
|
| For tree-based model, the returned value is the `base_score`.
|
| Returns
| -------
| intercept_ : array of shape ``(1,)`` or ``[n_classes]``
|
| n_features_in_
| Number of features seen during :py:meth:`fit`.
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.base.BaseEstimator:
|
| __getstate__(self)
| Helper for pickle.
|
| __repr__(self, N_CHAR_MAX=700)
| Return repr(self).
|
| __setstate__(self, state)
|
| __sklearn_clone__(self)
|
| ----------------------------------------------------------------------
| Methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| get_metadata_routing(self)
| Get metadata routing of this object.
|
| Please check :ref:`User Guide <metadata_routing>` on how the routing
| mechanism works.
|
| Returns
| -------
| routing : MetadataRequest
| A :class:`~sklearn.utils.metadata_routing.MetadataRequest` encapsulating
| routing information.
|
| ----------------------------------------------------------------------
| Class methods inherited from sklearn.utils._metadata_requests._MetadataRequester:
|
| __init_subclass__(**kwargs)
| Set the ``set_{method}_request`` methods.
|
| This uses PEP-487 [1]_ to set the ``set_{method}_request`` methods. It
| looks for the information available in the set default values which are
| set using ``__metadata_request__*`` class attributes, or inferred
| from method signatures.
|
| The ``__metadata_request__*`` class attributes are used when a method
| does not explicitly accept a metadata through its arguments or if the
| developer would like to specify a request value for those metadata
| which are different from the default ``None``.
|
| References
| ----------
| .. [1] https://www.python.org/dev/peps/pep-0487
FUNCTIONS
get_model_categories(X: Any, model: Union[xgboost.core.Booster, str, NoneType], feature_types: Optional[Sequence[str]]) -> Tuple[Union[xgboost.core.Booster, str, NoneType], Union[Sequence[str], xgboost._data_utils.Categories, NoneType]]
Extract the optional reference categories from the booster. Used for training
continuation. The result should be passed to the :py:func:`pick_ref_categories`.
ltr_metric_decorator(func: Callable, n_jobs: Optional[int]) -> Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[str, float]]
Decorate a learning to rank metric.
pick_ref_categories(X: Any, model_cats: Union[Sequence[str], xgboost._data_utils.Categories, NoneType], Xy_cats: Optional[xgboost._data_utils.Categories]) -> Union[Sequence[str], xgboost._data_utils.Categories, NoneType]
Use the reference categories from the model. If none, then use the reference
categories from the training DMatrix.
Parameters
----------
X :
Input feature matrix.
model_cats :
Optional categories stored in the previous model (training continuation). This
should come from the :py:func:`get_model_categories`.
Xy_cats :
Optional categories from the training DMatrix. Used for re-coding the validation
dataset.
xgboost_model_doc(header: str, items: List[str], extra_parameters: Optional[str] = None, end_note: Optional[str] = None) -> Callable[[~TDoc], ~TDoc]
Obtain documentation for Scikit-Learn wrappers
Parameters
----------
header: str
An introducion to the class.
items : list
A list of common doc items. Available items are:
- estimators: the meaning of n_estimators
- model: All the other parameters
- objective: note for customized objective
extra_parameters: str
Document for class specific parameters, placed at the head.
end_note: str
Extra notes put to the end.
DATA
CAT_T = 'c'
Callable = typing.Callable
Deprecated alias to collections.abc.Callable.
Callable[[int], str] signifies a function that takes a single
parameter of type int and returns a str.
The subscription syntax must always be used with exactly two
values: the argument list and the return type.
The argument list must be a list of types, a ParamSpec,
Concatenate or ellipsis. The return type must be a single type.
There is no syntax to indicate optional or keyword arguments;
such function types are rarely used as callback types.
DEFAULT_N_ESTIMATORS = 100
Dict = typing.Dict
A generic version of dict.
EvalsLog = typing.Dict[str, typing.Dict[str, typing.Union[t...loat], t...
FeatureNames = typing.Sequence[str]
FeatureTypes = typing.Sequence[str]
IterationRange = typing.Tuple[typing.Union[int, numpy.integer], typing...
List = typing.List
A generic version of list.
Metric = typing.Callable[[numpy.ndarray, xgboost.core.DMatrix], typing...
ModelIn = typing.Union[os.PathLike[~AnyStr], bytearray, str]
Optional = typing.Optional
Optional[X] is equivalent to Union[X, None].
PlainObj = typing.Callable[[numpy.ndarray, xgboost.core.DMatrix], typi...
PredtT = ~PredtT
SKLEARN_INSTALLED = True
Sequence = typing.Sequence
A generic version of collections.abc.Sequence.
Set = typing.Set
A generic version of set.
SklObjective = typing.Union[str, xgboost.sklearn._SklObjWProto,...g.Tu...
TDoc = ~TDoc
Tuple = typing.Tuple
Deprecated alias to builtins.tuple.
Tuple[X, Y] is the cross-product type of X and Y.
Example: Tuple[T1, T2] is a tuple of two elements corresponding
to type variables T1 and T2. Tuple[int, float, str] is a tuple
of an int, a float and a string.
To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
Type = typing.Type
Deprecated alias to builtins.type.
builtins.type or typing.Type can be used to annotate class objects.
For example, suppose we have the following classes::
class User: ... # Abstract base for User classes
class BasicUser(User): ...
class ProUser(User): ...
class TeamUser(User): ...
And a function that takes a class argument that's a subclass of
User and returns an instance of the corresponding class::
def new_user[U](user_class: Type[U]) -> U:
user = user_class()
# (Here we could write the user object to a database)
return user
joe = new_user(BasicUser)
At this point the type checker knows that joe has type BasicUser.
Union = typing.Union
Union type; Union[X, Y] means either X or Y.
On Python 3.10 and higher, the | operator
can also be used to denote unions;
X | Y means the same thing to the type checker as Union[X, Y].
To define a union, use e.g. Union[int, str]. Details:
- The arguments must be types and there must be at least one.
- None as an argument is a special case and is replaced by
type(None).
- Unions of unions are flattened, e.g.::
assert Union[Union[int, str], float] == Union[int, str, float]
- Unions of a single argument vanish, e.g.::
assert Union[int] == int # The constructor actually returns int
- Redundant arguments are skipped, e.g.::
assert Union[int, str, int] == Union[int, str]
- When comparing unions, the argument order is ignored, e.g.::
assert Union[int, str] == Union[str, int]
- You cannot subclass or instantiate a union.
- You can use Optional[X] as a shorthand for Union[X, None].
FILE
c:\users\youse\appdata\local\programs\python\python312\lib\site-packages\xgboost\sklearn.py
xgboost.to_graphviz
Python Library Documentation: function to_graphviz in module xgboost.plotting
to_graphviz(booster: Union[xgboost.core.Booster, xgboost.sklearn.XGBModel], *, fmap: Union[str, os.PathLike] = '', num_trees: Optional[int] = None, rankdir: Optional[str] = None, yes_color: Optional[str] = None, no_color: Optional[str] = None, condition_node_params: Optional[dict] = None, leaf_node_params: Optional[dict] = None, with_stats: bool = False, tree_idx: int = 0, **kwargs: Any) -> Any
Convert specified tree to graphviz instance. IPython can automatically plot
the returned graphviz instance. Otherwise, you should call .render() method
of the returned graphviz instance.
Parameters
----------
booster :
Booster or XGBModel instance
fmap :
The name of feature map file
num_trees :
.. deprecated:: 3.0
Specify the ordinal number of target tree
rankdir :
Passed to graphviz via graph_attr
yes_color :
Edge color when meets the node condition.
no_color :
Edge color when doesn't meet the node condition.
condition_node_params :
Condition node configuration for for graphviz. Example:
.. code-block:: python
{'shape': 'box',
'style': 'filled,rounded',
'fillcolor': '#78bceb'}
leaf_node_params :
Leaf node configuration for graphviz. Example:
.. code-block:: python
{'shape': 'box',
'style': 'filled',
'fillcolor': '#e48038'}
with_stats :
.. versionadded:: 3.0
Controls whether the split statistics should be included.
tree_idx :
.. versionadded:: 3.0
Specify the ordinal index of target tree.
kwargs :
Other keywords passed to graphviz graph_attr, e.g. ``graph [ {key} = {value} ]``
Returns
-------
graph: graphviz.Source
xgboost.tracker
Python Library Documentation: module xgboost.tracker in xgboost
NAME
xgboost.tracker - Tracker for XGBoost collective.
CLASSES
builtins.object
RabitTracker
class RabitTracker(builtins.object)
| RabitTracker(n_workers: int, host_ip: Optional[str], port: int = 0, *, sortby: str = 'host', timeout: int = 0) -> None
|
| Tracker for the collective used in XGBoost, acting as a coordinator between
| workers.
|
| Parameters
| ----------
|
| n_workers:
|
| The total number of workers in the communication group.
|
| host_ip:
|
| The IP address of the tracker node. XGBoost can try to guess one by probing with
| sockets. But it's best to explicitly pass an address.
|
| port:
|
| The port this tracker should listen to. XGBoost can query an available port from
| the OS, this configuration is useful for restricted network environments.
|
| sortby:
|
| How to sort the workers for rank assignment. The default is host, but users can
| set the `DMLC_TASK_ID` via arguments of :py:meth:`~xgboost.collective.init` and
| obtain deterministic rank assignment through sorting by task name. Available
| options are:
|
| - host
| - task
|
| timeout :
|
| Timeout for constructing (bootstrap) and shutting down the communication group,
| doesn't apply to communication when the group is up and running.
|
| The timeout value should take the time of data loading and pre-processing into
| account, due to potential lazy execution. By default the Tracker doesn't have
| any timeout to avoid pre-mature aborting.
|
| The :py:meth:`.wait_for` method has a different timeout parameter that can stop
| the tracker even if the tracker is still being used. A value error is raised
| when timeout is reached.
|
| Examples
| --------
|
| .. code-block:: python
|
| from xgboost.tracker import RabitTracker
| from xgboost import collective as coll
|
| tracker = RabitTracker(host_ip="127.0.0.1", n_workers=2)
| tracker.start()
|
| with coll.CommunicatorContext(**tracker.worker_args()):
| ret = coll.broadcast("msg", 0)
| assert str(ret) == "msg"
|
| Methods defined here:
|
| __del__(self) -> None
|
| __init__(self, n_workers: int, host_ip: Optional[str], port: int = 0, *, sortby: str = 'host', timeout: int = 0) -> None
| Initialize self. See help(type(self)) for accurate signature.
|
| free(self) -> None
| Internal function for testing.
|
| start(self) -> None
| Start the tracker. Once started, the client still need to call the
| :py:meth:`wait_for` method in order to wait for it to finish (think of it as a
| thread).
|
| wait_for(self, timeout: Optional[int] = None) -> None
| Wait for the tracker to finish all the work and shutdown. When timeout is
| reached, a value error is raised. By default we don't have timeout since we
| don't know how long it takes for the model to finish training.
|
| worker_args(self) -> Dict[str, Union[int, str]]
| Get arguments for workers.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
FUNCTIONS
get_family(addr: str) -> int
Get network family from address.
DATA
Dict = typing.Dict
A generic version of dict.
Optional = typing.Optional
Optional[X] is equivalent to Union[X, None].
Union = typing.Union
Union type; Union[X, Y] means either X or Y.
On Python 3.10 and higher, the | operator
can also be used to denote unions;
X | Y means the same thing to the type checker as Union[X, Y].
To define a union, use e.g. Union[int, str]. Details:
- The arguments must be types and there must be at least one.
- None as an argument is a special case and is replaced by
type(None).
- Unions of unions are flattened, e.g.::
assert Union[Union[int, str], float] == Union[int, str, float]
- Unions of a single argument vanish, e.g.::
assert Union[int] == int # The constructor actually returns int
- Redundant arguments are skipped, e.g.::
assert Union[int, str, int] == Union[int, str]
- When comparing unions, the argument order is ignored, e.g.::
assert Union[int, str] == Union[str, int]
- You cannot subclass or instantiate a union.
- You can use Optional[X] as a shorthand for Union[X, None].
FILE
c:\users\youse\appdata\local\programs\python\python312\lib\site-packages\xgboost\tracker.py
xgboost.train
Python Library Documentation: function train in module xgboost.training
train(params: Dict[str, Any], dtrain: xgboost.core.DMatrix, num_boost_round: int = 10, *, evals: Optional[Sequence[Tuple[xgboost.core.DMatrix, str]]] = None, obj: Optional[Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[numpy.ndarray, numpy.ndarray]]] = None, maximize: Optional[bool] = None, early_stopping_rounds: Optional[int] = None, evals_result: Optional[Dict[str, Dict[str, Union[List[float], List[Tuple[float, float]]]]]] = None, verbose_eval: Union[bool, int, NoneType] = True, xgb_model: Union[str, os.PathLike, xgboost.core.Booster, bytearray, NoneType] = None, callbacks: Optional[Sequence[xgboost.callback.TrainingCallback]] = None, custom_metric: Optional[Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[str, float]]] = None) -> xgboost.core.Booster
Train a booster with given parameters.
Parameters
----------
params :
Booster params.
dtrain :
Data to be trained.
num_boost_round :
Number of boosting iterations.
evals :
List of validation sets for which metrics will evaluated during training.
Validation metrics will help us track the performance of the model.
obj
Custom objective function. See :doc:`Custom Objective
</tutorials/custom_metric_obj>` for details.
maximize :
Whether to maximize custom_metric.
early_stopping_rounds :
Activates early stopping. Validation metric needs to improve at least once in
every **early_stopping_rounds** round(s) to continue training.
Requires at least one item in **evals**.
The method returns the model from the last iteration (not the best one). Use
custom callback :py:class:`~xgboost.callback.EarlyStopping` or :py:meth:`model
slicing <xgboost.Booster.__getitem__>` if the best model is desired. If there's
more than one item in **evals**, the last entry will be used for early stopping.
If there's more than one metric in the **eval_metric** parameter given in
**params**, the last metric will be used for early stopping.
If early stopping occurs, the model will have two additional fields:
``bst.best_score``, ``bst.best_iteration``.
evals_result :
This dictionary stores the evaluation results of all the items in watchlist.
Example: with a watchlist containing
``[(dtest,'eval'), (dtrain,'train')]`` and
a parameter containing ``('eval_metric': 'logloss')``,
the **evals_result** returns
.. code-block:: python
{'train': {'logloss': ['0.48253', '0.35953']},
'eval': {'logloss': ['0.480385', '0.357756']}}
verbose_eval :
Requires at least one item in **evals**.
If **verbose_eval** is True then the evaluation metric on the validation set is
printed at each boosting stage.
If **verbose_eval** is an integer then the evaluation metric on the validation
set is printed at every given **verbose_eval** boosting stage. The last boosting
stage / the boosting stage found by using **early_stopping_rounds** is also
printed.
Example: with ``verbose_eval=4`` and at least one item in **evals**, an
evaluation metric is printed every 4 boosting stages, instead of every boosting
stage.
xgb_model :
Xgb model to be loaded before training (allows training continuation).
callbacks :
List of callback functions that are applied at end of each iteration.
It is possible to use predefined callbacks by using
:ref:`Callback API <callback_api>`.
.. note::
States in callback are not preserved during training, which means callback
objects can not be reused for multiple training sessions without
reinitialization or deepcopy.
.. code-block:: python
for params in parameters_grid:
# be sure to (re)initialize the callbacks before each run
callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
xgboost.train(params, Xy, callbacks=callbacks)
custom_metric:
.. versionadded 1.6.0
Custom metric function. See :doc:`Custom Metric </tutorials/custom_metric_obj>`
for details. The metric receives transformed prediction (after applying the
reverse link function) when using a builtin objective, and raw output when using
a custom objective.
Returns
-------
Booster : a trained booster model
xgboost.training
Python Library Documentation: module xgboost.training in xgboost
NAME
xgboost.training - Training Library containing training routines.
CLASSES
builtins.object
CVPack
class CVPack(builtins.object)
| CVPack(dtrain: xgboost.core.DMatrix, dtest: xgboost.core.DMatrix, param: Union[Dict, List, NoneType]) -> None
|
| "Auxiliary datastruct to hold one fold of CV.
|
| Methods defined here:
|
| __getattr__(self, name: str) -> Callable
|
| __init__(self, dtrain: xgboost.core.DMatrix, dtest: xgboost.core.DMatrix, param: Union[Dict, List, NoneType]) -> None
| Initialize the CVPack.
|
| eval(self, iteration: int, feval: Optional[Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[str, float]]], output_margin: bool) -> str
| "Evaluate the CVPack for one iteration.
|
| update(self, iteration: int, fobj: Optional[Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[numpy.ndarray, numpy.ndarray]]]) -> None
| "Update the boosters for one iteration
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables
|
| __weakref__
| list of weak references to the object
FUNCTIONS
cv(params: Union[List, Dict[str, Any]], dtrain: xgboost.core.DMatrix, num_boost_round: int = 10, *, nfold: int = 3, stratified: bool = False, folds: Optional[sklearn.model_selection._split.StratifiedKFold] = None, metrics: Sequence[str] = (), obj: Optional[Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[numpy.ndarray, numpy.ndarray]]] = None, maximize: Optional[bool] = None, early_stopping_rounds: Optional[int] = None, fpreproc: Optional[Callable] = None, as_pandas: bool = True, verbose_eval: Union[bool, int, NoneType] = None, show_stdv: bool = True, seed: int = 0, callbacks: Optional[Sequence[xgboost.callback.TrainingCallback]] = None, shuffle: bool = True, custom_metric: Optional[Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[str, float]]] = None) -> Union[Dict[str, float], ForwardRef('PdDataFrame')]
Cross-validation with given parameters.
Parameters
----------
params : dict
Booster params.
dtrain :
Data to be trained. Only the :py:class:`DMatrix` without external memory is
supported.
num_boost_round :
Number of boosting iterations.
nfold : int
Number of folds in CV.
stratified : bool
Perform stratified sampling.
folds : a KFold or StratifiedKFold instance or list of fold indices
Sklearn KFolds or StratifiedKFolds object.
Alternatively may explicitly pass sample indices for each fold.
For ``n`` folds, **folds** should be a length ``n`` list of tuples.
Each tuple is ``(in,out)`` where ``in`` is a list of indices to be used
as the training samples for the ``n`` th fold and ``out`` is a list of
indices to be used as the testing samples for the ``n`` th fold.
metrics : string or list of strings
Evaluation metrics to be watched in CV.
obj :
Custom objective function. See :doc:`Custom Objective
</tutorials/custom_metric_obj>` for details.
maximize : bool
Whether to maximize the evaluataion metric (score or error).
early_stopping_rounds: int
Activates early stopping. Cross-Validation metric (average of validation
metric computed over CV folds) needs to improve at least once in
every **early_stopping_rounds** round(s) to continue training.
The last entry in the evaluation history will represent the best iteration.
If there's more than one metric in the **eval_metric** parameter given in
**params**, the last metric will be used for early stopping.
fpreproc : function
Preprocessing function that takes (dtrain, dtest, param) and returns
transformed versions of those.
as_pandas : bool, default True
Return pd.DataFrame when pandas is installed.
If False or pandas is not installed, return np.ndarray
verbose_eval : bool, int, or None, default None
Whether to display the progress. If None, progress will be displayed
when np.ndarray is returned. If True, progress will be displayed at
boosting stage. If an integer is given, progress will be displayed
at every given `verbose_eval` boosting stage.
show_stdv : bool, default True
Whether to display the standard deviation in progress.
Results are not affected, and always contains std.
seed : int
Seed used to generate the folds (passed to numpy.random.seed).
callbacks :
List of callback functions that are applied at end of each iteration.
It is possible to use predefined callbacks by using
:ref:`Callback API <callback_api>`.
.. note::
States in callback are not preserved during training, which means callback
objects can not be reused for multiple training sessions without
reinitialization or deepcopy.
.. code-block:: python
for params in parameters_grid:
# be sure to (re)initialize the callbacks before each run
callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
xgboost.train(params, Xy, callbacks=callbacks)
shuffle : bool
Shuffle data before creating folds.
custom_metric :
.. versionadded 1.6.0
Custom metric function. See :doc:`Custom Metric </tutorials/custom_metric_obj>`
for details.
Returns
-------
evaluation history : list(string)
groups_to_rows(groups: numpy.ndarray, boundaries: numpy.ndarray) -> numpy.ndarray
Given group row boundaries, convert ground indexes to row indexes
:param groups: list of groups for testing
:param boundaries: rows index limits of each group
:return: row in group
mkgroupfold(*, dall: xgboost.core.DMatrix, nfold: int, param: Union[List, Dict[str, Any]], evals: Sequence[str] = (), fpreproc: Optional[Callable] = None, shuffle: bool = True) -> List[xgboost.training.CVPack]
Make n folds for cross-validation maintaining groups
:return: cross-validation folds
mknfold(*, dall: xgboost.core.DMatrix, nfold: int, param: Union[List, Dict[str, Any]], seed: int, evals: Sequence[str] = (), fpreproc: Optional[Callable] = None, stratified: Optional[bool] = False, folds: Optional[sklearn.model_selection._split.StratifiedKFold] = None, shuffle: bool = True) -> List[xgboost.training.CVPack]
Make an n-fold list of CVPack from random indices.
train(params: Dict[str, Any], dtrain: xgboost.core.DMatrix, num_boost_round: int = 10, *, evals: Optional[Sequence[Tuple[xgboost.core.DMatrix, str]]] = None, obj: Optional[Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[numpy.ndarray, numpy.ndarray]]] = None, maximize: Optional[bool] = None, early_stopping_rounds: Optional[int] = None, evals_result: Optional[Dict[str, Dict[str, Union[List[float], List[Tuple[float, float]]]]]] = None, verbose_eval: Union[bool, int, NoneType] = True, xgb_model: Union[str, os.PathLike, xgboost.core.Booster, bytearray, NoneType] = None, callbacks: Optional[Sequence[xgboost.callback.TrainingCallback]] = None, custom_metric: Optional[Callable[[numpy.ndarray, xgboost.core.DMatrix], Tuple[str, float]]] = None) -> xgboost.core.Booster
Train a booster with given parameters.
Parameters
----------
params :
Booster params.
dtrain :
Data to be trained.
num_boost_round :
Number of boosting iterations.
evals :
List of validation sets for which metrics will evaluated during training.
Validation metrics will help us track the performance of the model.
obj
Custom objective function. See :doc:`Custom Objective
</tutorials/custom_metric_obj>` for details.
maximize :
Whether to maximize custom_metric.
early_stopping_rounds :
Activates early stopping. Validation metric needs to improve at least once in
every **early_stopping_rounds** round(s) to continue training.
Requires at least one item in **evals**.
The method returns the model from the last iteration (not the best one). Use
custom callback :py:class:`~xgboost.callback.EarlyStopping` or :py:meth:`model
slicing <xgboost.Booster.__getitem__>` if the best model is desired. If there's
more than one item in **evals**, the last entry will be used for early stopping.
If there's more than one metric in the **eval_metric** parameter given in
**params**, the last metric will be used for early stopping.
If early stopping occurs, the model will have two additional fields:
``bst.best_score``, ``bst.best_iteration``.
evals_result :
This dictionary stores the evaluation results of all the items in watchlist.
Example: with a watchlist containing
``[(dtest,'eval'), (dtrain,'train')]`` and
a parameter containing ``('eval_metric': 'logloss')``,
the **evals_result** returns
.. code-block:: python
{'train': {'logloss': ['0.48253', '0.35953']},
'eval': {'logloss': ['0.480385', '0.357756']}}
verbose_eval :
Requires at least one item in **evals**.
If **verbose_eval** is True then the evaluation metric on the validation set is
printed at each boosting stage.
If **verbose_eval** is an integer then the evaluation metric on the validation
set is printed at every given **verbose_eval** boosting stage. The last boosting
stage / the boosting stage found by using **early_stopping_rounds** is also
printed.
Example: with ``verbose_eval=4`` and at least one item in **evals**, an
evaluation metric is printed every 4 boosting stages, instead of every boosting
stage.
xgb_model :
Xgb model to be loaded before training (allows training continuation).
callbacks :
List of callback functions that are applied at end of each iteration.
It is possible to use predefined callbacks by using
:ref:`Callback API <callback_api>`.
.. note::
States in callback are not preserved during training, which means callback
objects can not be reused for multiple training sessions without
reinitialization or deepcopy.
.. code-block:: python
for params in parameters_grid:
# be sure to (re)initialize the callbacks before each run
callbacks = [xgb.callback.LearningRateScheduler(custom_rates)]
xgboost.train(params, Xy, callbacks=callbacks)
custom_metric:
.. versionadded 1.6.0
Custom metric function. See :doc:`Custom Metric </tutorials/custom_metric_obj>`
for details. The metric receives transformed prediction (after applying the
reverse link function) when using a builtin objective, and raw output when using
a custom objective.
Returns
-------
Booster : a trained booster model
DATA
BoosterParam = typing.Union[typing.List, typing.Dict[str, typing.Any]]
Callable = typing.Callable
Deprecated alias to collections.abc.Callable.
Callable[[int], str] signifies a function that takes a single
parameter of type int and returns a str.
The subscription syntax must always be used with exactly two
values: the argument list and the return type.
The argument list must be a list of types, a ParamSpec,
Concatenate or ellipsis. The return type must be a single type.
There is no syntax to indicate optional or keyword arguments;
such function types are rarely used as callback types.
Dict = typing.Dict
A generic version of dict.
FPreProcCallable = typing.Callable
Deprecated alias to collections.abc.Callable.
Callable[[int], str] signifies a function that takes a single
parameter of type int and returns a str.
The subscription syntax must always be used with exactly two
values: the argument list and the return type.
The argument list must be a list of types, a ParamSpec,
Concatenate or ellipsis. The return type must be a single type.
There is no syntax to indicate optional or keyword arguments;
such function types are rarely used as callback types.
Iterable = typing.Iterable
A generic version of collections.abc.Iterable.
List = typing.List
A generic version of list.
Metric = typing.Callable[[numpy.ndarray, xgboost.core.DMatrix], typing...
Optional = typing.Optional
Optional[X] is equivalent to Union[X, None].
PlainObj = typing.Callable[[numpy.ndarray, xgboost.core.DMatrix], typi...
SKLEARN_INSTALLED = True
Sequence = typing.Sequence
A generic version of collections.abc.Sequence.
TYPE_CHECKING = False
Tuple = typing.Tuple
Deprecated alias to builtins.tuple.
Tuple[X, Y] is the cross-product type of X and Y.
Example: Tuple[T1, T2] is a tuple of two elements corresponding
to type variables T1 and T2. Tuple[int, float, str] is a tuple
of an int, a float and a string.
To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
Union = typing.Union
Union type; Union[X, Y] means either X or Y.
On Python 3.10 and higher, the | operator
can also be used to denote unions;
X | Y means the same thing to the type checker as Union[X, Y].
To define a union, use e.g. Union[int, str]. Details:
- The arguments must be types and there must be at least one.
- None as an argument is a special case and is replaced by
type(None).
- Unions of unions are flattened, e.g.::
assert Union[Union[int, str], float] == Union[int, str, float]
- Unions of a single argument vanish, e.g.::
assert Union[int] == int # The constructor actually returns int
- Redundant arguments are skipped, e.g.::
assert Union[int, str, int] == Union[int, str]
- When comparing unions, the argument order is ignored, e.g.::
assert Union[int, str] == Union[str, int]
- You cannot subclass or instantiate a union.
- You can use Optional[X] as a shorthand for Union[X, None].
FILE
c:\users\youse\appdata\local\programs\python\python312\lib\site-packages\xgboost\training.py