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