【问题标题】:Scoring in GridSearchCV for XGBoost在 GridSearchCV 中为 XGBoost 评分
【发布时间】:2018-10-22 03:16:15
【问题描述】:

我目前第一次尝试使用 XGBoost 分析数据。我想使用 GridsearchCV 找到最佳参数。我想最小化均方根误差,为此,我使用“rmse”作为 eval_metric。但是,网格搜索中的评分没有这样的指标。我在这个网站上发现“neg_mean_squared_error”的作用相同,但我发现这给了我与 RMSE 不同的结果。当我计算“neg_mean_squared_error”的绝对值的根时,我得到一个大约 8.9 的值,而不同的函数给我一个大约 4.4 的 RMSE。 我不知道出了什么问题或者我如何让这两个函数同意/给出相同的值?

由于这个问题,我得到了错误的值“best_params_”,这给了我一个比我最初开始调整的一些值更高的 RMSE。

谁能解释一下如何在网格搜索中获得 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))))

【问题讨论】:

    标签: python machine-learning parameters xgboost grid-search


    【解决方案1】:

    GridSearchCV 中,评分参数进行了转换,因此较高的值始终优于较低的值。在您的示例中,neg_mean_squared_error 只是 RMSE 的否定版本。您不应将 neg_mean_squared_error 解释为 RMSE,而应在交叉验证中比较 neg_mean_squared_error 的值,其中较高的值优于较低的值。

    model_evaluation 文档的评分参数部分提到了这种行为。

    Scikit-Learn Scoring Parameter Documentation

    【讨论】:

    • 感谢您的回复!我明白了,但是 'print(np.sqrt(np.abs(grid2.score(X_train, y_train))))' 语句的值与 modelfit 'print("RMSE : %.4g" % np.sqrt(metrics.mean_squared_error(trainy, dtrain_predictions)))'?
    • 另外,当我实际上想要最小化 RMSE 时,最小化均方误差是错误的吗?我认为这归结为同一件事,因为如果你最小化某些东西,根也会被最小化。这是正确的吗?
    • grid2.best_score_ 是模型在交叉验证期间在保留数据上取得的最佳性能。然后,您将使用该估计器并将其拟合到整个训练集,并使用这些预测来计算 RMSE。所以差异归结为分数不是基于他们计算中的相同数据。
    • 谢谢,但这是否解释了“modelfit(grid2.best_estimator_, X_train, y_train)”(我适合 grid2.best_estimator_ 然后计算 RMSE)和“np. sqrt(np.abs(grid2.score(X_train, y_train)))" 在最后一行?
    • 你解决过这个问题吗?我们发现完全相同的东西,但看不到 best_score 与我们认为我们在手动计算时用于评分的指标有何一致。
    【解决方案2】:

    这是因为 XGBoostRegressor.score 返回 the coefficient of determination of the prediction,而不是 RMSE。

    【讨论】:

      猜你喜欢
      • 2018-11-05
      • 2019-03-03
      • 2021-07-11
      • 2018-05-14
      • 2017-05-31
      • 1970-01-01
      • 2016-03-05
      • 2018-04-19
      相关资源
      最近更新 更多