我试图使用metrics.average_precision_score来计算我的sklearn KNN模型的平均精度,我的代码如下:
average_precision = metrics.average_precision_score(y_test.values, np.array(y_pre.tolist))
但我得到了这个错误:
...File "C:\My software\Anaconda\lib\site-packages\sklearn\metrics\_ranking.py", line 211, in average_precision_score "y_true." % pos_label)ValueError: pos_label=1 is invalid. Set it to a label in y_true.
为了检查我的y_test和y_pre,我使用了以下代码:
print(type(y_test))print(type(y_test.values))print(type(y_pre))print(y_test.values)print(y_pre)
我得到了以下结果:
<class 'pandas.core.series.Series'><class 'numpy.ndarray'><class 'numpy.ndarray'>[3 3 3 3 3 2 3 3 3 2 2 3 3 2 3 3 3 2 3 2 3 3 3 2 3 2 3 3 3 3 3 2 2 3 2 3 3 3 3 2 3 2 3 3 2 3 3 3 2 3 3 3 3 2 3 3 3 3 2 3 3 3 3 3 3 2 3 3 2 3 3 3 3 3 3 3 3 3 2 3 3 2 2 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 2 3 3 2 3 2 3 3 2 2 3 2 3 2 2 3 2 3 3][3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3]
我不知道为什么这不起作用,请帮帮我,谢谢。
回答:
将y_test.values
和y_pre
中的标签映射到0
和1
,而不是3
和2
。
请记住:1
必须是正标签。