How to show the accuracy on a confusion matrix

8 visualizzazioni (ultimi 30 giorni)
Nick Vasilakis
Nick Vasilakis il 17 Apr 2022
Risposto: Chunru il 18 Apr 2022
Hello!
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
New_data=x_new;
y_problepsi_new=sign(New_data(:,1)*W(1,1)+New_data(:,2)*W(1,2)+W(1,3));
katataksh=y_problepsi_new;
apodosh=confusionmat(y_new,katataksh);
accuracy=sum(y_new == katataksh,'all')/numel(katataksh);
confusionchart(apodosh);
end
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!

Risposte (1)

Chunru
Chunru il 18 Apr 2022
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.
cm.NormalizedValues
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';

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by