我试图找到召回但发生输入错误
import pandas as pd
y_test = {'o1': [0,1,0,1],'o2': [1,1,0,1],'o3':[0,0,1,1]}
y_test = pd.DataFrame (y_test)
y_pred = {'o1': [1,1,0,1],'o2': [1,0,0,1],'o3':[1,0,1,1]}
y_pred = pd.DataFrame (y_pred)
y_pred = y_pred.to_numpy()
def precision(y_test, y_pred):
i = set(y_test).intersection(y_pred)
len1 = len(y_pred)
if len1 == 0:
return 0
else:
return len(i) / len1
print("recall of Binary Relevance Classifier: " + str(precision(y_test, y_pred)))
此代码显示错误: 实际上,我尝试找到针对多标签分类的召回产品 错误详情如下
TypeError Traceback (most recent call last)
<ipython-input-41-8f3ca706a8e6> in <module>
16 return len(i) / len1
17
---> 18 print("recall of Binary Relevance Classifier: " + str(precision(y_test, y_pred)))
<ipython-input-41-8f3ca706a8e6> in precision(y_test, y_pred)
9
10 def precision(y_test, y_pred):
---> 11 i = set(y_test).intersection(y_pred)
12 len1 = len(y_pred)
13 if len1 == 0:
TypeError: unhashable type: 'numpy.ndarray'
答案 0 :(得分:1)
您的numpy数组y_test
无法转换为集合(在第11行),因为该数组是二维的。
要将可迭代对象转换为集合,所有项目都必须是可哈希的。对于一维numpy数组来说很好,因为数字是可哈希的:
>>> array_1d = np.array([1, 2, 3])
>>> array_1d
array([1, 2, 3])
>>> set(array_1d)
{1, 2, 3}
但是对于二维数组,您会收到此错误,因为嵌套数组本身不可哈希:
>>> array_2d = np.array([[1,2,3], [1,2,3], [1,2,3]])
>>> array_2d
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
>>> set(array_2d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'numpy.ndarray'