# How can I calculate True positive, False positive, True negative and False negative of real and predicted dataset?

97 views (last 30 days)
Ali on 17 Jul 2023
Commented: Jeff Miller on 18 Jul 2023
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 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.
Ali on 18 Jul 2023
Edited: Ali on 18 Jul 2023
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 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