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

조회 수: 18 (최근 30일)
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?

채택된 답변

MathWorks Support Team
MathWorks Support Team 2017년 7월 5일
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;
  댓글 수: 1
Michael Abboud
Michael Abboud 2017년 7월 6일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Franco
David Franco 2018년 1월 23일
편집: MathWorks Support Team 2018년 3월 16일
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)

카테고리

Help CenterFile Exchange에서 Model Building and Assessment에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by