matlab 混淆矩阵 代码示例

    技术2023-10-14  71

    参考自以下大佬的博客:

    https://blog.csdn.net/zhaomengszu/article/details/56283832

    https://blog.csdn.net/songchaomail/article/details/43834741/

    https://blog.csdn.net/dujiahei/article/details/80789956

    https://blog.csdn.net/xuyingjie125/article/details/78417760

    万分感谢他们提供的 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]); 混淆矩阵图
    Processed: 0.027, SQL: 9