Raksha
Raksha

Reputation: 1699

confusion_matrix error 'DataFrame' "object is not callable"

What am I doing wrong? I set both vars to list. Also tried 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)

Out:

[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

Not sure what's going on here...

Upvotes: 3

Views: 3920

Answers (2)

Ivanovitch
Ivanovitch

Reputation: 368

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

When I run confusion_matrix(y_true, y_pred) the result is the following:

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

Be aware that the result, for this particular input, is a 3x3 matrix, so for this case you will need a list with three names for the columns and index. You can put the result directly into a Dataframe like so:

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

which give the following result:

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

Upvotes: 2

Bruno L
Bruno L

Reputation: 909

Are you doing that in a notebook? If so, maybe the confusion_matrix method has been shadowed by the DataFrame when you called it the first time. Try to change the variable name and restart the kernel.

Upvotes: 6

Related Questions