python - UndefinedMetricWarning : F-score is ill-defined and being set to 0. 0 在没有预测样本的标签中

标签 python scikit-learn

我收到了这个奇怪的错误:

classification.py:1113: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 in labels with no predicted samples.
'precision', 'predicted', average, warn_for)`

但它也会在我第一次运行时打印 f 分数:

metrics.f1_score(y_test, y_pred, average='weighted')

我第二次运行时,它提供的分数没有错误。这是为什么呢?

>>> y_pred = test.predict(X_test)
>>> y_test
array([ 1, 10, 35,  9,  7, 29, 26,  3,  8, 23, 39, 11, 20,  2,  5, 23, 28,
       30, 32, 18,  5, 34,  4, 25, 12, 24, 13, 21, 38, 19, 33, 33, 16, 20,
       18, 27, 39, 20, 37, 17, 31, 29, 36,  7,  6, 24, 37, 22, 30,  0, 22,
       11, 35, 30, 31, 14, 32, 21, 34, 38,  5, 11, 10,  6,  1, 14, 12, 36,
       25,  8, 30,  3, 12,  7,  4, 10, 15, 12, 34, 25, 26, 29, 14, 37, 23,
       12, 19, 19,  3,  2, 31, 30, 11,  2, 24, 19, 27, 22, 13,  6, 18, 20,
        6, 34, 33,  2, 37, 17, 30, 24,  2, 36,  9, 36, 19, 33, 35,  0,  4,
        1])
>>> y_pred
array([ 1, 10, 35,  7,  7, 29, 26,  3,  8, 23, 39, 11, 20,  4,  5, 23, 28,
       30, 32, 18,  5, 39,  4, 25,  0, 24, 13, 21, 38, 19, 33, 33, 16, 20,
       18, 27, 39, 20, 37, 17, 31, 29, 36,  7,  6, 24, 37, 22, 30,  0, 22,
       11, 35, 30, 31, 14, 32, 21, 34, 38,  5, 11, 10,  6,  1, 14, 30, 36,
       25,  8, 30,  3, 12,  7,  4, 10, 15, 12,  4, 22, 26, 29, 14, 37, 23,
       12, 19, 19,  3, 25, 31, 30, 11, 25, 24, 19, 27, 22, 13,  6, 18, 20,
        6, 39, 33,  9, 37, 17, 30, 24,  9, 36, 39, 36, 19, 33, 35,  0,  4,
        1])
>>> metrics.f1_score(y_test, y_pred, average='weighted')
C:\Users\Michael\Miniconda3\envs\snowflakes\lib\site-packages\sklearn\metrics\classification.py:1113: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 in labels with no predicted samples.
  'precision', 'predicted', average, warn_for)
0.87282051282051276
>>> metrics.f1_score(y_test, y_pred, average='weighted')
0.87282051282051276
>>> metrics.f1_score(y_test, y_pred, average='weighted')
0.87282051282051276

另外,为什么会有尾随 'precision', 'predicted', average, warn_for) 错误信息?没有左括号,为什么它以右括号结尾?我在 Windows 10 的 conda 环境中使用 Python 3.6.0 运行 sklearn 0.18.1。

我也看了here我不知道这是否是同一个错误。这个SO post也没有解决办法。

最佳答案

正如评论中提到的,y_test 中的一些标签不会出现在 y_pred 中。特别是在这种情况下,永远不会预测标签“2”:

>>> set(y_test) - set(y_pred)
{2}

这意味着没有要计算此标签的 F-score,因此这种情况下的 F-score 被认为是 0.0。由于您要求平均分,因此您必须考虑到计算中包含 0 分,这就是 scikit-learn 向您显示该警告的原因。

这让我看到你没有第二次看到错误。正如我所提到的,这是一个警告,它的处理方式与 python 中的错误不同。大多数环境中的默认行为是仅显示一次特定警告。可以更改此行为:

import warnings
warnings.filterwarnings('always')  # "error", "ignore", "always", "default", "module" or "once"

如果您在导入其他模块之前设置此项,则每次运行代码时都会看到警告。

除了设置 warnings.filterwarnings('ignore') 之外,没有办法避免第一次看到此警告。您可以做的,是确定您对未预测的标签分数不感兴趣,然后明确指定您感兴趣的标签(即标签预测至少一次):

>>> metrics.f1_score(y_test, y_pred, average='weighted', labels=np.unique(y_pred))
0.91076923076923078

警告将消失。

关于python - UndefinedMetricWarning : F-score is ill-defined and being set to 0. 0 在没有预测样本的标签中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43162506/

相关文章:

python - 将参数限制为 python 中的特定值

python - 为什么我们需要 python 中的协程?

python - 打开一个 csv 文件,删除名称中包含单词的列,保存在一个新的 csv 中 Pandas,Python

python - 调用保存的分类器时无法转换数组数据

python - 在 tensorflow 中读取大型数据集

python - 如何有效合并 EMR 上的 Spark 输出文件?

python - 在 Google App Engine 中将二进制文件转换为 PIL Image 数据类型

python - 如何将一列值转换为可能离散的训练输出类?

python - 绘制用 sklearn 制作的回归量的 3d 图

python - 如何在 scikit-learn 中生成自定义交叉验证生成器?