在 cross_val_predict 输出上评估分类器是否正确?
Is it correct to evaluate a classifier on cross_val_predict output?
我有一些分类器。我想使用 classification_report
获取分类器的指标
我用cross_val_predict
得到了预测,然后把它们传给了classification_report
。
我还使用 cross_val_predict
的输出来绘制混淆矩阵。
labels = get_labels() #ground truth
result = cross_val_predict(classifier, features, labels, cv=KFold(n_splits=10, shuffle=True, random_state=seed))
report = classification_report(labels, result, digits=3, target_names=['no', 'yes'], output_dict=True)
cm = confusion_matrix(result, labels, [no, yes])
在 cross_val_predict
文档中:
Passing these predictions into an evaluation metric may not be a valid way to measure generalization performance. Results can differ from cross_validate and cross_val_score unless all tests sets have equal size and the metric decomposes over samples.
所以,这样做是不是错误的方法?我应该怎么做?
我会说你的过程应该是这样的:
- Train/test拆分
- 使用(交叉)验证集选择模型
- 使用整个训练集重新训练您的模型
- 评估第 1 步中的测试拆分
如果您没有大量数据,使用 KFold 进行训练应该比单个 train/test 分割给您更可靠的结果,但根据经验,您应该考虑在 dataset/split 以前没有用过,即使它只是用于模型选择或提前停止。
回到你的问题,cross_val_predict
实际上是将输入数组拆分为 K
个拆分,并使用所有经过训练的 CV 模型来预测 5 个预测拆分,然后组合在一起。我认为您可以使用它来大致了解 cross-validation 结果(例如,如果您想绘制它们或计算其他指标),但绝对不能评估您的模型。
我有一些分类器。我想使用 classification_report
我用cross_val_predict
得到了预测,然后把它们传给了classification_report
。
我还使用 cross_val_predict
的输出来绘制混淆矩阵。
labels = get_labels() #ground truth
result = cross_val_predict(classifier, features, labels, cv=KFold(n_splits=10, shuffle=True, random_state=seed))
report = classification_report(labels, result, digits=3, target_names=['no', 'yes'], output_dict=True)
cm = confusion_matrix(result, labels, [no, yes])
在 cross_val_predict
文档中:
Passing these predictions into an evaluation metric may not be a valid way to measure generalization performance. Results can differ from cross_validate and cross_val_score unless all tests sets have equal size and the metric decomposes over samples.
所以,这样做是不是错误的方法?我应该怎么做?
我会说你的过程应该是这样的:
- Train/test拆分
- 使用(交叉)验证集选择模型
- 使用整个训练集重新训练您的模型
- 评估第 1 步中的测试拆分
如果您没有大量数据,使用 KFold 进行训练应该比单个 train/test 分割给您更可靠的结果,但根据经验,您应该考虑在 dataset/split 以前没有用过,即使它只是用于模型选择或提前停止。
回到你的问题,cross_val_predict
实际上是将输入数组拆分为 K
个拆分,并使用所有经过训练的 CV 模型来预测 5 个预测拆分,然后组合在一起。我认为您可以使用它来大致了解 cross-validation 结果(例如,如果您想绘制它们或计算其他指标),但绝对不能评估您的模型。