Python 如何使用GridSearchCV仅进行简单的交叉验证
我是如何使用下面的代码执行简单交叉验证和K-fold交叉验证的Python 如何使用GridSearchCV仅进行简单的交叉验证,python,machine-learning,scikit-learn,grid-search,Python,Machine Learning,Scikit Learn,Grid Search,我是如何使用下面的代码执行简单交叉验证和K-fold交叉验证的 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] pa
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']
访问使用最佳超参数集训练的分类器,然后调用
score
方法,该方法将根据X\u-cv
进行预测,并与y\u-cv
进行评分准确性比较:
clf.best_estimator_.score(X_cv,y_cv)
如果您只是想要预测,那么调用
predict
方法,而将X_cv
作为参数。什么是“简单交叉验证”?什么是X_-cv
?简单的交叉验证意味着我们在X_-train上训练我们的模型,并在X_-cv上得到不同参数集的结果(而这只是交叉验证的数据矩阵)。但是如果我们在GridSearchCV()中通过cv=3然后它使用我不想要的3倍交叉验证?您已经使用3倍CV训练模型,以选择最佳的超参数集。调用best\u estimator\u
不会再次运行3倍CV。