我正在Python语言中使用XGBoost (PyPl上的最新版本: 0.6)开发一个预测模型,并且已经对大约一半的数据进行了训练。现在我有了我的最终模型,我用我所有的数据对它进行了训练,但得到了这条消息,这是我以前从未见过的:
树方法被自动选择为“近似”以获得更快的速度。要使用旧的行为(单机上的精确贪婪算法),请将tree_method设置为‘exact’。“
作为一个可重现的示例,下面的代码也会在我的机器上生成该消息:
import numpy as np
import xgboost as xgb
rows = 10**7
cols = 20
X = np.random.randint(0, 100, (rows, cols))
y = np.random.randint(0,2, size=rows)
clf = xgb.XGBClassifier(max_depth=5)
clf.fit(X,y)
我尝试在我的模型的初始化和fit()
步骤中将tree_method设置为'exact‘,但是每个步骤都抛出错误:
import xgboost as xgb
clf = xgb.XGBClassifier(tree_method = 'exact')
clf
> __init__() got an unexpected keyword argument 'tree_method'
my_pipeline.fit(X_train, Y_train, clf__tree_method='exact')
> self._final_estimator.fit(Xt, y, **fit_params) TypeError: fit() got an
> unexpected keyword argument 'tree_method'
如何在Python语言中使用XGBoost指定tree_method='exact‘?
发布于 2017-07-26 04:33:51
根据XGBoost parameter documentation的说法,这是因为tree_method
的默认值是"auto“。“自动”设置依赖于数据:对于“中小型”数据,它将使用“精确”方法,而对于“非常大”的数据集,它将使用“近似”。当您开始使用整个训练集(而不是50%)时,您必须已经超过了更改tree_method
的自动值的训练大小阈值。从文档中还不清楚需要多少个观察值才能达到这个阈值,但它似乎在500万到1000万行之间(因为您有rows = 10**7
)。
我不知道XGBoost Python模块中是否公开了tree_method
参数(听起来好像没有,所以可以提交一个错误报告?),但是tree_method
在R中公开了。
文档描述了为什么您会看到警告消息:
发布于 2018-07-16 03:59:16
它仍然没有在xgboost的scikit-learn API中实现。因此,我引用了here中的以下代码示例。
import xgboost as xgb
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
digits = load_digits(2)
X = digits['data']
y = digits['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
dtrain = xgb.DMatrix(X_train, y_train)
dtest = xgb.DMatrix(X_test, y_test)
param = {'objective': 'binary:logistic',
'tree_method':'hist',
'grow_policy':"lossguide",
'eval_metric': 'auc'}
res = {}
bst = xgb.train(param, dtrain, 10, [(dtrain, 'train'), (dtest, 'test')], evals_result=res)
发布于 2018-12-20 15:56:33
您可以在xGBoost中通过sklearn API使用GPU。你可以这样使用它:
import xgboost
xgb = xgboost.XGBClassifier(n_estimators=200, tree_method='gpu_hist', predictor='gpu_predictor')
xgb.fit(X_train, y_train)
您可以使用不同的树方法。请参阅documentation以选择最适合您需要的方法。
https://stackoverflow.com/questions/44058786
复制相似问题