输出与预测不符

Posted

技术标签:

【中文标题】输出与预测不符【英文标题】:Output is not as expected in the prediction 【发布时间】:2021-08-18 11:27:24 【问题描述】:

我有这样的训练和测试数据集:

train: 2000 files
hit.txt
nohit.txt
hit.txt

Test: 1500 
hit.txt
nohit.txt
hit.txt

我训练了一个模型,得到了 74% 的准确率;这是下面的代码:

但是当我对测试数据集进行预测时,我得到的分数是一个我不想要的数组。

test_dir = 'test/'
dictionary = make_dic(test_dir)

features_, labels_ = make_dataset(dictionary)

calibrated_pred_final = calibrated_clf_pipe.predict(features_)

calibrated_pred_final
array([1, 1, 1, ..., 1, 1, 0])

test_pred_final = calibrated_clf_pipe.predict_proba(features_)
import numpy as np
batch_y = np.array(test_pred_final).flatten()

f = open('scores.txt', 'w')
for i in range(len(batch_y)):
    f.write(str(batch_y))    

score.txt 文件长这样,

[0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306] this goes forever.

我期望的是每封测试邮件的分数,如下所示。

 0.38636364     
 0.61363636 
 0.05147059
    ...
    ...

我不确定出了什么问题。为什么我在一个重复 1500 次的数组中得到这个分数?我假设这个分数数组代表每封邮件的分数,但我如何删除整个列表并让每封邮件只有一个分数,就像我的预期结果一样?

【问题讨论】:

【参考方案1】:

预测符合预期。问题在于您写信给score.txt 的方式。您错过了对batch_y 的索引,因此它为数组中的每个元素添加了整个数组。这是更新的代码:

test_dir = 'test/'
dictionary = make_dic(test_dir)

features_, labels_ = make_dataset(dictionary)

calibrated_pred_final = calibrated_clf_pipe.predict(features_)

calibrated_pred_final
array([1, 1, 1, ..., 1, 1, 0])

# line changed below.
test_pred_final = calibrated_clf_pipe.predict_proba(features_)

import numpy as np
batch_y = np.array(test_pred_final).flatten()

f = open('scores.txt', 'w')
for i in range(len(batch_y)):
    f.write(str(batch_y[i]) + '\n') # You missed indexing into batch_y here

如果您想要概率,您可以继续使用predict_proba。如果您想要 0 1 个预测,请使用 predict

【讨论】:

我的测试数据确实有 1500 个数据点,但是每个数据点都是一个单独的文本文件。所以我希望输出如上所示。 使用您的索引代码,我的输出仍然如下所示[0.42857147 0.0618750] 0.71875 0.0611470.0620770.06207 0.0611870.0611770 0.71875 ... 0.71875 ... 0.71875 ... 0.0611730] 0.62] [0.42857143 0.94117647 0.0625 ... 0.84210526 0.71875 0.62] [0.42857143 0.94117647 0.0625 ... 0.84210526 0.71875 0.62] [0.42857143 0.94117647 0.0625 ... 0.84210526 0.71875 0.62] [0.42857143 0.94117647 0.0625 ... 0.84210526 0.71875 0.62] 跨度> 对。然后你需要提取概率最高的索引。那你为什么用predict_proba?你应该使用predict 你知道为什么我们会为单个数据点行获得这么多概率吗? 它基于您使用的分类器。通常,当使用predict_proba 时,您会得到数据集中每个类为正的概率。这就是为什么你得到这么多概率(每个班级)。如果你使用predict,那么它只给出概率最高的类。

以上是关于输出与预测不符的主要内容,如果未能解决你的问题,请参考以下文章

BCEWithLogitsLoss:试图将预测标签的二进制输出作为张量,与输出层混淆

分类与回归的关系和区别

与 foreach 并行预测 nnet 输出时 R 内存爆炸

回归(regression)与分类(classification)的区别

分类与回归的区别

OpenMP 并行代码与串行代码的输出不同