为什么在XGBRegressor中使用优化参数(MSE是最小化目标)会得到与优化RMSE不同的RMSE?

我正在处理一个回归问题。目前我正在调整XGBRegressor模型的参数,因此我使用GPyOpt库来获取优化参数。该函数返回一个包含5个元素的数组和最小化的MSE,数值为1813。然后我尝试将优化参数输入模型,但模型返回的MSE为2810。我想知道这是为什么?

我对GPyOpt库非常熟悉。关于我遇到的问题,信息很少,所以我想知道这是因为我的疏忽错误,还是有我没有理解的地方?

import GPyOptfrom GPyOpt.methods import BayesianOptimizationdef cv_score(parameters):    parameters = parameters[0]    score = cross_val_score(                XGBRegressor(learning_rate=parameters[0],                              gamma=int(parameters[1]),                              max_depth=int(parameters[2]),                              n_estimators=int(parameters[3]),                              min_child_weight = parameters[4]),                 x_train, y_train, scoring='neg_mean_squared_error').mean()    score = np.array(score)    return scorebds = [{'name': 'learning_rate', 'type': 'continuous', 'domain': (0, 1)},        {'name': 'gamma', 'type': 'continuous', 'domain': (0, 5)},        {'name': 'max_depth', 'type': 'discrete', 'domain': (1, 50)},        {'name': 'n_estimators', 'type': 'discrete', 'domain': (1, 300)},        {'name': 'min_child_weight', 'type': 'discrete', 'domain': (1, 10)}]optimizer = BayesianOptimization(f=cv_score, domain=bds,                                 model_type='GP',                                 acquisition_type ='EI',                                 acquisition_jitter = 0.05,                                 exact_feval=True,                                  maximize=True)optimizer.run_optimization(max_iter=20)
optimizer.x_opt

array([ 0.56133897, 2.697656 , 50. , 300. , 10. ])

xgb_final_param = {'learning_rate': 0.56133897, 'gamma': 2.697656, 'max_depth': 50, 'n_estimators': 300, 'min_child_weight': 10}xgb_final = SklearnExtra(clf = XGBRegressor(), seed = Seed, params = xgb_final_param)xgb_final.fit(x_train, y_train)evaluate(xgb_final, x_test, y_test) #evaluate returns MSE

我期望MSE大约在1813左右,但得到的是2810。所以我想知道为什么


回答:

在gpyopt中,离散变量不是通过它们的最大值/最小值来指定的,而是通过它们的完整值列表来指定的。为什么呢?因为你可能有不连续性,也就是说你的变量可能只取某些值,比如(1, 3, 8)。这里有一个例子可以参考。

所以在你的例子中,正确指定这些域的方法是生成所有可能值的列表:

{'name': 'max_depth', 'type': 'discrete', 'domain': list(range(1, 51))}

其他离散变量也是如此。请注意,对于连续变量,你的代码是正确的——它们是通过它们的范围来指定的。

Related Posts

在使用k近邻算法时,有没有办法获取被使用的“邻居”?

我想找到一种方法来确定在我的knn算法中实际使用了哪些…

Theano在Google Colab上无法启用GPU支持

我在尝试使用Theano库训练一个模型。由于我的电脑内…

准确性评分似乎有误

这里是代码: from sklearn.metrics…

Keras Functional API: “错误检查输入时:期望input_1具有4个维度,但得到形状为(X, Y)的数组”

我在尝试使用Keras的fit_generator来训…

如何使用sklearn.datasets.make_classification在指定范围内生成合成数据?

我想为分类问题创建合成数据。我使用了sklearn.d…

如何处理预测时不在训练集中的标签

已关闭。 此问题与编程或软件开发无关。目前不接受回答。…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注