File: python_intro.rst

package info (click to toggle)
xgboost 3.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 13,848 kB
  • sloc: cpp: 67,603; python: 35,537; java: 4,676; ansic: 1,426; sh: 1,352; xml: 1,226; makefile: 204; javascript: 19
file content (332 lines) | stat: -rw-r--r-- 15,003 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
###########################
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
:doc:`/tutorials/dask`.

**List of other Helpful Links**

* :doc:`/python/examples/index`
* :doc:`Python API Reference <python_api>`

**Contents**

.. contents::
  :backlinks: none
  :local:

Install XGBoost
---------------
To install XGBoost, follow instructions in :doc:`/install`.

To verify your installation, run the following in Python:

.. code-block:: python

  import xgboost as xgb

.. _python_data_interface:

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 :ref:`py-data`. For a detailed description of text input formats, please visit :doc:`/tutorials/input_format`.

The input data is stored in a :py:class:`DMatrix <xgboost.DMatrix>` object. For the sklearn estimator interface, a :py:class:`DMatrix` or a :py:class:`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.

* To load a NumPy array into :py:class:`DMatrix <xgboost.DMatrix>`:

  .. code-block:: python

    data = np.random.rand(5, 10)  # 5 entities, each contains 10 features
    label = np.random.randint(2, size=5)  # binary target
    dtrain = xgb.DMatrix(data, label=label)

* To load a :py:mod:`scipy.sparse` array into :py:class:`DMatrix <xgboost.DMatrix>`:

  .. code-block:: python

    csr = scipy.sparse.csr_matrix((dat, (row, col)))
    dtrain = xgb.DMatrix(csr)

* To load a Pandas data frame into :py:class:`DMatrix <xgboost.DMatrix>`:

  .. code-block:: python

    data = pandas.DataFrame(np.arange(12).reshape((4,3)), columns=['a', 'b', 'c'])
    label = pandas.DataFrame(np.random.randint(2, size=4))
    dtrain = xgb.DMatrix(data, label=label)

* Saving :py:class:`DMatrix <xgboost.DMatrix>` into a XGBoost binary file will make loading faster:

  .. code-block:: python

    dtrain = xgb.DMatrix('train.svm.txt?format=libsvm')
    dtrain.save_binary('train.buffer')

* Missing values can be replaced by a default value in the :py:class:`DMatrix <xgboost.DMatrix>` constructor:

  .. code-block:: python

    dtrain = xgb.DMatrix(data, label=label, missing=np.NaN)

* Weights can be set when needed:

  .. code-block:: python

    w = np.random.rand(5, 1)
    dtrain = xgb.DMatrix(data, label=label, missing=np.NaN, weight=w)

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

* To load a LIBSVM text file or a XGBoost binary file into :py:class:`DMatrix <xgboost.DMatrix>`:

  .. code-block:: python

    dtrain = xgb.DMatrix('train.svm.txt?format=libsvm')
    dtest = xgb.DMatrix('test.svm.buffer')

  The parser in XGBoost has limited functionality. When using Python interface, it's
  recommended to use sklearn ``load_svmlight_file`` or other similar utilites than
  XGBoost's builtin parser.

* To load a CSV file into :py:class:`DMatrix <xgboost.DMatrix>`:

  .. code-block:: python

    # label_column specifies the index of the column containing the true label
    dtrain = xgb.DMatrix('train.csv?format=csv&label_column=0')
    dtest = xgb.DMatrix('test.csv?format=csv&label_column=0')

  The parser in XGBoost has limited functionality. When using Python interface, it's
  recommended to use pandas ``read_csv`` or other similar utilites than XGBoost's builtin
  parser.

.. _py-data:

Supported data structures for various XGBoost functions
=======================================================

*******
Markers
*******

- T: Supported.
- F: Not supported.
- NE: Invalid type for the use case. For instance, `pd.Series` can not be multi-target label.
- NPA: Support with the help of numpy array.
- AT: Support with the help of arrow table.
- CPA: Support with the help of cupy array.
- SciCSR: Support with the help of scripy sparse CSR. The conversion to scipy CSR may or may not be possible. Raise a type error if conversion fails.
- FF: We can look forward to having its support in recent future if requested.
- empty: To be filled in.

************
Table Header
************
- `X` means predictor matrix.
- Meta info: label, weight, etc.
- Multi Label: 2-dim label for multi-target.
- Others: Anything else that we don't list here explicitly including formats like `lil`, `dia`, `bsr`. XGBoost will try to convert it into scipy csr.

**************
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 :doc:`parameters </parameter>`. For instance:

* Booster parameters

  .. code-block:: python

    param = {'max_depth': 2, 'eta': 1, 'objective': 'binary:logistic'}
    param['nthread'] = 4
    param['eval_metric'] = 'auc'

* You can also specify multiple eval metrics:

  .. code-block:: python

    param['eval_metric'] = ['auc', 'ams@0']

    # alternatively:
    # plst = param.items()
    # plst += [('eval_metric', 'ams@0')]

* Specify validations set to watch performance

  .. code-block:: python

    evallist = [(dtrain, 'train'), (dtest, 'eval')]

Training
--------

Training a model requires a parameter list and data set.

.. code-block:: python

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

After training, the model can be saved.

.. code-block:: python

  bst.save_model('0001.model')

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

.. code-block:: python

  # 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:

.. code-block:: python

  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.

.. code-block:: python

  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 :py:meth:`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.

.. code-block:: python

  # 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``:

.. code-block:: python

  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 :py:meth:`xgboost.plot_importance`. This function requires ``matplotlib`` to be installed.

.. code-block:: python

  xgb.plot_importance(bst)

To plot the output tree via ``matplotlib``, use :py:meth:`xgboost.plot_tree`, specifying the ordinal number of the target tree. This function requires ``graphviz`` and ``matplotlib``.

.. code-block:: python

  xgb.plot_tree(bst, num_trees=2)

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

.. code-block:: python

  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 :doc:`/python/sklearn_estimator`
for more info.

.. code-block:: python

  # 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:

.. code-block:: python

   booster: xgb.Booster = reg.get_booster()