gpt4 book ai didi

以 Octave 为单位绘制混淆矩阵;我如何绘制矩阵本身?

转载 作者:行者123 更新时间:2023-12-04 18:08:58 27 4
gpt4 key购买 nike

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

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

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

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

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

然后我会计算并输出:
对角线之和/表格之和
“误报”误报/第一行的总和
“假阴性”假阴性/第二行的总和
“召回”真阳性/第二列的总和
“精度/”真阳性/第 2 行的总和

感谢你的协助,

克里斯

最佳答案

我找到了 this basic solution对于 MATLAB,进行了一些更改以使其与 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

关于以 Octave 为单位绘制混淆矩阵;我如何绘制矩阵本身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19411982/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com