I have two columns in a pandas dataframe : label(ground truth) and pred (model prediction). I have 4 classes in the labels: dog, cat, elephant and snake. What I want is the accuracy or precision of the prediction per class. For instance if I have dataframe below:
label pred
dog cat
elephant elephant
dog snake
cat cat
snake snake
snake cat
dog dog
What I do is use value_counts for each class and then manually plug in the ratios to get the accuracy in pandas. The problem is that value_counts is sorted by raw count numbers so the order for label and pred can be different.
numerators = df[pred].value_counts()
denominators = df[label].value_counts()
and then I get the outputs:
dog 0.33
cat 1
snake 0.5
elephant 1
Is there a way to automate this in pandas?
