How can I calculate True positive, False positive, True negative and False negative of real and predicted dataset?
77 views (last 30 days)
Show older comments
Hi everyone. I have two dataset (real and predicted) and I have to calculate TP,TN, FP and FN in order to get accuracy, precision and recall. The dataset are as follows:
real: [1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3]
predicted: [2 2 3 2 1 2 3 2 3 3 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3]
As there are plenty of function, I have confused. I apperciate for your helps.
Thank you.
0 Comments
Accepted Answer
Jeff Miller
on 18 Jul 2023
First you need to map your 3 values 1-3 onto just two categories: positive and negative. For example, you might decide to call the 1's and 2's positive and the 3's negative. Or maybe you think of the 2's and 3's as positive and the 1's as negative.
After you have done that, you just need to count the numbers of the different combinations. For example, the number of cases where the real and predicted values are both positive is the number of true positives. The number of false positives is the number of cases where the prediction was positive but the real value was negative. And so on. You can use the crosstab function to get those counts, for example. Convert the counts to proportions of the total sample size if you want.
2 Comments
Jeff Miller
on 18 Jul 2023
I don't think you can use 'confusionmat' because it is still considering 3 possible outcomes rather than two. I would do it like this:
% An example considering 1 & 2 as positive and 3 as negative:
real = [1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3];
predicted = [2 2 3 2 1 2 3 2 3 3 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3];
realPos = (real==1) | (real == 2);
realNeg = ~realPos;
predictedPos = (predicted==1) | (predicted == 2);
predictedNeg = ~predictedPos;
TP = sum(predictedPos & realPos) % 6
FP = sum(predictedPos & realNeg) % 1
TN = sum(predictedNeg & realNeg) % 27
FN = sum(predictedNeg & realPos) % 6
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!