MATLAB实现混淆矩阵

本文介绍了如何在MATLAB中实现混淆矩阵,提供了MATLAB代码示例和调用方法。文章通过详细步骤解释了compute_confusion_matrix函数的使用,并分享了代码资源,包括百度网盘和GitHub链接。同时,针对可能出现的错误,如rotateXLabels.m的兼容性问题,给出了修改建议。
摘要由CSDN通过智能技术生成


参考博客1: https://blog.csdn.net/sherry_gp/article/details/50560003
感谢博主大大 sherry_gp
参考博客2: https://blog.csdn.net/stu_lavender/article/details/79615474
感谢博主大大: stu_lavender
参考博客3: https://www.csdn.net/gather_25/MtzaMg3sNTE0LWJsb2cO0O0O.html
感谢博主大大

0.太长不看版

这是一份demo,直接就可以运行,链接:https://pan.baidu.com/s/1NYMgBkog-dBZ4RGmGHRwbg 提取码:pyit
问一下大家,网盘失效了吗,百度网盘怎么看自己分享的东西失效了没?我这边显示的是永久有效。。。
可供参考,这个是GitHub上的代码,我只是调通了搞懂调用方式而已。
demo里的东西(其实就是网盘里的东西)因为听说网盘失效了。。。然而我不会更新网盘。。。有没有人能在评论区告诉我怎么更新。。。

demo.m文件

clc
clear all
% 假设predict_label即预测标签是下面这个1z25的向量说明第15张被预测为类16-10张被预测为类2
predict_label=[1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5];
% predict_label=double(predict_label);%其实这一步比较多余,本来就是double了
name_class={
   '1','2','3','4','5'};


% actual_label=[1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 ];
num_in_class=[4 6 5 5 5];%num_in_class就是实际标签每一类的数目
[confusion_matrix]=compute_confusion_matrix(predict_label,num_in_class,name_class);

ps:如果实际中得到的是actual_label,怎么从actual_label得到对应的num_in_class呢?下面是一种方法

actual_label=[1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 ];
t=tabulate(actual_label);
num_in_class=t(:,2)';

compute_confusion_matrix.m文件

% 预测标签,每一类的数目,类别数目
function [confusion_matrix]=compute_confusion_matrix(predict_label,num_in_class,name_class)
% predict_label为一维行向量
% num_in_class代表每一类的个数
% name_class代表类名
num_class=length(num_in_class);
num_in_class=[0 num_in_class];

confusion_matrix=size(num_class,num_class);
 
for ci=1:num_class
    for cj=1:num_class
        summer=0;%统计对应标签个数  
        c_start=sum(num_in_class(1:ci))+1;
        c_end=sum(num_in_class(1:ci+1));
        summer=size(find(predict_label(c_start:c_end)==cj),2);  % 统计对应标签个数,注意此处的predict_label可能是数值组成的向量,不是字符串组成的向量
%         confusion_matrix(ci,cj)=summer/num_in_class(ci+1);%这个是显示正确率,
        confusion_matrix(ci,cj)=summer;%这个是显示图片有多少张,
    end
end
 
draw_cm(confusion_matrix,name_class,num_class);
 
end

draw_cm.m文件

function draw_cm(mat,tick,num_class)
%%
%  Matlab code for visualization of confusion matrix;
%  Parameters:mat: confusion matrix;
%              tick: name of each class, e.g. 'class_1' 'class_2'...
%              num_class: number of class
%
%  Author: Page( 丕子)  
%           Blog: www.shamoxia.com;  
%           QQ:379115886;  
%           Email: peegeelee@gmail.com
%%
imagesc(1:num_class,1:num_class,mat);            %# in color
colormap(flipud(gray));  %# for gray; black for large value.

textStrings = num2str(mat(:),'%0.2f');  
textStrings = strtrim(cellstr(textStrings)); 
[x,y] = meshgrid(1:num_class); 
hStrings = text(x(:),y(:),textStrings(:), 'HorizontalAlignment','center','FontSize',14);
midValue = mean(get(gca,'CLim')); 
textColors = repmat(mat(:) > midValue,1,3); 
set(hStrings,{
   'Color'},num2cell(textColors,2));  %# Change the text colors

set(gca,'xticklabel',tick,'XAxisLocation','top');
set(gca, 'XTick', 1:num_class, 'YTick', 1:num_class);
set(gca,'yticklabel',tick);
set(gca,'FontSize',14,'Fontname', 'Helvetica');
rotateXLabels(gca, 315 );% rotate the x tick




rotateXLabels.m文件

function hh = rotateXLabels( ax, angle, varargin )
%rotateXLabels: rotate any xticklabels
%
%   hh = rotateXLabels(ax,angle) rotates all XLabels on axes AX by an angle
%   ANGLE (in degrees). Handles to the resulting text objects are returned
%   in HH.
%
%   hh = rotateXLabels(ax,angle,param,value,...) also allows one or more
%   optional parameters to be specified. Possible parameters are:
%     'MaxStringLength'   The maximum length of label to show (default inf)
%
%   Examples:
%   >> bar( hsv(5)+0.05 )
%   >> days = {
   'Monday','Tuesday','Wednesday','Thursday','Friday'};
%
  • 43
    点赞
  • 194
    收藏
    觉得还不错? 一键收藏
  • 23
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值