Skip to main content
 首页 » 编程设计

python之混淆矩阵错误 'DataFrame' "object is not callable"

2025年05月04日23rubylouvre

我做错了什么?我将两个变量都设置为列表。还尝试了 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 

不知道这里发生了什么......

请您参考如下方法:

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