Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: 'int' object is not callable in Python (3.7)

I am trying to write the following formula in Python (3.7):

CBA formula

But unfortunately I am getting a TypeError: ‘int’ object is not callable . Below is the code I am trying to run:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import numpy as np

##CBA
cm=np.array([[20,0,0],[1,14,0],[0,0,13]])
print(cm)

for i in range(3):
    a = cm[i,i]
    sum1 = cm[i,0]+cm[i,1]+cm[i,2]
    sum2 = cm[0,i] + cm[1,i] + cm[2,i]
    eq = ((a / max(sum1,sum2))/3)
    add = sum(eq)
    print(add)

Here is the error:

 """
import numpy as np

##CBA
cm=np.array([[20,0,0],[1,14,0],[0,0,13]])
print(cm)

for i in range(3):
    a = cm[i,i]
    sum1 = cm[i,0]+cm[i,1]+cm[i,2]
    sum2 = cm[0,i] + cm[1,i] + cm[2,i]
    eq = ((a / max(sum1,sum2))/3)
    add = sum(eq)
    print(add)
[[20  0  0]
 [ 1 14  0]
 [ 0  0 13]]

Traceback (most recent call last):

  File "<ipython-input-1-72bf927660e0>", line 17, in <module>
    add = sum(eq)

TypeError: 'numpy.float64' object is not iterable

How can I sum the eq variable?

>Solution :

If eq is just a single variable (and not an array), then you don’t use the sum function at all. You would need:

##CBA
cm=confusion_matrix(y_test,y_pred)

sumx = 0
for i in range(3):
    a = cm[i,i]
    sum1 = cm[i,0] + cm[i,1] + cm[i,2]
    sum2 = cm[0,i] + cm[1,i] + cm[2,i]
    sumx += ((a / max(sum1,sum2))/3)
print(sumx)
print(sumx/3)

However, there are many potential improvements to this. If cm is a numpy array, then you can sum up a column or a row at a time:

for i in range(3):
    a = cm[i,i]
    sum1 = np.sum(cm[i,:])
    sum2 = np.sum(cm[:,i])
    sumx += ((a / max(sum1,sum2))/3)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading