【问题标题】:ValueError: pos_label=1 is not a valid label: array(['neg', 'pos'], dtype='<U3')ValueError:pos_label=1 不是有效标签:array(['neg', 'pos'], dtype='<U3')
【发布时间】:2018-10-16 14:30:48
【问题描述】:

我在尝试获取召回分数时收到此错误。

X_test = test_pos_vec + test_neg_vec
Y_test = ["pos"] * len(test_pos_vec) + ["neg"] * len(test_neg_vec)

recall_average = recall_score(Y_test, y_predict, average="binary")

print(recall_average)

这会给我:

    C:\Users\anca_elena.moisa\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\metrics\classification.py:1030: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if pos_label not in present_labels:
Traceback (most recent call last):
  File "G:/PyCharmProjects/NB/accuracy/script.py", line 812, in <module>
    main()
  File "G:/PyCharmProjects/NB/accuracy/script.py", line 91, in main
    evaluate_model(model, train_pos_vec, train_neg_vec, test_pos_vec, test_neg_vec, False)
  File "G:/PyCharmProjects/NB/accuracy/script.py", line 648, in evaluate_model
    recall_average = recall_score(Y_test, y_predict, average="binary")
  File "C:\Users\anca_elena.moisa\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\metrics\classification.py", line 1359, in recall_score
    sample_weight=sample_weight)
  File "C:\Users\anca_elena.moisa\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\metrics\classification.py", line 1036, in precision_recall_fscore_support
    (pos_label, present_labels))
ValueError: pos_label=1 is not a valid label: array(['neg', 'pos'],
      dtype='<U3')

我尝试以这种方式将 'pos' 转换为 1 并将 'neg' 转换为 0:

for i in range(len(Y_test)):
     if 'neg' in Y_test[i]:
         Y_test[i] = 0
     else:
         Y_test[i] = 1

但这给了我另一个错误:

    C:\Users\anca_elena.moisa\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\metrics\classification.py:181: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  score = y_true == y_pred
Traceback (most recent call last):
  File "G:/PyCharmProjects/NB/accuracy/script.py", line 812, in <module>
    main()
  File "G:/PyCharmProjects/NB/accuracy/script.py", line 91, in main
    evaluate_model(model, train_pos_vec, train_neg_vec, test_pos_vec, test_neg_vec, False)
  File "G:/PyCharmProjects/NB/accuracy/script.py", line 648, in evaluate_model
    recall_average = recall_score(Y_test, y_predict, average="binary")
  File "C:\Users\anca_elena.moisa\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\metrics\classification.py", line 1359, in recall_score
    sample_weight=sample_weight)
  File "C:\Users\anca_elena.moisa\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\metrics\classification.py", line 1026, in precision_recall_fscore_support
    present_labels = unique_labels(y_true, y_pred)
  File "C:\Users\anca_elena.moisa\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\multiclass.py", line 103, in unique_labels
    raise ValueError("Mix of label input types (string and number)")
ValueError: Mix of label input types (string and number)

我要做的是获取指标:准确度、精度、召回率、f_measure。使用average='weighted',我得到相同的结果:accuracy=recall。我想这是不正确的,所以我更改了average='binary',但我有这些错误。有什么想法吗?

【问题讨论】:

  • 你能发布你的recall_average方法吗?
  • recall_average 只是一个变量名
  • 抱歉,我在发布此内容时将变量 recall_average 重命名为 recall。现在我编辑了帖子。
  • 它们来自python import from sklearn.metrics import roc_curve, auc, f1_score, recall_score, precision_score
  • @MihaiAlexandru-Ionut recall_score() 是来自 sklearn 的函数。由于 OP 正在进行二进制分类,只需在对 recall_score() 的调用中设置 pos_label='neg'

标签: python machine-learning precision precision-recall


【解决方案1】:

当您遇到此错误时,这意味着您的 target 变量的值不是 recall_score() 的预期值,默认情况下,1 表示正例0 表示负例案例 [这也适用于precision_score()]

根据你提到的错误:

pos_label=1 is not a valid label: array(['neg', 'pos']

很明显,您的积极情景的值是pos 而不是1,消极情景的值是neg 而不是0

那么你有两个选项来解决这个不匹配:

  • 更改recall_score() 中的默认值以考虑当pos 出现时的积极情况:
recall_average = recall_score(Y_test, y_predict, average="binary", pos_label='pos') 
  • 将数据集中目标变量的值更改为10
Y_test = Y_test.map({'pos': 1, 'neg': 0}).astype(int)

【讨论】:

  • 不应该是“pos_label='pos'”,“pos”周围有连字符吗?
  • @Moritz 是的!很好发现
【解决方案2】:

recall_average = recall_score(Y_test, y_predict, pos_label="no")

pos_label 的数组只有 ["yes","no"]

【讨论】:

    【解决方案3】:

    (pos_label=pos)表明你的积极类

    所以使用:

    Recall=recall_score(Y_test, Y_predict, pos_label='pos') 
    

    【讨论】:

      【解决方案4】:
      recall_average = recall_score(Y_test, y_predict, average="binary", pos_label="neg")
      

      "neg""pos" 用作pos_label,此错误不会再次出现。

      【讨论】:

      • 因为召回率和精度有两个分数 i) 就 -ve 值而言 ii) 就 +ve 值而言 所以你需要在 pos_label 中传递这些值之一,以便函数可以返回分数那个 pos 标签的
      • 感谢此评论确实添加了答案有效的原因。
      • 我现在收到此错误 ValueError: pos_label='neg' is not a valid label: array(['0', '1'], dtype='
      猜你喜欢
      • 2021-04-16
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 2015-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多