xgboost使用細節

from http://blog.csdn.net/zc02051126/article/details/46771793python

 

在Python中使用XGBoost

下面將介紹XGBoost的Python模塊,內容以下: 
編譯及導入Python模塊 
數據接口 
參數設置 
訓練模型l 
提早終止程序 
預測git

walk through python example for UCI Mushroom dataset is provided.github

安裝

首先安裝XGBoost的C++版本,而後進入源文件的根目錄下的 wrappers文件夾執行以下腳本安裝Python模塊算法

python setup.py install

安裝完成後按照以下方式導入XGBoost的Python模塊shell

import xgboost as xgb

數據接口

XGBoost能夠加載libsvm格式的文本數據,加載的數據格式能夠爲Numpy的二維數組和XGBoost的二進制的緩存文件。加載的數據存儲在對象DMatrix中。數組

  • 加載libsvm格式的數據和二進制的緩存文件時能夠使用以下方式
dtrain = xgb.DMatrix('train.svm.txt') dtest = xgb.DMatrix('test.svm.buffer')
  • 加載numpy的數組到DMatrix對象時,能夠用以下方式
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)
  • scipy.sparse格式的數據轉化爲 DMatrix格式時,能夠使用以下方式
csr = scipy.sparse.csr_matrix( (dat, (row,col)) )
dtrain = xgb.DMatrix( csr )
  • 將 DMatrix 格式的數據保存成XGBoost的二進制格式,在下次加載時能夠提升加載速度,使用方式以下
dtrain = xgb.DMatrix('train.svm.txt') dtrain.save_binary("train.buffer")
  • 能夠用以下方式處理 DMatrix中的缺失值:
dtrain = xgb.DMatrix( data, label=label, missing = -999.0)
  • 當須要給樣本設置權重時,能夠用以下方式
w = np.random.rand(5,1) dtrain = xgb.DMatrix( data, label=label, missing = -999.0, weight=w)

 

參數設置

XGBoost使用key-value格式保存參數. Eg 
* Booster(基本學習器)參數緩存

param = {'bst:max_depth':2, 'bst:eta':1, 'silent':1, 'objective':'binary:logistic' } param['nthread'] = 4 plst = param.items() plst += [('eval_metric', 'auc')] # Multiple evals can be handled in this way plst += [('eval_metric', 'ams@0')]

 

  • 還能夠定義驗證數據集,驗證算法的性能
evallist  = [(dtest,'eval'), (dtrain,'train')]

訓練模型

有了參數列表和數據就能夠訓練模型了 
* 訓練app

num_round = 10 bst = xgb.train( plst, dtrain, num_round, evallist )
  • 保存模型
    在訓練完成以後能夠將模型保存下來,也能夠查看模型內部的結構
bst.save_model('0001.model')
  • Dump Model and Feature Map 
    You can dump the model to txt and review the meaning of model
# dump model bst.dump_model('dump.raw.txt') # dump model with feature map bst.dump_model('dump.raw.txt','featmap.txt')
  • 加載模型 
    經過以下方式能夠加載模型
bst = xgb.Booster({'nthread':4}) #init model bst.load_model("model.bin") # load data

提早終止程序

若是有評價數據,能夠提早終止程序,這樣能夠找到最優的迭代次數。若是要提早終止程序必須至少有一個評價數據在參數evals中。 If there’s more than one, it will use the last.dom

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

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 and bst.best_iteration. Note that 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).

=

Prediction

After you training/loading a model and preparing the data, you can start to do prediction.

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

If early stopping is enabled during training, you can predict with the best iteration.

ypred = bst.predict(xgmat,ntree_limit=bst.best_iteration)
相關文章
相關標籤/搜索