Medial Code Documentation
Loading...
Searching...
No Matches
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
xgboost.sklearn.XGBModel Class Reference
Inheritance diagram for xgboost.sklearn.XGBModel:
xgboost.dask.DaskScikitLearnBase xgboost.sklearn.XGBClassifier xgboost.sklearn.XGBRanker xgboost.sklearn.XGBRegressor xgboost.dask.DaskXGBClassifier xgboost.dask.DaskXGBRanker xgboost.dask.DaskXGBRegressor xgboost.sklearn.XGBRFClassifier xgboost.sklearn.XGBRFRegressor xgboost.dask.DaskXGBRFClassifier xgboost.dask.DaskXGBRFRegressor

Public Member Functions

None __init__ (self, Optional[int] max_depth=None, Optional[int] max_leaves=None, Optional[int] max_bin=None, Optional[str] grow_policy=None, Optional[float] learning_rate=None, Optional[int] n_estimators=None, Optional[int] verbosity=None, SklObjective objective=None, Optional[str] booster=None, Optional[str] tree_method=None, Optional[int] n_jobs=None, Optional[float] gamma=None, Optional[float] min_child_weight=None, Optional[float] max_delta_step=None, Optional[float] subsample=None, Optional[str] sampling_method=None, Optional[float] colsample_bytree=None, Optional[float] colsample_bylevel=None, Optional[float] colsample_bynode=None, Optional[float] reg_alpha=None, Optional[float] reg_lambda=None, Optional[float] scale_pos_weight=None, Optional[float] base_score=None, Optional[Union[np.random.RandomState, int]] random_state=None, float missing=np.nan, Optional[int] num_parallel_tree=None, Optional[Union[Dict[str, int], str]] monotone_constraints=None, Optional[Union[str, Sequence[Sequence[str]]]] interaction_constraints=None, Optional[str] importance_type=None, Optional[str] device=None, Optional[bool] validate_parameters=None, bool enable_categorical=False, Optional[FeatureTypes] feature_types=None, Optional[int] max_cat_to_onehot=None, Optional[int] max_cat_threshold=None, Optional[str] multi_strategy=None, Optional[Union[str, List[str], Callable]] eval_metric=None, Optional[int] early_stopping_rounds=None, Optional[List[TrainingCallback]] callbacks=None, **Any kwargs)
 
bool __sklearn_is_fitted__ (self)
 
Booster get_booster (self)
 
"XGBModel" set_params (self, **Any params)
 
Dict[str, Any] get_params (self, bool deep=True)
 
Dict[str, Any] get_xgb_params (self)
 
int get_num_boosting_rounds (self)
 
None save_model (self, Union[str, os.PathLike] fname)
 
None load_model (self, ModelIn fname)
 
"XGBModel" fit (self, ArrayLike X, ArrayLike y, *Optional[ArrayLike] sample_weight=None, Optional[ArrayLike] base_margin=None, Optional[Sequence[Tuple[ArrayLike, ArrayLike]]] eval_set=None, Optional[Union[str, Sequence[str], Metric]] eval_metric=None, Optional[int] early_stopping_rounds=None, Optional[Union[bool, int]] verbose=True, Optional[Union[Booster, str, "XGBModel"]] xgb_model=None, Optional[Sequence[ArrayLike]] sample_weight_eval_set=None, Optional[Sequence[ArrayLike]] base_margin_eval_set=None, Optional[ArrayLike] feature_weights=None, Optional[Sequence[TrainingCallback]] callbacks=None)
 
ArrayLike predict (self, ArrayLike X, bool output_margin=False, bool validate_features=True, Optional[ArrayLike] base_margin=None, Optional[Tuple[int, int]] iteration_range=None)
 
np.ndarray apply (self, ArrayLike X, Optional[Tuple[int, int]] iteration_range=None)
 
Dict[str, Dict[str, List[float]]] evals_result (self)
 
int n_features_in_ (self)
 
np.ndarray feature_names_in_ (self)
 
float best_score (self)
 
int best_iteration (self)
 
np.ndarray feature_importances_ (self)
 
np.ndarray coef_ (self)
 
np.ndarray intercept_ (self)
 

Data Fields

 n_estimators
 
 objective
 
 max_depth
 
 max_leaves
 
 max_bin
 
 grow_policy
 
 learning_rate
 
 verbosity
 
 booster
 
 tree_method
 
 gamma
 
 min_child_weight
 
 max_delta_step
 
 subsample
 
 sampling_method
 
 colsample_bytree
 
 colsample_bylevel
 
 colsample_bynode
 
 reg_alpha
 
 reg_lambda
 
 scale_pos_weight
 
 base_score
 
 missing
 
 num_parallel_tree
 
 random_state
 
 n_jobs
 
 monotone_constraints
 
 interaction_constraints
 
 importance_type
 
 device
 
 validate_parameters
 
 enable_categorical
 
 feature_types
 
 max_cat_to_onehot
 
 max_cat_threshold
 
 multi_strategy
 
 eval_metric
 
 early_stopping_rounds
 
 callbacks
 
 kwargs
 
 n_classes_
 
 evals_result_
 

Protected Member Functions

Dict[str, bool] _more_tags (self)
 
str _get_type (self)
 
None _load_model_attributes (self, dict config)
 
Tuple[ Optional[Union[Booster, str, "XGBModel"]], Optional[Metric], Dict[str, Any], Optional[int], Optional[Sequence[TrainingCallback]],] _configure_fit (self, Optional[Union[Booster, "XGBModel", str]] booster, Optional[Union[Callable, str, Sequence[str]]] eval_metric, Dict[str, Any] params, Optional[int] early_stopping_rounds, Optional[Sequence[TrainingCallback]] callbacks)
 
DMatrix _create_dmatrix (self, Optional[DMatrix] ref, **Any kwargs)
 
None _set_evaluation_result (self, TrainingCallback.EvalsLog evals_result)
 
bool _can_use_inplace_predict (self)
 
Tuple[int, int] _get_iteration_range (self, Optional[Tuple[int, int]] iteration_range)
 

Protected Attributes

 _Booster
 

Member Function Documentation

◆ _configure_fit()

Tuple[ Optional[Union[Booster, str, "XGBModel"]], Optional[Metric], Dict[str, Any], Optional[int], Optional[Sequence[TrainingCallback]], ] xgboost.sklearn.XGBModel._configure_fit (   self,
Optional[Union[Booster, "XGBModel", str]]  booster,
Optional[Union[Callable, str, Sequence[str]]]  eval_metric,
Dict[str, Any]  params,
Optional[int]  early_stopping_rounds,
Optional[Sequence[TrainingCallback]]  callbacks 
)
protected
Configure parameters for :py:meth:`fit`.

◆ _load_model_attributes()

None xgboost.sklearn.XGBModel._load_model_attributes (   self,
dict  config 
)
protected
Load model attributes without hyper-parameters.

◆ _more_tags()

Dict[str, bool] xgboost.sklearn.XGBModel._more_tags (   self)
protected
Tags used for scikit-learn data validation.

◆ apply()

np.ndarray xgboost.sklearn.XGBModel.apply (   self,
ArrayLike  X,
Optional[Tuple[int, int]]   iteration_range = None 
)
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 : array_like, shape=[n_samples, n_features]
    Input features matrix.

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.

Reimplemented in xgboost.dask.DaskScikitLearnBase, and xgboost.sklearn.XGBRanker.

◆ best_iteration()

int xgboost.sklearn.XGBModel.best_iteration (   self)
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()

float xgboost.sklearn.XGBModel.best_score (   self)
The best score obtained by early stopping.

◆ coef_()

np.ndarray xgboost.sklearn.XGBModel.coef_ (   self)
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]``

◆ evals_result()

Dict[str, Dict[str, List[float]]] xgboost.sklearn.XGBModel.evals_result (   self)
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

◆ feature_importances_()

np.ndarray xgboost.sklearn.XGBModel.feature_importances_ (   self)
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_()

np.ndarray xgboost.sklearn.XGBModel.feature_names_in_ (   self)
Names of features seen during :py:meth:`fit`.  Defined only when `X` has
feature names that are all strings.

◆ fit()

"XGBModel" xgboost.sklearn.XGBModel.fit (   self,
ArrayLike  X,
ArrayLike  y,
*Optional[ArrayLike]   sample_weight = None,
Optional[ArrayLike]   base_margin = None,
Optional[Sequence[Tuple[ArrayLike, ArrayLike]]]   eval_set = None,
Optional[Union[str, Sequence[str], Metric]]   eval_metric = None,
Optional[int]   early_stopping_rounds = None,
Optional[Union[bool, int]]   verbose = True,
Optional[Union[Booster, str, "XGBModel"]]   xgb_model = None,
Optional[Sequence[ArrayLike]]   sample_weight_eval_set = None,
Optional[Sequence[ArrayLike]]   base_margin_eval_set = None,
Optional[ArrayLike]   feature_weights = None,
Optional[Sequence[TrainingCallback]]   callbacks = None 
)
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 :
    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.
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_metric : str, list of str, or callable, optional

    .. deprecated:: 1.6.0

    Use `eval_metric` in :py:meth:`__init__` or :py:meth:`set_params` instead.

early_stopping_rounds : int

    .. deprecated:: 1.6.0

    Use `early_stopping_rounds` in :py:meth:`__init__` or :py:meth:`set_params`
    instead.
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 :
    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.

callbacks :
    .. deprecated:: 1.6.0
        Use `callbacks` in :py:meth:`__init__` or :py:meth:`set_params` instead.

Reimplemented in xgboost.dask.DaskXGBRanker, xgboost.dask.DaskXGBRegressor, xgboost.dask.DaskXGBClassifier, xgboost.dask.DaskXGBRFRegressor, xgboost.dask.DaskXGBRFClassifier, xgboost.sklearn.XGBRanker, xgboost.sklearn.XGBClassifier, xgboost.sklearn.XGBRFClassifier, and xgboost.sklearn.XGBRFRegressor.

◆ get_booster()

Booster xgboost.sklearn.XGBModel.get_booster (   self)
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()

int xgboost.sklearn.XGBModel.get_num_boosting_rounds (   self)

◆ get_params()

Dict[str, Any] xgboost.sklearn.XGBModel.get_params (   self,
bool   deep = True 
)
Get parameters.

◆ get_xgb_params()

Dict[str, Any] xgboost.sklearn.XGBModel.get_xgb_params (   self)

◆ intercept_()

np.ndarray xgboost.sklearn.XGBModel.intercept_ (   self)
Intercept (bias) property

.. note:: Intercept is defined only for linear learners

    Intercept (bias) is 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
-------
intercept_ : array of shape ``(1,)`` or ``[n_classes]``

◆ n_features_in_()

int xgboost.sklearn.XGBModel.n_features_in_ (   self)
Number of features seen during :py:meth:`fit`.

◆ predict()

ArrayLike xgboost.sklearn.XGBModel.predict (   self,
ArrayLike  X,
bool   output_margin = False,
bool   validate_features = True,
Optional[ArrayLike]   base_margin = None,
Optional[Tuple[int, int]]   iteration_range = None 
)
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.
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 :
    Margin added to prediction.
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

Reimplemented in xgboost.dask.DaskScikitLearnBase, xgboost.sklearn.XGBClassifier, and xgboost.sklearn.XGBRanker.

◆ set_params()

"XGBModel" xgboost.sklearn.XGBModel.set_params (   self,
**Any  params 
)
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

The documentation for this class was generated from the following file: