混淆矩阵的MATLAB实现

本文介绍了混淆矩阵的概念及其在图像精度评价中的应用,并提供了一个Matlab实现的示例代码,用于展示如何创建并可视化一个混淆矩阵。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

混淆矩阵的每一列代表了预测类别,每一列的总数表示预测为该类别的数据的数目;每一行代表了数据的真实归属类别,每一行的数据总数表示该类别的数据实例的数目。在图像精度评价中,主要用于比较分类结果和实际测得值,可以把分类结果的精度显示在一个混淆矩阵里面。

下面是混淆矩阵的Matlab实现示例,可直接复制粘贴运行:

% function confusion_matrix(actual,detected)
%  [mat,order] = confusionmat(actual,detected);
 
 mat = rand(5);           %# A 5-by-5 matrix of random values from 0 to 1
mat(3,3) = 0;            %# To illustrate
mat(5,2) = 0;            %# To illustrate
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('confusion matrix of recognition');
textStrings = num2str(mat(:),'%0.02f');  %# Create strings from the matrix values
textStrings = strtrim(cellstr(textStrings));  %# Remove any space padding
 
%% ## New code: ###
%idx = find(strcmp(textStrings(:), '0.00'));
%textStrings(idx) = {'   '};
%% ################
 
[x,y] = meshgrid(1:5);   %# Create x and y coordinates for the strings
hStrings = text(x(:),y(:),textStrings(:),...      %# Plot the strings
                'HorizontalAlignment','center');
% hStrings = text(x,y,textStrings(:),...      %# Plot the strings
%                 '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
set(hStrings,{'Color'},num2cell(textColors,2));  %# Change the text colors
 
set(gca,'XTick',1:5,...                         %# Change the axes tick marks
        'XTickLabel',{'Bob','Hyt','Maple','Study','Zm'},...  %#   and tick labels
        'YTick',1:5,...
        'YTickLabel',{'Bob','Hyt','Maple','Study','Zm'},...
        'TickLength',[0 0]);
下面是运行结果:












参考:

MATLAB中的混淆矩阵的实现


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值