%% Plot Confusion Matrix Using One-of-N Labels
% Load sample data using the cancer_dataset function. XTrain is a 9-by-699 matrix defining nine attributes of 699 biopsies. YTrain is a 2-by-699 matrix where each column indicates the correct category of the corresponding observation. Each column of YTrain has one element that equals one in either the first or second row, corresponding to the cancer being benign or malignant, respectively. For more information on this dataset, type help cancer_dataset at the command line.

rng default
[XTrain,YTrain] = cancer_dataset;
YTrain(:,1:10)

%
% Create a pattern recognition network and train it using the sample data.

net = patternnet(10);
net = train(net,XTrain,YTrain);

%
% Estimate the cancer status using the trained network. Each column of the matrix YPredicted contains the predicted probabilities of each observation belonging to class 1 and class 2, respectively.

YPredicted = net(XTrain);
YPredicted(:,1:10)

%
% Plot the confusion matrix. To create the plot, plotconfusion labels each observation according to the highest class probability.

plotconfusion(YTrain,YPredicted)

% In this figure, the first two diagonal cells show the number and percentage of correct classifications by the trained network. For example, 446 biopsies are correctly classified as benign. This corresponds to 63.8% of all 699 biopsies. Similarly, 236 cases are correctly classified as malignant. This corresponds to 33.8% of all biopsies.
% 5 of the malignant biopsies are incorrectly classified as benign and this corresponds to 0.7% of all 699 biopsies in the data. Similarly, 12 of the benign biopsies are incorrectly classified as malignant and this corresponds to 1.7% of all data.
% Out of 451 benign predictions, 98.9% are correct and 1.1% are wrong. Out of 248 malignant predictions, 95.2% are correct and 4.8% are wrong. Out of 458 benign cases, 97.4% are correctly predicted as benign and 2.6% are predicted as malignant. Out of 241 malignant cases, 97.9% are correctly classified as malignant and 2.1% are classified as benign.
% Overall, 97.6% of the predictions are correct and 2.4% are wrong.
% Copyright 2012 The MathWorks, Inc.