How can I plot a confusion matrix for a multi-class or non-binary classification problem?

Asked by MathWorks Support Team

MathWorks Support Team (view profile)

on 1 May 2017
Latest activity Commented on by Fatai Anifowose

Fatai Anifowose (view profile)

on 31 Aug 2019
Accepted Answer by MathWorks Support Team

MathWorks Support Team (view profile)

I want to make a plot similar to the confusion matrix created in the Classification Learner app. This can make a confusion matrix for a multi-class or non-binary classification problem. In addition, it can plot things such as a True Positive or False Negative rates.
How can I do this?

Release

R2017a

Answer by MathWorks Support Team

MathWorks Support Team (view profile)

on 5 Jul 2017

Similar to the binary or two-class problem, this can be done using the "plotconfusion" function. By default, this command will also plot the True Positive, False Negative, Positive Predictive, and False Discovery rates in they grey-colored boxes. Please refer to the following example:
targetsVector = [1 2 1 1 3 2]; % True classes
outputsVector = [1 3 1 2 3 1]; % Predicted classes
% Convert this data to a [numClasses x 6] matrix
targets = zeros(3,6);
outputs = zeros(3,6);
targetsIdx = sub2ind(size(targets), targetsVector, 1:6);
outputsIdx = sub2ind(size(outputs), outputsVector, 1:6);
targets(targetsIdx) = 1;
outputs(outputsIdx) = 1;
% Plot the confusion matrix for a 3-class problem
plotconfusion(targets,outputs)
The class labels can be customized by setting that 'XTickLabel' and 'YTickLabel' properties of the axis:
h = gca;
h.XTickLabel = {'Class A','Class B','Class C',''};
h.YTickLabel = {'Class A','Class B','Class C',''};
h.YTickLabelRotation = 90;

tejasvee

tejasvee (view profile)

on 16 Jun 2017
targetsVec may have input data set with predefined classes(True classes)?
Ivan Abraham

Ivan Abraham (view profile)

on 21 Jun 2017
The classification learner app plots the figure with proper class labels (i.e strings too). Is it possible to do the same with plotconfusion?
On the other hand given a vector of targets and outputs (i.e. of the cell type with strings for class labels) the confusionmat command creates a confusion matrix; how can we plot that matrix like MATLAB does? I suspect it should be possible since internally plotconfusion should generate the confusion matrix at some point.
Michael Abboud

Michael Abboud (view profile)

on 6 Jul 2017
I have updated the above answer to better indicate that the 'TargetsVector' contains the true class labels.
I also included a quick example in the answer showing how to add strings as a name for each class, as I think that is a great easy way to make the plot more easily interpretable

Answer by David Franco

David Franco (view profile)

on 23 Jan 2018
Edited by MathWorks Support Team

MathWorks Support Team (view profile)

on 16 Mar 2018

Implementation code:
Confusion Matrix
function [] = confusion_matrix(T,Y)
M = size(unique(T),2);
N = size(T,2);
targets = zeros(M,N);
outputs = zeros(M,N);
targetsIdx = sub2ind(size(targets), T, 1:N);
outputsIdx = sub2ind(size(outputs), Y, 1:N);
targets(targetsIdx) = 1;
outputs(outputsIdx) = 1;
% Plot the confusion matrix
plotconfusion(targets,outputs)

Anmol Pardeshi

Anmol Pardeshi (view profile)

on 30 Nov 2018
is the plotconfusion function available in the normal versions of MATLAB or does it need to have some special toolbox to be attached to the normal version. I have a normal version and it throws an error at the line where the function is written & the error says that undefined funciton.
MA-Winlab

MA-Winlab (view profile)

on 17 Apr 2019
what are T and Y?
is plotconfusion(targets,outputs) part of the function?

Answer by Fatai Anifowose

Fatai Anifowose (view profile)

on 28 Aug 2019

I am trying to use the "plotconfusion" function in my code but it took a very long time until MATLAB crashed.
What could be the reason for this? I have a high end workstation so I would not expect it to be a memory issue.
Thanks for your help.

Narmada Herath (view profile)

on 28 Aug 2019
Hello,
Do you have a crash log resulting from the crash? I suggest contacting MathWorks Technical Support at
who may be able to assist further.