当我试图适应我的XGBRegressor模型时,我在TypeError: Expected sequence or array-like, got <class 'xgboost.core.DMatrix'>
参数上获得了x
,即使它是numpy.ndarray
类型
from xgboost import XGBRegressor
from sklearn.metrics import r2_score
model = XGBRegressor(max_depth=5, learning_rate=0.001, n_estimators=5000)
eval_set = [(X_test, y_test)]
model.fit(X_train, y_train, eval_set=eval_set, eval_metric=r2_score, early_stopping_rounds=20, verbose=True)
错误信息:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-50-4bb3ebe8ef00> in <module>
----> 1 model.fit(X_train, y_train, eval_set=eval_set, eval_metric=r2_score, early_stopping_rounds=20, verbose=True)
~/anaconda3/envs/ds/lib/python3.6/site-packages/xgboost/sklearn.py in fit(self, X, y, sample_weight, eval_set, eval_metric, early_stopping_rounds, verbose, xgb_model, sample_weight_eval_set, callbacks)
376 evals_result=evals_result, obj=obj, feval=feval,
377 verbose_eval=verbose, xgb_model=xgb_model,
--> 378 callbacks=callbacks)
379
380 if evals_result:
~/anaconda3/envs/ds/lib/python3.6/site-packages/xgboost/training.py in train(params, dtrain, num_boost_round, evals, obj, feval, maximize, early_stopping_rounds, evals_result, verbose_eval, xgb_model, callbacks, learning_rates)
214 evals=evals,
215 obj=obj, feval=feval,
--> 216 xgb_model=xgb_model, callbacks=callbacks)
217
218
~/anaconda3/envs/ds/lib/python3.6/site-packages/xgboost/training.py in
_train_internal(params, dtrain, num_boost_round, evals, obj, feval, xgb_model, callbacks)
82 # check evaluation result.
83 if len(evals) != 0:
---> 84 bst_eval_set = bst.eval_set(evals, i, feval)
85 if isinstance(bst_eval_set, STRING_TYPES):
86 msg = bst_eval_set
~/anaconda3/envs/ds/lib/python3.6/site-packages/xgboost/core.py in eval_set(self, evals, iteration, feval) 1175 if feval is not None: 1176 for dmat, evname in evals:
-> 1177 feval_ret = feval(self.predict(dmat), dmat) 1178 if isinstance(feval_ret, list): 1179 for name, val in feval_ret:
~/anaconda3/envs/ds/lib/python3.6/site-packages/sklearn/metrics/regression.py in r2_score(y_true, y_pred, sample_weight, multioutput)
532 """
533 y_type, y_true, y_pred, multioutput = _check_reg_targets(
--> 534 y_true, y_pred, multioutput)
535 check_consistent_length(y_true, y_pred, sample_weight)
536
~/anaconda3/envs/ds/lib/python3.6/site-packages/sklearn/metrics/regression.py in _check_reg_targets(y_true, y_pred, multioutput)
73
74 """
---> 75 check_consistent_length(y_true, y_pred)
76 y_true = check_array(y_true, ensure_2d=False)
77 y_pred = check_array(y_pred, ensure_2d=False)
~/anaconda3/envs/ds/lib/python3.6/site-packages/sklearn/utils/validation.py in check_consistent_length(*arrays)
229 """
230
--> 231 lengths = [_num_samples(X) for X in arrays if X is not None]
232 uniques = np.unique(lengths)
233 if len(uniques) > 1:
~/anaconda3/envs/ds/lib/python3.6/site-packages/sklearn/utils/validation.py in <listcomp>(.0)
229 """
230
--> 231 lengths = [_num_samples(X) for X in arrays if X is not None]
232 uniques = np.unique(lengths)
233 if len(uniques) > 1:
~/anaconda3/envs/ds/lib/python3.6/site-packages/sklearn/utils/validation.py in _num_samples(x)
136 else:
137 raise TypeError("Expected sequence or array-like, got %s" %
--> 138 type(x))
139 if hasattr(x, 'shape'):
140 if len(x.shape) == 0:
TypeError: Expected sequence or array-like, got <class 'xgboost.core.DMatrix'>
我得到了导致此错误的原因。这是因为我设置了错误的eval_metric=r2_score