How can I calculate True positive, False positive, True negative and False negative of real and predicted dataset?
58 次查看(过去 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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Geographic Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!