Matlab draw confusion matrix

    Recently, I need to use the confusion matrix in my work, so I have a brief understanding of the confusion matrix. This blog is mainly written with reference to the blog https://blog.csdn.net/xuyingjie125/article/details/78417760, and I would like to express my thanks to the original author.

        Let’s first look at what a confusion matrix is. In artificial intelligence, confusion matrix is ​​a visualization tool, especially for supervised learning. In unsupervised learning, it is generally called matching matrix. In the image accuracy evaluation, it is mainly used to compare the classification results and the actual measured values, and the accuracy of the classification results can be displayed in a confusion matrix. The confusion matrix is ​​computed by comparing the location and classification of each measured pixel with the corresponding location and classification image in the classified image. Each column of the confusion matrix represents the predicted category, and the total number of each column represents the number of data predicted to be that category; each row represents the true attribution category of the data, and the total number of data in each row represents the number of data instances of that category. The numbers in each column represent the number of real data predicted for that class.

        Next I briefly comment on the code.

function confusion_matrix1(act1, det1)
%act1 is the real label, det1 is the predicted label
% input is a row vector
%https://blog.csdn.net/xuyingjie125/article/details/78417760
[mat, order] = confusionmat(act1, det1);
%confusionmat is used to build the confusion matrix, mat returns the confusion matrix, and order returns the class where each variable is located
%mat(i,j) indicates the number of class i in the training set that is assigned to class j in the test set
k = max(order); %k is the number of categories

imagesc(mat); The %imagesc function converts the element values ​​of the matrix mat into different colors according to the size, and dyes the corresponding position of the coordinate axis with this color
colormap(flipud(gray)); % convert to grayscale
The %colormap function is a function used to set and get the current colormap
%flipud is a matrix inversion command, and gray is a built-in matrix of matlab
%gray() will return an M×3 matrix
title('confusion matrix');
textStrings = num2str (mat (:), '% 0.02f');
%num2str(x, precision) converts the array x into a string representation, precision is the precision
textStrings = strtrim (cellstr (textStrings));
%cellstr is to convert the character array to cell type
% Trim spaces, tabs, and carriage returns at the beginning and end of a string

%meshgrid is a function in MATLAB for generating grid sampling points
[x,y] = meshgrid(1:k);
hStrings = text (x (:), y (:), textStrings (:), 'HorizontalAlignment', 'center');
The %text function indicates that the text string is positioned at the position specified by the coordinate axis, and the specified attribute is set
%HorizontalAlignment indicates the horizontal alignment of the text, and center indicates the middle alignment of the text frame
midValue = mean(get(gca,'CLim'));
%get(gca,'CLim') gets the current axis handle and sets its CLim(ColorLimit) value to make different graphs use the same color scale
The %mean function returns the mean value, so that midValue obtains the mean value of the graph color scale 0.5000
textColors = repmat(mat(:) > midValue,1,3);
% Copy the matrix [mat(:)>midValue] to a vector of 1×3 blocks (the color value must be a numeric vector containing 3 elements)
% Take the matrix [mat(:)>midValue] as the element of the matrix textColors
%B=repmat(A,M,N) This is used when dealing with large matrices and the contents are repeated. Its function is to stack the contents of A in the (M×N) matrix B,
The size of the %B matrix is ​​determined by the content of the M×N and A matrix,
% If A is a 3×4×5 matrix, then matrix B becomes 6×12×5

set(hStrings,{'Color'},num2cell(textColors,2));
%n=1 means convert all rows of each column to cells, n=2 means convert all columns of each row to cells, and convert the structure array to a heterogeneous array
% Then set it to hStrings after deduplication

% Change the category labels according to your own category requirements
set(gca,'XTick',1:9,'XTickLabel',{'1','2','3','4','5','6','7','8','9'},'YTick''1:9','YTickLabel',{'1','2','3','4','5','6','7','8','9'});



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324606917&siteId=291194637