不要在每次折叠后记录精度和召回值,而是存储预测在每次折叠后的测试样品上。下一个,collect所有测试(即袋外)预测以及计算精度和召回率。
## let test_samples[k] = test samples for the kth fold (list of list)
## let train_samples[k] = test samples for the kth fold (list of list)
for k in range(0, k):
model = train(parameters, train_samples[k])
predictions_fold[k] = predict(model, test_samples[k])
# collect predictions
predictions_combined = [p for preds in predictions_fold for p in preds]
## let predictions = rearranged predictions s.t. they are in the original order
## use predictions and labels to compute lists of TP, FP, FN
## use TP, FP, FN to compute precisions and recalls for one run of k-fold cross-validation
在一次完整的 k 倍交叉验证运行中,预测器对每个样本进行一次且仅有一次的预测。给定 n 个样本,您应该有 n 个测试预测。
(注意:这些预测与训练预测不同,因为预测器对每个样本进行预测而之前没有见过它。)
除非你正在使用留一交叉验证,k 折交叉验证通常需要对数据进行随机划分。理想情况下,你会做repeated (and 分层) k 折交叉验证。然而,结合不同轮次的精确率-召回率曲线并不简单,因为你cannot与 ROC 不同,在精确回忆点之间使用简单的线性插值(参见戴维斯和戈德里奇 2006 http://pages.cs.wisc.edu/~jdavis/davisgoadrichcamera2.pdf).
我个人计算过AUC-PR使用 Davis-Goadrich 方法在 PR 空间中进行插值(随后进行数值积分),并使用来自重复分层 10 倍交叉验证的 AUC-PR 估计值来比较分类器。
为了得到一个漂亮的图,我展示了其中一轮交叉验证的代表性 PR 曲线。
当然,还有许多其他评估分类器性能的方法,具体取决于数据集的性质。
例如,如果数据集中(二进制)标签的比例没有倾斜(即大约 50-50),您可以使用更简单的 ROC 分析和交叉验证:
收集每个折叠的预测并构建 ROC 曲线(如前所述),收集所有 TPR-FPR 点(即取所有 TPR-FPR 元组的并集),然后绘制具有可能平滑的组合点集。或者,使用简单线性插值和复合梯形方法进行数值积分来计算 AUC-ROC。