# How to plot Confusion Matrix with tolerence limit ?

5 views (last 30 days)
RAJEEV on 2 May 2023
Commented: RAJEEV on 10 May 2023
I have Measured Output and Predicted output. I want to create a confusion matrix with 10% tolerance limit. How can I plot ?
Predicted Value = P_out
Target Value = T_out
max = 1.1*T_out
min = 0.9*T_out
True Positive: (P_out >= T_out) and (P_out<= max)
True Negative: (P_out <= T_out) and (P_out>=min)
False Positive: (P_out > max)
False Positive: (P_out < max)
I have attached the values for your reference.

MarKf on 2 May 2023
Edited: MarKf on 2 May 2023
So you just want to translate the pseudo code above into working code and choose how to best plot it. The pseudo code probably does not do what you want, seeing the result, and needs some troubleshooting (see comments in code). In any case, it wouldn't be a confusion matrix if elements are allowed in more than one cell and the sum is more than the total. But here it is:
T_out = confdat(:,1); % I had assumed Predicted Value to be the first column since that's how you ordered them
P_out = confdat(:,2); % then I actually checked the xls file
maxTou = 1.1.*T_out;
minTou = 0.9.*T_out;
Tr_Pos = (P_out >= T_out) & (P_out <= maxTou);
Tr_Neg = (P_out <= T_out) & (P_out >= minTou); % all 0s (try switching maxTou/minTou here and line above)
Fa_Pos = (P_out > maxTou); % all 1s here, that's what I meant with "probably does not do what you want"
Fa_Neg = (P_out < maxTou); % see above, I'll assume you meant False Negative here (maybe minTou)
conmat = [sum(Tr_Pos) sum(Fa_Pos); sum(Fa_Neg) sum(Tr_Neg)];
cm = confusionchart(conmat); %this is one way to plot, convenient
cm.Title = 'Confusion ± 10% tolerance';
cm.RowSummary = 'row-normalized'; %adds percentage rows (or % cm.ColumnSummary = 'column-normalized'; columns),% you can omit this RAJEEV on 10 May 2023
Thank You.