ValueError:pos_label = 1不是有效的标签:array(['neg','pos'],dtype ='< U3') [英] ValueError: pos_label=1 is not a valid label: array(['neg', 'pos'], dtype='<U3')

查看:856
本文介绍了ValueError:pos_label = 1不是有效的标签:array(['neg','pos'],dtype ='< U3')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试获取召回得分时收到此错误.

I recieve this error while trying to obtain the recall score.

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:

I tried to transform 'pos' in 1 and 'neg' in 0 this way:

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

但这给了我另一个错误:

But this is giving me another error:

    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',可以获得相同的结果:precision = recall.我猜这是不正确的,因此我更改了 average ='binary',但是我遇到了这些错误.有什么想法吗?

What I am trying to do is to obtain the metrics: accuracy, precision, recall, f_measure. With average='weighted', I obtain the same result: accuracy=recall. I guess this is not correct, so I changed the average='binary', but I have those errors. Any ideas?

推荐答案

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

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

Use "neg" or "pos" as pos_label and this error won't raise again.

这篇关于ValueError:pos_label = 1不是有效的标签:array(['neg','pos'],dtype ='&lt; U3')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆