通过 xgboost 找到最佳 n_estimators 时出错

Error in finding optimal n_estimators via xgboost

这里是初学者。 我正在尝试使用 xgboost 找到 n_estimators 的最佳数量。

但是,我得到 this error

diabetes.head() #this is a toy dataset in sklearn.datasets.

diabetes.head()

x=diabetes.drop('y',axis=1).values
y=diabetes.y.values

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=16,test_size=0.25)

import xgboost as xgb
xgbmodel=xgb.XGBRegressor(objective="reg:squarederror",eval_metric='rmse',early_stopping_rounds=10,n_estimators=1000,random_state=16)
xgbmodel.fit(x_train,y_train,eval_set=[x_test,y_test])

我认为问题在于:

eval_set=[x_test,y_test]

P.S。我仔细检查了来自 sklearn 的糖尿病数据集是否可以用于 regression.I 想知道我的错误是否在 eval_metric 方法中。

完整错误:

ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10180/532743731.py in <module>
      4 import xgboost as xgb
      5 xgbmodel=xgb.XGBRegressor(objective="reg:squarederror",eval_metric='rmse',early_stopping_rounds=10,n_estimators=1000,random_state=16)
----> 6 xgbmodel.fit(x_train,y_train,eval_set=[x_test,y_test])

C:\ProgramData\Anaconda3\lib\site-packages\xgboost\core.py in inner_f(*args, **kwargs)
    530         for k, arg in zip(sig.parameters, args):
    531             kwargs[k] = arg
--> 532         return f(**kwargs)
    533 
    534     return inner_f

C:\ProgramData\Anaconda3\lib\site-packages\xgboost\sklearn.py in fit(self, X, y, sample_weight, base_margin, eval_set, eval_metric, early_stopping_rounds, verbose, xgb_model, sample_weight_eval_set, base_margin_eval_set, feature_weights, callbacks)
    929         """
    930         evals_result: TrainingCallback.EvalsLog = {}
--> 931         train_dmatrix, evals = _wrap_evaluation_matrices(
    932             missing=self.missing,
    933             X=X,

C:\ProgramData\Anaconda3\lib\site-packages\xgboost\sklearn.py in _wrap_evaluation_matrices(missing, X, y, group, qid, sample_weight, base_margin, feature_weights, eval_set, sample_weight_eval_set, base_margin_eval_set, eval_group, eval_qid, create_dmatrix, enable_categorical)
    434 
    435         evals = []
--> 436         for i, (valid_X, valid_y) in enumerate(eval_set):
    437             # Skip the duplicated entry.
    438             if all(

ValueError: too many values to unpack (expected 2)

是的,它必须是一个元组,见下文,你的x_test,y_test应该用括号括起来

eval_setparam = [(self.X_valid, self.y_valid)]
        
xg_model.fit(self.X_train, self.y_train,  
                     eval_set = eval_setparam,
                     verbose=False)