我目前正在尝试第一次使用XGBoost分析数据。我想使用GridsearchCV找到最佳参数。我想最小化均方根误差,为此,我使用"rmse“作为eval_metric。然而,网格搜索中的评分没有这样的指标。我在这个网站上发现"neg_mean_squared_error“做了同样的事情,但我发现这给我的结果与RMSE不同。当我计算"neg_mean_squared_error“的绝对值的根时,我得到的值约为8.9,而另一个函数给出的均方根约为4.4。我不知道哪里出了问题,或者我如何让这两个函数同意/给出相同的值?
由于这个问题,我得到了错误的值"best_params_“,它给了我一个比我最初开始调优的一些值更高的均方根。
谁能解释一下如何在网格搜索中获得RMSE的分数,或者为什么我的代码会给出不同的值?
提前谢谢。
def modelfit(alg, trainx, trainy, useTrainCV=True, cv_folds=10, early_stopping_rounds=50):
if useTrainCV:
xgb_param = alg.get_xgb_params()
xgtrain = xgb.DMatrix(trainx, label=trainy)
cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=alg.get_params()['n_estimators'], nfold=cv_folds,
metrics='rmse', early_stopping_rounds=early_stopping_rounds)
alg.set_params(n_estimators=cvresult.shape[0])
# Fit the algorithm on the data
alg.fit(trainx, trainy, eval_metric='rmse')
# Predict training set:
dtrain_predictions = alg.predict(trainx)
# dtrain_predprob = alg.predict_proba(trainy)[:, 1]
print(dtrain_predictions)
print(np.sqrt(mean_squared_error(trainy, dtrain_predictions)))
# Print model report:
print("\nModel Report")
print("RMSE : %.4g" % np.sqrt(metrics.mean_squared_error(trainy, dtrain_predictions)))
param_test2 = {
'max_depth':[6,7,8],
'min_child_weight':[2,3,4]
}
grid2 = GridSearchCV(estimator = xgb.XGBRegressor( learning_rate =0.1, n_estimators=2000, max_depth=5,
min_child_weight=2, gamma=0, subsample=0.8, colsample_bytree=0.8,
objective= 'reg:linear', nthread=4, scale_pos_weight=1, random_state=4),
param_grid = param_test2, scoring='neg_mean_squared_error', n_jobs=4,iid=False, cv=10, verbose=20)
grid2.fit(X_train,y_train)
# Mean cross-validated score of the best_estimator
print(grid2.best_params_, np.sqrt(np.abs(grid2.best_score_))), print(np.sqrt(np.abs(grid2.score(X_train, y_train))))
modelfit(grid2.best_estimator_, X_train, y_train)
print(np.sqrt(np.abs(grid2.score(X_train, y_train))))
发布于 2018-05-11 17:39:36
在GridSearchCV
中,对评分参数进行了转换,以便较高的值总是比较低的值更好。在您的示例中,neg_mean_squared_error
只是RMSE的否定版本。您不应该将neg_mean_squared_error
解释为RMSE,相反,在交叉验证中,您应该比较neg_mean_squared_error
的值,其中值越高越好。
在model_evaluation
文档的评分参数部分中提到了此行为。
发布于 2022-02-14 12:46:00
这是因为XGBoostRegressor.score返回the coefficient of determination of the prediction,而不是RMSE。
https://stackoverflow.com/questions/50296817
复制相似问题