Style fixes in Python documentation. (#1764)
This commit is contained in:
parent
0ccb9b87d0
commit
b9a9d2bf45
@ -8,14 +8,15 @@ This document gives a basic walkthrough of xgboost python package.
|
|||||||
|
|
||||||
Install XGBoost
|
Install XGBoost
|
||||||
---------------
|
---------------
|
||||||
To install XGBoost, do the following steps:
|
To install XGBoost, do the following:
|
||||||
|
|
||||||
* You need to run `make` in the root directory of the project
|
* Run `make` in the root directory of the project
|
||||||
* In the `python-package` directory run
|
* In the `python-package` directory, run
|
||||||
```shell
|
```shell
|
||||||
python setup.py install
|
python setup.py install
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To verify your installation, try to `import xgboost` in Python.
|
||||||
```python
|
```python
|
||||||
import xgboost as xgb
|
import xgboost as xgb
|
||||||
```
|
```
|
||||||
@ -27,34 +28,34 @@ The XGBoost python module is able to load data from:
|
|||||||
- Numpy 2D array, and
|
- Numpy 2D array, and
|
||||||
- xgboost binary buffer file.
|
- xgboost binary buffer file.
|
||||||
|
|
||||||
The data will be store in a ```DMatrix``` object.
|
The data is stored in a ```DMatrix``` object.
|
||||||
|
|
||||||
* To load a libsvm text file or a XGBoost binary file into ```DMatrix```, the command is:
|
* To load a libsvm text file or a XGBoost binary file into ```DMatrix```:
|
||||||
```python
|
```python
|
||||||
dtrain = xgb.DMatrix('train.svm.txt')
|
dtrain = xgb.DMatrix('train.svm.txt')
|
||||||
dtest = xgb.DMatrix('test.svm.buffer')
|
dtest = xgb.DMatrix('test.svm.buffer')
|
||||||
```
|
```
|
||||||
* To load a numpy array into ```DMatrix```, the command is:
|
* To load a numpy array into ```DMatrix```:
|
||||||
```python
|
```python
|
||||||
data = np.random.rand(5,10) # 5 entities, each contains 10 features
|
data = np.random.rand(5,10) # 5 entities, each contains 10 features
|
||||||
label = np.random.randint(2, size=5) # binary target
|
label = np.random.randint(2, size=5) # binary target
|
||||||
dtrain = xgb.DMatrix( data, label=label)
|
dtrain = xgb.DMatrix( data, label=label)
|
||||||
```
|
```
|
||||||
* To load a scpiy.sparse array into ```DMatrix```, the command is:
|
* To load a scpiy.sparse array into ```DMatrix```:
|
||||||
```python
|
```python
|
||||||
csr = scipy.sparse.csr_matrix((dat, (row, col)))
|
csr = scipy.sparse.csr_matrix((dat, (row, col)))
|
||||||
dtrain = xgb.DMatrix(csr)
|
dtrain = xgb.DMatrix(csr)
|
||||||
```
|
```
|
||||||
* Saving ```DMatrix``` into XGBoost binary file will make loading faster in next time:
|
* Saving ```DMatrix``` into a XGBoost binary file will make loading faster:
|
||||||
```python
|
```python
|
||||||
dtrain = xgb.DMatrix('train.svm.txt')
|
dtrain = xgb.DMatrix('train.svm.txt')
|
||||||
dtrain.save_binary("train.buffer")
|
dtrain.save_binary("train.buffer")
|
||||||
```
|
```
|
||||||
* To handle missing value in ```DMatrix```, you can initialize the ```DMatrix``` by specifying missing values:
|
* Missing values can be replaced by a default value in the ```DMatrix``` constructor:
|
||||||
```python
|
```python
|
||||||
dtrain = xgb.DMatrix(data, label=label, missing = -999.0)
|
dtrain = xgb.DMatrix(data, label=label, missing = -999.0)
|
||||||
```
|
```
|
||||||
* Weight can be set when needed:
|
* Weights can be set when needed:
|
||||||
```python
|
```python
|
||||||
w = np.random.rand(5, 1)
|
w = np.random.rand(5, 1)
|
||||||
dtrain = xgb.DMatrix(data, label=label, missing = -999.0, weight=w)
|
dtrain = xgb.DMatrix(data, label=label, missing = -999.0, weight=w)
|
||||||
@ -62,7 +63,7 @@ dtrain = xgb.DMatrix(data, label=label, missing = -999.0, weight=w)
|
|||||||
|
|
||||||
Setting Parameters
|
Setting Parameters
|
||||||
------------------
|
------------------
|
||||||
XGBoost use list of pair to save [parameters](../parameter.md). Eg
|
XGBoost can use either a list of pairs or a dictionary to set [parameters](../parameter.md). For instance:
|
||||||
* Booster parameters
|
* Booster parameters
|
||||||
```python
|
```python
|
||||||
param = {'bst:max_depth':2, 'bst:eta':1, 'silent':1, 'objective':'binary:logistic' }
|
param = {'bst:max_depth':2, 'bst:eta':1, 'silent':1, 'objective':'binary:logistic' }
|
||||||
@ -73,7 +74,7 @@ param['eval_metric'] = 'auc'
|
|||||||
```python
|
```python
|
||||||
param['eval_metric'] = ['auc', 'ams@0']
|
param['eval_metric'] = ['auc', 'ams@0']
|
||||||
|
|
||||||
# alternativly:
|
# alternatively:
|
||||||
# plst = param.items()
|
# plst = param.items()
|
||||||
# plst += [('eval_metric', 'ams@0')]
|
# plst += [('eval_metric', 'ams@0')]
|
||||||
```
|
```
|
||||||
@ -86,27 +87,23 @@ evallist = [(dtest,'eval'), (dtrain,'train')]
|
|||||||
Training
|
Training
|
||||||
--------
|
--------
|
||||||
|
|
||||||
With parameter list and data, you are able to train a model.
|
Training a model requires a parameter list and data set.
|
||||||
* Training
|
|
||||||
```python
|
```python
|
||||||
num_round = 10
|
num_round = 10
|
||||||
bst = xgb.train( plst, dtrain, num_round, evallist )
|
bst = xgb.train( plst, dtrain, num_round, evallist )
|
||||||
```
|
```
|
||||||
* Saving model
|
After training, the model can be saved.
|
||||||
After training, you can save model and dump it out.
|
|
||||||
```python
|
```python
|
||||||
bst.save_model('0001.model')
|
bst.save_model('0001.model')
|
||||||
```
|
```
|
||||||
* Dump Model and Feature Map
|
The model and its feature map can also be dumped to a text file.
|
||||||
You can dump the model to txt and review the meaning of model
|
|
||||||
```python
|
```python
|
||||||
# dump model
|
# dump model
|
||||||
bst.dump_model('dump.raw.txt')
|
bst.dump_model('dump.raw.txt')
|
||||||
# dump model with feature map
|
# dump model with feature map
|
||||||
bst.dump_model('dump.raw.txt','featmap.txt')
|
bst.dump_model('dump.raw.txt','featmap.txt')
|
||||||
```
|
```
|
||||||
* Loading model
|
A saved model can be loaded as follows:
|
||||||
After you save your model, you can load model file at anytime by using
|
|
||||||
```python
|
```python
|
||||||
bst = xgb.Booster({'nthread':4}) #init model
|
bst = xgb.Booster({'nthread':4}) #init model
|
||||||
bst.load_model("model.bin") # load data
|
bst.load_model("model.bin") # load data
|
||||||
@ -127,7 +124,7 @@ This works with both metrics to minimize (RMSE, log loss, etc.) and to maximize
|
|||||||
|
|
||||||
Prediction
|
Prediction
|
||||||
----------
|
----------
|
||||||
After you training/loading a model and preparing the data, you can start to do prediction.
|
A model that has been trained or loaded can perform predictions on data sets.
|
||||||
```python
|
```python
|
||||||
# 7 entities, each contains 10 features
|
# 7 entities, each contains 10 features
|
||||||
data = np.random.rand(7, 10)
|
data = np.random.rand(7, 10)
|
||||||
@ -135,7 +132,7 @@ dtest = xgb.DMatrix(data)
|
|||||||
ypred = bst.predict(xgmat)
|
ypred = bst.predict(xgmat)
|
||||||
```
|
```
|
||||||
|
|
||||||
If early stopping is enabled during training, you can get predicticions from the best iteration with `bst.best_ntree_limit`:
|
If early stopping is enabled during training, you can get predictions from the best iteration with `bst.best_ntree_limit`:
|
||||||
```python
|
```python
|
||||||
ypred = bst.predict(xgmat,ntree_limit=bst.best_ntree_limit)
|
ypred = bst.predict(xgmat,ntree_limit=bst.best_ntree_limit)
|
||||||
```
|
```
|
||||||
@ -151,14 +148,13 @@ To plot importance, use ``plot_importance``. This function requires ``matplotlib
|
|||||||
xgb.plot_importance(bst)
|
xgb.plot_importance(bst)
|
||||||
```
|
```
|
||||||
|
|
||||||
To output tree via ``matplotlib``, use ``plot_tree`` specifying ordinal number of the target tree.
|
To plot the output tree via ``matplotlib``, use ``plot_tree``, specifying the ordinal number of the target tree. This function requires ``graphviz`` and ``matplotlib``.
|
||||||
This function requires ``graphviz`` and ``matplotlib``.
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
xgb.plot_tree(bst, num_trees=2)
|
xgb.plot_tree(bst, num_trees=2)
|
||||||
```
|
```
|
||||||
|
|
||||||
When you use ``IPython``, you can use ``to_graphviz`` function which converts the target tree to ``graphviz`` instance. ``graphviz`` instance is automatically rendered on ``IPython``.
|
When you use ``IPython``, you can use the ``to_graphviz`` function, which converts the target tree to a ``graphviz`` instance. The ``graphviz`` instance is automatically rendered in ``IPython``.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
xgb.to_graphviz(bst, num_trees=2)
|
xgb.to_graphviz(bst, num_trees=2)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user