ValueError:pos_label = 1不是有效的标签:数组([&#39; neg&#39;,&#39; pos&#39;],dtype =&#39; <u3')

时间:2018-05-06 18:21:52

标签: python machine-learning precision precision-recall

=”“

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

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')

我试图改变&#39; pos&#39;在1和&#39; neg&#39;在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',我得到相同的结果:准确度=召回。我想这不对,所以我更改了average='binary',但我有这些错误。有什么想法吗?

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

(pos_label=pos)

表示您的正面评价

因此使用:

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

答案 2 :(得分:0)

遇到此错误时,意味着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)