Reputation: 122
I'm trying to compute code to find accuracy score from one-hot MNIST letters data. I want to calculate the accuracy per label in MNIST data because I use the same for precision, recall, and f1 score. y_true
is dataframe [88800, 26]
. First, I define true positive, true negative and the others. My code is:
for i in y_true:
x=y_true[i]
y=y_pred[i]
for j in range(len(x)):
if (x.values[j] == 1) and (y.values[j] == 1):
print("True Positive", y_pred.columns[i-1])
elif (x.values[j] == 0) and (y.values[j] == 0):
print("True Negative", y_pred.columns[i-1])
elif (x.values[j] == 0) and (y.values[j] == 1):
print("False Positive", y_pred.columns[i-1])
else:
print("False Negative", y_pred.columns[i-1])
And the output is:
True Positive 1
True Positive 1
True Negative 1
...
True Negative 26
Till as much as the rows where 1 and 26 are each label. But, I realize, that I cannot count how much True Positive, True Negative, False Positive and False Negative per label from print result. I dunno have idea how to count it. Is it possible to count it from print result?
Upvotes: 0
Views: 158
Reputation: 11073
You can use Counter
In your code:
from collections import Counter
c = Counter()
for j in range(len(x)):
if (x.values[j] == 1) and (y.values[j] == 1):
print("True Positive", y_pred.columns[i-1])
c.update([f'"True Positive" {y_pred.columns[i-1]}'])
elif (x.values[j] == 0) and (y.values[j] == 0):
print("True Negative", y_pred.columns[i-1])
c.update([f'"True Negative" {y_pred.columns[i-1]}'])
elif (x.values[j] == 0) and (y.values[j] == 1):
print("False Positive", y_pred.columns[i-1])
c.update([f'"False Positive" {y_pred.columns[i-1]}'])
else:
print("False Negative", y_pred.columns[i-1])
c.update([f'"False Negative" {y_pred.columns[i-1]}'])
After this, the c
will be your desired output.
For printing the output use this:
for k,v in dict(c).items():
print(k,':',v)
Upvotes: 0