我想在kfold交叉验证中对训练数据应用超参数调整,以便估计将要选择的最佳参数,因此,我稍后将记录最佳超参数并将其用于我的模型训练和测试。
我想知道如何将超参数调整放入 kfold交叉验证循环中。但是,这就是我通常使用kfold的方式:
kfold = KFold(n_splits=4, random_state=42)
model = LinearRegression()
results = cross_val_score(model, X, Y, cv=kfold, scoring='r2')
print("R2: %.3f%% (%.3f%%)" % (results.mean() * 100.0, results.std() * 100.0))
但是由于我要访问kfold循环,因此我可以执行以下操作吗??
kf = KFold(n_splits=4, random_state=42)
kf.get_n_splits(X)
predicted_y = []
expected_y = []
for train_index, test_index in kf.split(X):
# print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
regressor = LinearRegression()
regressor.fit(X_train, y_train)
predicted_y.extend(regressor.predict(X_test))
expected_y.extend(y_test)
accuracy = r2_score(expected_y, predicted_y)
print("R2: ", accuracy)
,如果可以,我应该如何插入GridSearch CV?