(四)XGBoost——预测用1棵树木vs全部树

#预测1棵树木vs全部树木
import numpy as np
import xgboost as xgb

### load data in do training
dtrain = xgb.DMatrix('xxx/xgboost/demo/data/agaricus.txt.train')
dtest = xgb.DMatrix('xxx/xgboost/demo/data/agaricus.txt.test')
param = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'binary:logistic'}
watchlist = [(dtest, 'eval'), (dtrain, 'train')]
num_round = 3
bst = xgb.train(param, dtrain, num_round, watchlist)

print('start testing prediction from first n trees')
### predict using first 1 tree
label = dtest.get_label()
ypred1 = bst.predict(dtest, ntree_limit=1)
# by default, we predict using all the trees
ypred2 = bst.predict(dtest)
print('error of ypred1=%f' % (np.sum((ypred1 > 0.5) != label) / float(len(label))))
print('error of ypred2=%f' % (np.sum((ypred2 > 0.5) != label) / float(len(label))))
[20:20:57] 6513x127 matrix with 143286 entries loaded from /home/b8204/HawardData/xgboost/demo/data/agaricus.txt.train
[20:20:57] 1611x127 matrix with 35442 entries loaded from /home/b8204/HawardData/xgboost/demo/data/agaricus.txt.test
[0] eval-error:0.042831 train-error:0.046522
[1] eval-error:0.021726 train-error:0.022263
[2] eval-error:0.006207 train-error:0.007063
start testing prediction from first n trees
error of ypred1=0.042831
error of ypred2=0.006207

猜你喜欢

转载自blog.csdn.net/hao5335156/article/details/81174174