How to create a confusion matrix.
Show older comments
How to create a confusion matrix for clustering? For this how can I get predicted class and actual class? And how can I get those TP, TN, FP and FN values from it? I am confused please help me.
Thanks in advance.
2 Comments
Benjamin Großmann
on 9 Mar 2020
Edited: Benjamin Großmann
on 9 Mar 2020
- Create confusion matrix with
conf_mat = confusion(targets,outputs);
- The predicted class (outputs) is the result of your net for a given input (after training), the "actual class" is your label (targets)
- We can now compare target and output for each sample and for one class "c" against all others:
- target == c & output == c --> TP (The output is positive and that is true)
- target == c & output ~= c --> FN (The output is negative but that is false)
- target ~= c & output == c --> FP (The output is positive but that is false)
- target ~= c & output ~= c --> TN (The output is negative and that is true)
sreelekshmi ms
on 9 Mar 2020
Answers (1)
Benjamin Großmann
on 9 Mar 2020
Lets use the cifar10 demo included in Matlab for your question
clearvars
close all
clc
load('Cifar10Labels.mat','trueLabels','predictedLabels');
[m,order] = confusionmat(trueLabels,predictedLabels);
figure
cm = confusionchart(m,order);
Please look at the confusionchart and consider the following for one particular class c,
- The diagonal element of class c is the amount of TP
- Everything inside the predicted class column except the diagonal element is falsely predicted as class c --> FP
- Everything inside the true class row except the diagonal element is of class c but not predicted as c --> FN
- Every other diagonal element except the diagonal element of class c itself is TN.
Now, we can put this easily in in code by summing up row, column and diagonal elements and substracting the TP. Lets pick a class, e.g. c=2 "automobile", and calculate TP, FP, FN, TN for that class
c = 2;
TP = cm.NormalizedValues(c,c) % true class is c and predicted as c
FP = sum(cm.NormalizedValues(:,c))-TP % predicted as c, true class is not c
FN = sum(cm.NormalizedValues(c,:))-TP % true class is c, not predicted as c
TN = sum(diag(cm.NormalizedValues))-TP % true class is not c, not predicted as c
3 Comments
Benjamin Großmann
on 9 Mar 2020
In addition, an approach to get the confusion matrixes for a single class and therefor their TP,FP,FN,TN values
clearvars
close all
clc
load('Cifar10Labels.mat','trueLabels','predictedLabels');
[m,order] = confusionmat(trueLabels,predictedLabels);
figure
cm = confusionchart(m,order);
cmv = cm.NormalizedValues;
% confusion matrix for a single class
conf_mat_sc = @(c) [cmv(c,c), sum(cmv(:,c))-cmv(c,c);
sum(cmv(c,:))-cmv(c,c), sum(diag(cmv))-cmv(c,c);];
cms = arrayfun(conf_mat_sc, [1:numel(order)], 'UniformOutput', false);
sreelekshmi ms
on 9 Mar 2020
sreelekshmi ms
on 10 Mar 2020
Categories
Find more on Visualization and Interpretability in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!