用八度音阶绘制混淆矩阵;我如何绘制矩阵本身?

时间:2013-10-16 19:23:56

标签: plot octave confusion-matrix

我正在寻找OCTAVE代码来显示行标题和列标题以及OCTAVE中混淆矩阵的行和打印数据。

我理解的数学;它输出行和列标题以及混淆矩阵的行和打印数据以及在OCTAVE中定位文本我不这样做。

我想要行: “实际”= hdr “坏” “好”

和列: “预测”= hdr “坏” “好”

然后我会在底部输出以下图例。

然后我会计算并输出: 对角线的总和/表的总和 “误报”误报/第1行总和 “假阴性”假阴性/第2行总和 “召回”真正的积极因素/第二列的总和 “精确/”真正的正面/第二排的总和

感谢您的协助,

克里斯

1 个答案:

答案 0 :(得分:1)

我为 MATLAB 找到了 this basic solution,做了一些更改以使其与 Octave 兼容,并添加了蓝色阴影,如 scikit-learn 混淆图。这是我的解决方案:https://github.com/gutierrezps/plotConfMat

Confusion plot using plotConfMat, by gutierrezps

以下是源代码:

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, Gutierrez PS
%   19-march-2021
%
%   Arguments
%   confmat (required): a square confusion matrix
%   labels:     vector of class labels
%   fontsize:   to be used on plot texts (default is 14)
%   grayscale:  if true, use grayscale colors (default is false, blue colors)
%
% Source: https://github.com/gutierrezps/plotConfMat

% default arguments
fontsize = 14;
grayscale = false;

% number of arguments
switch (nargin)
    case 0
        confmat = 1;
        labels = {'1'};
    case 1
        confmat = varargin{1};
        labels = 1:size(confmat, 1);
    case 2
        confmat = varargin{1};
        labels = varargin{2};
    case 3
        confmat = varargin{1};
        labels = varargin{2};
        fontsize = varargin{3};
    otherwise
        confmat = varargin{1};
        labels = varargin{2};
        fontsize = varargin{3};
        grayscale = varargin{4} == true;
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(gca, 'FontSize', fontsize);

% set the colormap
if grayscale
    colormap(flipud(gray));
else
    % scikit-learn confusion matrix colors (dark-blue, blue, white)
    confColors = [
        0.03 0.19 0.42;     % 100%
        0.29 0.60 0.79;     % 60%
        1.00 1.00 1.00      % 0%
    ];

    confColorMap = zeros(64, 3);
    colorPts = int8([0.4 0.6] .* 64);

    for i = 1:2
        colors = zeros(colorPts(i), 3);
        for j = 1:3
            colors(:, j) = linspace(confColors(i, j), confColors(i+1, j), colorPts(i))';
        end
        if i == 1
            confColorMap(1:colorPts(1), :) = colors;
        else
            confColorMap(colorPts(1)+1:64, :) = colors;
        end
    end

    colormap(flipud(confColorMap));
end

% Create strings from the matrix values and remove spaces
textStrings = num2str([confpercent(:), confmat(:)], '%.1f%% (%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', 'FontSize', fontsize);

% 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 = double(repmat(confpercent(:) > midValue,1,3));
for i = 1:length(hStrings)
    set(hStrings(i),'Color', textColors(i,:));
end

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