How can I calculate True positive, False positive, True negative and False negative of real and predicted dataset?
조회 수: 27 (최근 30일)
이전 댓글 표시
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
채택된 답변
Jeff Miller
2023년 7월 18일
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
Jeff Miller
2023년 7월 18일
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
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!