如何使用 GridSearchCV 为每次迭代预测 X_test?

How do I use GridSearchCV to predict X_test for each iteration?

image of graph i want to plot

我正在考虑绘制一个图表,其中 x 轴是图表的复杂性(例如,KNN 中的 n_neighbors),y_axis 是误差(如均方误差).

我目前正在使用 Grid Search CV,我意识到对于 .cv_results_ ,它们只显示列车数据错误。

KNN={
    'classifier':  [KNeighborsClassifier()],
    'classifier__n_neighbors':  [i for i in range (10, 200, 10)],
}

pipeline = Pipeline(
    steps = [('classifier', KNN["classifier"][0])]

    )

grid_serach_knn = GridSearchCV(pipeline, [KNN], n_jobs=-1).fit(x_train, y_train)

grid_serach_knn.cv_results_ 会给我

'split0_test_score': array([0.97, 0.97, 0.97, 0.97, 0.97, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96,0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96]), 
'split1_test_score': array([0.97, 0.97, 0.97, 0.97, 0.97, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96]), 
'split2_test_score': array([0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.96, 0.96, 0.96, 0.96, 0.96,0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96]), 
'split3_test_score': array([0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96]), 
'split4_test_score': array([0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96]), 
'mean_test_score': array([0.97, 0.97, 0.97, 0.97, 0.97, 0.97, 0.96, 0.96, 0.96, 0.96, 0.96,0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96]), 
'std_test_score': array([9.84e-04, 8.70e-04, 1.30e-03, 1.09e-03, 7.68e-04, 9.61e-04,1.11e-16, 1.11e-16, 1.11e-16, 1.11e-16, 1.11e-16, 1.11e-16,1.11e-16, 1.11e-16, 1.11e-16, 1.11e-16, 1.11e-16, 1.11e-16,1.11e-16]), 
'rank_test_score': array([3, 2, 1, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7])}

首先,我不了解各种考试成绩。他们是训练成绩吗?如果是,他们使用什么指标? accuracy/r^2/precision/recall?

其次,我如何在每次迭代中使用 model.predict(X_test) 来查找测试数据集的错误,以便我可以在顶部绘制图表?

firstly, i don't understand the different kinds of test scores. are they the training scores? if they are, what metrics are they using? accuracy/r^2/precision/recall?

不,这些是验证分数。每个值都是一个包含 19 个数字的数组,对应于 n_neighbors 的 19 个不同值。您没有指定 cv 参数,因此 GridSearchCV 默认将您的训练集分成 5 个部分并运行 5 次,每次使用其中一个部分作为验证集,另外 4 个部分来训练模型。这就是“split0”到“split4”名称所指的内容。这些值都是准确度分数。

例如,“'split0_test_score': array([0.97, ...”告诉你在用n_neighbors=10在80%的训练数据上训练模型后,根据第一次分割,模型正确分类了剩余训练数据中 97% 的实例。

结果中还包含 5 次拆分的平均分以及相应的标准差和排名。

secondly, how would i use model.predict(X_test) for each iteration to find the error for the test dataset so that i can plot the graph at the top?

注意 GridSearchCV 有一个参数 return_train_score。引用自 scikit-learn docs:

return_train_score : bool, default=False
If False, the cv_results_ attribute will not include training scores. Computing training scores is used to get insights on how different parameter settings impact the overfitting/underfitting trade-off. However computing the scores on the training set can be computationally expensive and is not strictly required to select the parameters that yield the best generalization performance.

因此您可以将其设置为 True 以获取训练分数并将其绘制为一条曲线,以及验证曲线。 Scikit-learn 甚至还有 validation_curve function to help with this, and an example 如何使用它。

但是,请注意您显示的图根本没有提到(交叉)验证,并且您说您有一个单独的测试集要用于该图。因此,不是做任何 cross-validation,更简单的方法是迭代 n_neighbors 值,每次都将模型拟合到整个训练集,并计算该模型的准确度分数(例如使用 accuracy_score),一个用于训练集,一个用于测试集。这种方法在您的情况下是可行的,因为目标是生成绘图,并且您对 n_neighbors.

之外的任何其他超参数不感兴趣