【问题标题】:Plot confusion matrix绘制混淆矩阵
【发布时间】:2016-01-31 19:30:49
【问题描述】:

我想在 MATLAB 中绘制一个混淆矩阵。这是我的代码;

data = rand(3, 3)
imagesc(data) 
colormap(gray) 
colorbar 

当我运行它时,会显示一个带有颜色条的混淆矩阵。但通常,我在 MATLAB 中看到混淆矩阵会给出计数和概率。我怎样才能得到它们?如何更改将显示为 1、2、3 等的类标签?

我想要一个这样的矩阵:

【问题讨论】:

  • 你能展示一个你希望它是什么样子的示例吗?

标签: matlab confusion-matrix


【解决方案1】:

如果没有神经网络工具箱,可以使用plotConfMat。它会为您提供以下结果。

我还在下面包含了一个独立的示例,不需要该函数:

% sample data
confmat = magic(3);
labels = {'Dog', 'Cat', 'Horse'};

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('Confusion Matrix');
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]);

【讨论】:

    【解决方案2】:

    如果你有神经网络工具箱,你可以使用函数plotconfusion。您可以创建副本并对其进行编辑以进一步自定义,例如打印自定义标签。

    【讨论】:

    • 使用plotconfusion后如何自定义标签?
    猜你喜欢
    • 1970-01-01
    • 2016-06-04
    • 2017-10-17
    • 2013-10-25
    • 2020-07-15
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多