artemis
artemis

Reputation: 7281

Calculating Precision and Recall by Class

I am working with a two class dataset, 2 and 4 where 2 is the positive class and 4 is the negative class (regarding sentiment analysis).

I have a set of predictions from my model, and a set of actual values. I need to determine Precision and Recall for each class (the P and R scores for the positive and negative class).

Code is below:

preds = [4, 4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  2,  4,  4,  4,  4,  4,  2,  2,  4,  4,  4,  4,  2]
actuals = [2,   4,  2,  4,  2,  4,  2,  4,  4,  4,  2,  4,  4,  4,  2,  2,  4,  4,  2,  4,  4,  4,  4,  4,  4,  2]

true_pos = 0
true_neg = 0
false_pos = 0
false_neg = 0
for pred, act in zip(preds, actuals):
    # 2 is positive, 4 is negative

    if(pred == 2 & act == 2):
        true_pos += 1

    elif(pred == 4 & act == 4):
        true_neg += 1

    elif(pred == 2 & act == 4):
        false_pos += 1

    elif(pred == 4 & act == 2):
        false_neg += 1

print("True Positive: ", true_pos)
print("True Negative: ", true_neg)
print("False Positive: ", false_neg)
print("False Negative: ", false_neg)

Which yields:

True Positive:  1
True Negative:  14
False Positive:  0
False Negative:  0

However, I am really stuck on how I am supposed to be calculating these metrics by class. This SO post indicates how to do this for an overall, but not by class.

Ideally, I would end up with an output like:

Class 2 P Score: x
Class 2 R Score: x
Class 4 P Score: x 
Class 4 R Score: x

but I am not sure how to compute that.

How can I adapt my current logic to check for Precision and Recall scores for each class with the above data?

Upvotes: 2

Views: 953

Answers (1)

ycx
ycx

Reputation: 3211

I think I know which direction to point you in:
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html
This should be what you are looking for.

Refer to the wikipedia link: https://en.wikipedia.org/wiki/Confusion_matrix
and read up on how to use this.

Upvotes: 1

Related Questions