【发布时间】:2021-05-12 09:12:10
【问题描述】:
我正在使用Learning API 版本的 xgboost。我想使用它来获取线性模型的系数,但它会导致错误AttributeError: 'Booster' object has no attribute 'coef_'。 Learning API 文档似乎没有说明如何检索系数。
###xtrain and ytrain are numpy arrays
dtrain = xgb.DMatrix(xtrain, label=ytrain)
dtest = xgb.DMatrix(xtest, label=ytest)
param = {'eta':0.3125, 'objective': 'binary:logistic' 'nthread':8, 'eval_metric':'auc', 'booster':'gblinear', 'max_depth':12}
model = xgb.train(param, dtrain, 60, [(dtrain, 'train'), (dtest, 'eval')], verbose_eval = 5, early_stopping_rounds = 12)
print(model.coef_) #results in an error
我尝试使用XGBRegressor 构建上述模型的等效版本,因为它确实具有属性coef_,但该模型确实返回了非常不同的预测。我查看了有关此主题的先前答案(1,2),这似乎暗示n_estimators 实际上与num_boost_round 相同,并且将提供相同的预测。但尽管考虑到这一点,基于以下参数的预测却大不相同。这个模型被证明是非常保守的。此外,从文档中,nthread 与 n_jobs 相同。我看不出两者的参数有任何其他差异。
model = XGBRegressor(n_estimators = 60, learning_rate = 0.3125, max_depth = 12, objective = 'binary:logistic', booster = 'gblinear', n_jobs = 8)
model = model.fit(xtrain, ytrain, eval_metric = 'auc', early_stopping_rounds = 12, eval_set = [(xtest, ytest)])
predictions = model.predict(xtrain, ntree_limit = 0) # need to include ntree_limit because of bug associated with early_stopping_rounds for gblinear
我的问题是:
- 有没有办法为使用
xgb.train为线性模型构建的模型获取系数,如果可以,我该怎么做? - 如果不是,为什么
XGBRegressor会给我不同的结果?
【问题讨论】:
标签: python machine-learning scikit-learn xgboost