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.

채택된 답변

Jeff Miller
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
Ali
Ali 2023년 7월 18일
편집: Ali 2023년 7월 18일
Dear Jeff
Thank you very much for your accurate answer. I got the concept of these materics, but I can't write the codes in Matlab. Also, there are plenty of codes on the internet which bring different answers of Accuracy, Precision and Recall. Is it possible for you to comment the relevant codes below in order to calculate mentioned parameters?
I already have these codes however I'm not sure about their authenticity, especially FP and FN equations :
CM=confusionmat(realdataset,predicteddataset);
for i=1:3
TP(i)=CM(i,i);
FP(i)=sum(CM(:,i))-CM(i,i);
TN(i)=sum(CM(:))-sum(CM(i,:))-sum(CM(:,i))+CM(i,i);
FN(i)=sum(CM(i,:))-CM(i,i);
end
Jeff Miller
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 CenterFile Exchange에서 Design of Experiments (DOE)에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by