confusion_matrix错误' DataFrame' "对象不可调用"

时间:2018-05-19 18:11:43

标签: python compiler-errors confusion-matrix

我做错了什么?我将两个vars设置为列表。还尝试了np.array

y = list(y_test.values)
yhat = list(predictions)

print(y)
print(yhat)

confusion_matrix = pd.DataFrame(confusion_matrix(y, yhat), columns=["Predicted False", "Predicted True"], index=["Actual False", "Actual True"])
display(confusion_matrix)

输出:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ..., 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ..., 0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-159-e1640f0e3b13> in <module>()
     45 print(yhat)
     46 
---> 47 confusion_matrix = pd.DataFrame(confusion_matrix(y, yhat), columns=["Predicted False", "Predicted True"], index=["Actual False", "Actual True"])
     48 display(confusion_matrix)
     49 

TypeError: 'DataFrame' object is not callable

不确定这里发生了什么......

2 个答案:

答案 0 :(得分:3)

你是在笔记本上做的吗?如果是这样,可能第一次调用时,confusion_matrix方法已被DataFrame遮蔽。尝试更改变量名称并重新启动内核。

答案 1 :(得分:1)

from sklearn.metrics import confusion_matrix
y_true = [2, 0, 2, 2, 0, 1]
y_pred = [0, 0, 2, 2, 0, 2]

当我运行confusion_matrix(y_true, y_pred)时,结果如下:

array([[2, 0, 0],
       [0, 0, 1],
       [1, 0, 2]], dtype=int64)

请注意,对于此特定输入,结果是3x3矩阵,因此对于这种情况,您将需要一个列和列和索引的三个名称。 您可以将结果直接放入Dataframe中,如下所示:

pd.DataFrame(confusion_matrix(y_true, y_pred),columns=['column 1','column 2','column 3'], index=['index 1', 'index 2','index 3'])

给出以下结果:

     column 1  column 2  column 3
index 1         2         0         0
index 2         0         0         1
index 3         1         0         2