【问题标题】:How to do only simple cross validation using GridSearchCV如何仅使用 GridSearchCV 进行简单的交叉验证
【发布时间】:2020-03-27 07:51:07
【问题描述】:

我如何使用下面的代码来执行简单的交叉验证和 K 折交叉验证

from sklearn.model_selection import GridSearchCV
import xgboost as xgb
import numpy as np

# our hyperparameters to choose from
learning_rate = [0.0001, 0.001, 0.01, 0.1, 0.2]
n_estimators = [30, 50, 100, 150, 200]

param_grid = dict(learning_rate = learning_rate, n_estimators = n_estimators)

xgb_model = xgb.XGBClassifier(random_state=42, n_jobs = -1)

clf = GridSearchCV(xgb_model, param_grid, scoring = 'roc_auc', cv=3, return_train_score=True)

sc = clf.fit(X_train, y_train)

# getting all the results
scores = clf.cv_results_
# getting train scores and cross validation scores
train_score = scores['mean_train_score']
cv_score = scores['mean_test_score']

【问题讨论】:

  • 什么是“简单交叉验证”? X_cv 是什么?
  • 简单的交叉验证意味着我们在 X_train 上训练我们的模型并在 X_cv 上获得结果(虽然是用于交叉验证的数据矩阵),用于不同的参数集。

标签: python machine-learning scikit-learn grid-search


【解决方案1】:

访问使用最佳超参数集训练的分类器,然后调用score 方法,该方法将从X_cv 进行预测,并与y_cv 相比得分准确度:

clf.best_estimator_.score(X_cv,y_cv)

如果您只需要预测,则调用 predict 方法,而不是使用 X_cv 作为参数。

【讨论】:

  • 但是如果我们在 GridSearchCV() 中传递 cv=3 那么它会使用我不想要的 3 折交叉验证?
  • 你已经用 3-fold CV 训练了模型来选择最佳的超参数集。调用 best_estimator_ 不会再次运行 3 倍 CV。
猜你喜欢
  • 2020-03-07
  • 2018-03-30
  • 2019-08-31
  • 2018-08-16
  • 2019-04-28
  • 2018-08-27
  • 2020-07-06
  • 2022-01-25
  • 2021-09-30
相关资源
最近更新 更多