1

I tried to use the eval_metric argument in XgBoost but got this error:

TypeError: fit() got an unexpected keyword argument 'eval_metric'

Here is my code:

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)

Does anyone know the solution to this problem?

user2505650
  • 1,293
  • 6
  • 20
  • 38

1 Answers1

2

I work a bit differently with xgb. The following code will give you some control over your hyper parameters and I would be able to assist you if things doesn't work

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)
Eran Moshe
  • 3,062
  • 2
  • 22
  • 41
  • 1
    Using scikit-learn API might be necessary for compatibility reasons, and tis does not address the issue of unexpected keyword argument 'eval_metric' in fit method of the XGBClassifier object. – CharlesG Jan 30 '20 at 12:36