[go: up one dir, main page]

0% found this document useful (0 votes)
79 views2 pages

Sample

This document contains a function called plotConfMat that plots confusion matrices. It takes in a confusion matrix and optional labels as inputs. It calculates accuracy percentages from the confusion matrix, plots the matrix with color scaling and absolute numbers, and adds the percentages and numbers as text on top of the plotted matrix for clarity.

Uploaded by

c_mc2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views2 pages

Sample

This document contains a function called plotConfMat that plots confusion matrices. It takes in a confusion matrix and optional labels as inputs. It calculates accuracy percentages from the confusion matrix, plots the matrix with color scaling and absolute numbers, and adds the percentages and numbers as text on top of the plotted matrix for clarity.

Uploaded by

c_mc2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

confmat = magic(3); % sample data

% plotting
plotConfMat(confmat, {'Dog', 'Cat', 'Horse'});

function plotConfMat(varargin)
%PLOTCONFMAT plots the confusion matrix with colorscale, absolute numbers
% and precision normalized percentages
%
% usage:
% PLOTCONFMAT(confmat) plots the confmat with integers 1 to n as class labels
% PLOTCONFMAT(confmat, labels) plots the confmat with the specified labels
%
% Vahe Tshitoyan
% 20/08/2017
%
% Arguments
% confmat: a square confusion matrix
% labels (optional): vector of class labels

% number of arguments
switch (nargin)
case 0
confmat = 1;
labels = {'1'};
case 1
confmat = varargin{1};
labels = 1:size(confmat, 1);
otherwise
confmat = varargin{1};
labels = varargin{2};
end

confmat(isnan(confmat))=0; % in case there are NaN elements


numlabels = size(confmat, 1); % number of labels

% calculate the percentage accuracies


confpercent = 100*confmat./repmat(sum(confmat, 1),numlabels,1);

% plotting the colors


imagesc(confpercent);
title(sprintf('Accuracy: %.2f%%', 100*trace(confmat)/sum(confmat(:))));
ylabel('Output Class'); xlabel('Target Class');

% set the colormap


colormap(flipud(gray));

% Create strings from the matrix values and remove spaces


textStrings = num2str([confpercent(:), confmat(:)], '%.1f%%\n%d\n');
textStrings = strtrim(cellstr(textStrings));

% Create x and y coordinates for the strings and plot them


[x,y] = meshgrid(1:numlabels);
hStrings = text(x(:),y(:),textStrings(:), ...
'HorizontalAlignment','center');

% Get the middle value of the color range


midValue = mean(get(gca,'CLim'));

% Choose white or black for the text color of the strings so


% they can be easily seen over the background color
textColors = repmat(confpercent(:) > midValue,1,3);
set(hStrings,{'Color'},num2cell(textColors,2));

% Setting the axis labels


set(gca,'XTick',1:numlabels,...
'XTickLabel',labels,...
'YTick',1:numlabels,...
'YTickLabel',labels,...
'TickLength',[0 0]);
end

You might also like