我尝试在XgBoost中使用eval_metric参数,但遇到了这个错误:
TypeError:fit()得到了一个意外的关键字参数'eval_metric'
这是我的代码:
eval_set = [(X_test_np, y_test_np)]
model = XGBClassifier()
model.fit(X_train_np, y_train_np,eval_metric="auc", eval_set=eval_set)
有谁知道这个问题的解决方案?
答案 0 :(得分:2)
我对xgb的工作方式略有不同。以下代码将为您提供对超参数的一些控制,如果事情不起作用,我将能够为您提供帮助
import xgboost as xgb
dtrain = xgb.DMatrix(X_train_np, label=y_train_np)
dtest = xgb.DMatrix(X_test_np, label=y_test_np)
# Here we set eval_metric to be 'auc' as well as other hypter parameters of xgboost
param0 = [
('max_depth', 4),
('eta', 0.1),
('objective', 'binary:logistic'),
('min_child_weight', 4),
('silent', 1),
('eval_metric', 'auc'),
('subsample', 0.75),
('colsample_bytree', 0.75),
('gamma', 1),
]
watchlist = [(dtrain, "trn"), (dtest, "tst")]
n_estimators = 100
# This is the same as fitting
model = xgb.train(param0, dtrain, n_estimators , evals=watchlist)