I am trying to write the following formula in Python (3.7):
But unfortunately I am getting a TypeError: ‘int’ object is not callable . Below is the code I am trying to run:
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)