how to find the accuracy from the predicted labels for test data in Matlab?

1 view (last 30 days)
I am using classification learner app svm generated code for the classification of multiclass dataset.
Now I wanted to test with the unseen dataset for this I am using yfit.
Now I got the predicted labels for the test data. How to find the test accuracy and from the predicted laebls?
Can someone please help me in this.

Answers (1)

Riya
Riya on 26 Mar 2025
Hi Kanuparthi,
You can compute the accuracy by comparing the predicted labels (yfit) with the actual labels (say “yTest).
The accuracy is calculated as the percentage of correctly predicted labels. Below is a sample MATLAB code for the same:
% Assuming yTest contains the actual labels of the test data
correctPredictions = sum(yfit == yTest); % Count correct predictions
totalTestSamples = length(yTest); % Total number of test samples
% Compute accuracy
accuracy = (correctPredictions / totalTestSamples) * 100;
% Display accuracy
fprintf('Test Accuracy: %.2f%%\n', accuracy);
This will give you the classification accuracy of the model on the test dataset. If your labels are categorical, you may need to use categorical(yfit) == categorical(yTest) for comparison.
For further reference, you can refer to the following official MATLAB documentation:
web(fullfile(docroot, 'stats/select-data-and-validation-for-classification-problem.html'))

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!