How to show the accuracy on a confusion matrix

조회 수: 29 (최근 30일)
Nick Vasilakis
Nick Vasilakis 2022년 4월 17일
답변: Chunru 2022년 4월 18일
So I've trained a perceptron with 29 data and I implemented its confusion matrix. Yet I want it to show me the accuracy. So far it only appears as follows:
And this is the code that I used to plot it:
function katataksh= Ask31c (w1,w2,b,x_new,y_new)
W=[w1 w2 b]; %ορίζω τον πίνακα W να παίρνει τις τιμές των w1,w2 και b
accuracy=sum(y_new == katataksh,'all')/numel(katataksh);
Note that there's more code that is implemented, but I think it's not necessary to write it here, since I only want to find the accuracy of the confusion matrix.
Thank you in advance!

답변 (1개)

Chunru 2022년 4월 18일
You nee to add column and row summaries. Check out the following example from doc.
load fisheriris
X = meas;
Y = species;
Mdl = fitcknn(X,Y,'NumNeighbors',5,'Standardize',1);
predictedY = resubPredict(Mdl);
cm = confusionchart(Y,predictedY);
% The confusion matrix displays the total number of observations in each cell.
% The rows of the confusion matrix correspond to the true class, and the columns
% correspond to the predicted class. Diagonal and off-diagonal cells correspond
% to correctly and incorrectly classified observations, respectively.
% By default, confusionchart sorts the classes into their natural order as defined
% by sort. In this example, the class labels are character vectors, so confusionchart
% sorts the classes alphabetically. Use sortClasses to sort the classes by a specified
% order or by the confusion matrix values.
% The NormalizedValues property contains the values of the confusion matrix. Display
% these values using dot notation.
ans = 3×3
50 0 0 0 47 3 0 4 46
% Modify the appearance and behavior of the confusion matrix chart by changing property
% values. Add a title.
cm.Title = 'Iris Flower Classification Using KNN';
% Add column and row summaries.
cm.RowSummary = 'row-normalized';
cm.ColumnSummary = 'column-normalized';




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by