A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://xgboost.readthedocs.io/en/stable/python/python_intro.html below:

Website Navigation


Python Package Introduction — xgboost 3.0.4 documentation

Python Package Introduction

This document gives a basic walkthrough of the xgboost package for Python. The Python package is consisted of 3 different interfaces, including native interface, scikit-learn interface and dask interface. For introduction to dask interface please see Distributed XGBoost with Dask.

List of other Helpful Links

Contents

Install XGBoost

To install XGBoost, follow instructions in Installation Guide.

To verify your installation, run the following in Python:

Data Interface

The XGBoost Python module is able to load data from many different types of data format including both CPU and GPU data structures. For a complete list of supported data types, please reference the Supported data structures for various XGBoost functions. For a detailed description of text input formats, please visit Text Input Format of DMatrix.

The input data is stored in a DMatrix object. For the sklearn estimator interface, a DMatrix or a QuantileDMatrix is created depending on the chosen algorithm and the input, see the sklearn API reference for details. We will illustrate some of the basic input types with the DMatrix here.

When performing ranking tasks, the number of weights should be equal to number of groups.

Supported data structures for various XGBoost functions Markers Support Matrix

Name

DMatrix X

QuantileDMatrix X

Sklearn X

Meta Info

Inplace prediction

Multi Label

numpy.ndarray

T

T

T

T

T

T

scipy.sparse.csr

T

T

T

NE

T

F

scipy.sparse.csc

T

F

T

NE

F

F

scipy.sparse.coo

SciCSR

F

SciCSR

NE

F

F

uri

T

F

F

F

NE

F

list

NPA

NPA

NPA

NPA

NPA

T

tuple

NPA

NPA

NPA

NPA

NPA

T

pandas.DataFrame

NPA

NPA

NPA

NPA

NPA

NPA

pandas.Series

NPA

NPA

NPA

NPA

NPA

NE

cudf.DataFrame

T

T

T

T

T

T

cudf.Series

T

T

T

T

FF

NE

cupy.ndarray

T

T

T

T

T

T

torch.Tensor

T

T

T

T

T

T

dlpack

CPA

CPA

CPA

FF

FF

modin.DataFrame

NPA

FF

NPA

NPA

FF

modin.Series

NPA

FF

NPA

NPA

FF

pyarrow.Table

T

T

T

T

T

T

polars.DataFrame

AT

AT

AT

AT

AT

AT

polars.LazyFrame (WARN)

AT

AT

AT

AT

AT

AT

polars.Series

AT

AT

AT

AT

AT

NE

__array__

NPA

F

NPA

NPA

H

Others

SciCSR

F

F

F

The polars LazyFrame.collect supports many configurations, ranging from the choice of query engine to type coercion. XGBoost simply uses the default parameter. Please run collect to obtain the DataFrame before passing it into XGBoost for finer control over the behaviour.

Setting Parameters

XGBoost can use either a list of pairs or a dictionary to set parameters. For instance:

Training

Training a model requires a parameter list and data set.

num_round = 10
bst = xgb.train(param, dtrain, num_round, evallist)

After training, the model can be saved.

bst.save_model('0001.model')

The model and its feature map can also be dumped to a text file.

# dump model
bst.dump_model('dump.raw.txt')
# dump model with feature map
bst.dump_model('dump.raw.txt', 'featmap.txt')

A saved model can be loaded as follows:

bst = xgb.Booster({'nthread': 4})  # init model
bst.load_model('model.bin')  # load model data

Methods including update and boost from xgboost.Booster are designed for internal usage only. The wrapper function xgboost.train does some pre-configuration including setting up caches and some other parameters.

Early Stopping

If you have a validation set, you can use early stopping to find the optimal number of boosting rounds. Early stopping requires at least one set in evals. If there’s more than one, it will use the last.

train(..., evals=evals, early_stopping_rounds=10)

The model will train until the validation score stops improving. Validation error needs to decrease at least every early_stopping_rounds to continue training.

If early stopping occurs, the model will have two additional fields: bst.best_score, bst.best_iteration. Note that xgboost.train() will return a model from the last iteration, not the best one.

This works with both metrics to minimize (RMSE, log loss, etc.) and to maximize (MAP, NDCG, AUC). Note that if you specify more than one evaluation metric the last one in param['eval_metric'] is used for early stopping.

Prediction

A model that has been trained or loaded can perform predictions on data sets.

# 7 entities, each contains 10 features
data = np.random.rand(7, 10)
dtest = xgb.DMatrix(data)
ypred = bst.predict(dtest)

If early stopping is enabled during training, you can get predictions from the best iteration with bst.best_iteration:

ypred = bst.predict(dtest, iteration_range=(0, bst.best_iteration + 1))
Plotting

You can use plotting module to plot importance and output tree.

To plot importance, use xgboost.plot_importance(). This function requires matplotlib to be installed.

To plot the output tree via matplotlib, use xgboost.plot_tree(), specifying the ordinal number of the target tree. This function requires graphviz and matplotlib.

xgb.plot_tree(bst, num_trees=2)

When you use IPython, you can use the xgboost.to_graphviz() function, which converts the target tree to a graphviz instance. The graphviz instance is automatically rendered in IPython.

xgb.to_graphviz(bst, num_trees=2)
Scikit-Learn interface

XGBoost provides an easy to use scikit-learn interface for some pre-defined models including regression, classification and ranking. See Using the Scikit-Learn Estimator Interface for more info.

# Use "hist" for training the model.
reg = xgb.XGBRegressor(tree_method="hist", device="cuda")
# Fit the model using predictor X and response y.
reg.fit(X, y)
# Save model into JSON format.
reg.save_model("regressor.json")

User can still access the underlying booster model when needed:

booster: xgb.Booster = reg.get_booster()

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4