Documentation |
Confusion matrix
C = confusionmat(group,grouphat)
C = confusionmat(group,grouphat,'order',grouporder)
[C,order] = confusionmat(...)
C = confusionmat(group,grouphat) returns the confusion matrix C determined by the known and predicted groups in group and grouphat, respectively. group and grouphat are grouping variables with the same number of observations, as described in Grouping Variables. Input vectors must be of the same type. C is a square matrix with size equal to the total number of distinct elements in group and grouphat. C(i,j) is a count of observations known to be in group i but predicted to be in group j. Group indices and their order are the same for the rows and columns of C, computed by grp2idx using grp2idx(group;grouphat). NaN, empty, or 'undefined' groups are not counted.
C = confusionmat(group,grouphat,'order',grouporder) uses grouporder to order the rows and columns of C. grouporder is a grouping variable containing all of the distinct elements in group and grouphat. If grouporder contains elements that are not in group or grouphat, the corresponding entries in C will be 0.
[C,order] = confusionmat(...) also returns the order of the rows and columns of C in a variable order the same type as group and grouphat.
Display the confusion matrix for data with two misclassifications and one missing classification:
g1 = [1 1 2 2 3 3]'; % Known groups g2 = [1 1 2 3 4 NaN]'; % Predicted groups [C,order] = confusionmat(g1,g2) C = 2 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 order = 1 2 3 4
Randomize the measurements and groups in Fisher's iris data:
load fisheriris numObs = length(species); p = randperm(numObs); meas = meas(p,:); species = species(p);
Use classify to classify measurements in the second half of the data, using the first half of the data for training:
half = floor(numObs/2); training = meas(1:half,:); trainingSpecies = species(1:half); sample = meas(half+1:end,:); grouphat = classify(sample,training,trainingSpecies);
Display the confusion matrix for the resulting classification:
group = species(half+1:end); [C,order] = confusionmat(group,grouphat) C = 22 0 0 2 22 0 0 0 29 order = 'virginica' 'versicolor' 'setosa'