Issue
I have a data frame as shown below.
id label prediction
1 cat cat
2 dog cat
3 cow dog
4 cow cow
5 dog cat
6 cat cat
7 cat cat
8 dog dog
9 dog dog
10 cat cat
from the above df, I would like to calculate overall accuracy using pandas.
I tried the below code to calculate class-wise accuracy.
class_wise_accuracy = (df.groupby('label')['prediction']
.value_counts(normalize=True)
.unstack(fill_value=0)
)
confusion_matrix = (df.groupby('label')['prediction']
.value_counts()
.unstack(fill_value=0)
.reset_index()
)
Expected Output:
overall_accuracy = (4+1+2)/df.shape[0] = 0.7
Solution
IIUC, use crosstab
and the underlying numpy array:
a = pd.crosstab(df['label'], df['prediction']).to_numpy()
overall_accuracy = a.diagonal().sum()/a.sum()
output: 0.7
intermediates:
pd.crosstab(df['label'], df['prediction'])
prediction cat cow dog
label
cat 4 0 0
cow 0 1 1
dog 2 0 2
.tonumpy()
array([[4, 0, 0],
[0, 1, 1],
[2, 0, 2]])
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.