混淆矩阵 matlab代码示例

参考自以下大佬的博客:

MATLAB中的混淆矩阵的实现_这孩子谁懂哈的博客-CSDN博客_matlab混淆矩阵

简单理解混淆矩阵_人和无求的博客-CSDN博客_混淆矩阵

简单理解混淆矩阵—Matlab详细代码注解_dujiahei的博客-CSDN博客_matlab混淆矩阵颜色设置

简单理解混淆矩阵—Matlab详细代码注解_斗战神佛的博客-CSDN博客_混淆矩阵matlab

万分感谢他们提供的 confusion_matrix.m ,以及他们的思路,由于他们的实验数据未能获取,因此我从matlab中文论坛找到了有关数据,同时感谢matlab中文论坛。

关于混淆矩阵的概念及介绍我就不在文中详述了,请移步至百度或者以上博客。

main.m: 

clc
clear all
close all
%%
g1 = [3 2 2 3 1 1]';	% Known groups   已知标签
g2 = [4 2 3 0 1 1]';	% Predicted groups  预测标签
confusion_matrix(g1,g2)

 confusion_matrix.m

function confusion_matrix(act1,det1)    %act1为实际值标签,det1为预测值标签
 
[mat,order] = confusionmat(act1,det1);
[m,n] = size(order);        %m为分类的个数
 

imagesc(mat); %# Create a colored plot of the matrix values
colormap(flipud(gray));  %# Change the colormap to gray (so higher values are
 
%#black and lower values are white)
title('混淆矩阵图'); 
textStrings = num2str(mat(:),'%0.02f');       %# Create strings from the matrix values
textStrings = strtrim(cellstr(textStrings));  %# Remove any space padding
 
%% ## New code: ###这里是不显示小矩阵块里的0,用空白代替
% idx = strcmp(textStrings(:), '0.00');
% textStrings(idx) = {'   '};
%% ################
 
%# Create x and y coordinates for the strings %meshgrid是MATLAB中用于生成网格采样点的函数 
[x,y] = meshgrid(1:m);  
hStrings=text(x(:),y(:),textStrings(:),'HorizontalAlignment','center');
midValue = mean(get(gca,'CLim'));  %# Get the middle value of the color range
textColors = repmat(mat(:) > midValue,1,3);  %# Choose white or black for the
                                             %#   text color of the strings so
                                             %#   they can be easily seen over
                                             %#   the background color
%将矩阵[mat(:) >midValue]复制1X3块的矢量(颜色值必须为包含3个元素的数值矢量),即把矩阵[mat(:) > midValue]作为矩阵textColors的元素。
set(hStrings,{'Color'},num2cell(textColors,2));  %# Change the text colors;
%num2cell(textColors, 2)中2 代表「直行被切割」将结构阵列转换成异质阵列 将结构阵列转换成异质阵列;
%然后set去重后放在hStrings;
 
set(gca,'XTick',1:m,...                                    
        'XTickLabel',{order},...  %#   and tick labels  %x轴标签也可自行设定,如本例{'0','1','2','3','4'}
        'YTick',1:m,...                                    %同上
        'YTickLabel',{order},...
        'TickLength',[0 0]);
混淆矩阵图
  • 5
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值