How can i calculate the percentage of error?

1 view (last 30 days)
I have this code:
%%Loading data
load('wine.data');
% first column stores the wine class according to wine.names file
nClass=max(wine(:,1));
%%Getting the mean of each class for the 13 parameters
meanEachClass=arrayfun(@(x) mean( wine( wine(:,1)==x ,2:end) ), 1:nClass,'UniformOutput',false);
%%Now checking the euclidean distance of a sample
% relative to the mean of each class
nSampleToTest=10;
for i=1:nSampleToTest
% Randomly choosing a sample
sampleNo=randi(size(wine,1));
sample=wine(sampleNo,2:end);
% calculate the Eudlidian distance to each class.
distances=arrayfun(@(x) norm(sample-meanEachClass{x}), 1:nClass, 'UniformOutput',true);
disp(sprintf('Sample #%d',sampleNo))
disp(sprintf('Distance: \n Class 1: %f \n Class 2: %f \n Class 3: %f \n',distances(1),distances(2),distances(3)));
disp(sprintf('Based on distance, Sample seems to belong to class %d\n', find(distances==min(distances))))
disp(sprintf('According to the database, sample belongs to class %d\n',wine(sampleNo,1)))
end
Here is the database: http://archive.ics.uci.edu/ml/machine-learning-databases/wine/ I found that some of the random vectors chosen are belonging to the class 2 for example and the algorithm points to class 1. How can i calculate the percentage of the algorithm precision ? Can you provide me some code?

Accepted Answer

Image Analyst
Image Analyst on 25 Jan 2015
You need to look at what class you think it is and compare it to what class it actually is:
Before the loop:
truePositives = zeros(1, nSampleToTest); % Initialize.
Inside the loop:
[~, myClass] = find(distances==min(distances));
trueClass = wine(sampleNo,1);
if myClass == trueClass
% Accurately determined the class.
truePositives(sampleNo) = 1
end
After the loop:
overallAccuracyPercentage = 100 * sum(truePositives) / nSampleToTest;

More Answers (0)

Community Treasure Hunt

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

Start Hunting!