How to split classes of a database in two ?
1 view (last 30 days)
Show older comments
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;
truePositives = zeros(1, nSampleToTest); % Initialize
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)))
[~, myClass] = find(distances==min(distances));
trueClass = wine(sampleNo,1);
if myClass == trueClass
% Accurately determined the class.
truePositives(sampleNo) = 1
end
end
overallAccuracyPercentage = 100 * sum(truePositives) / nSampleToTest;
Here is the database: http://archive.ics.uci.edu/ml/machine-learning-databases/wine/ This code calculates the nearest prototype by using the euclidian distance. The problem is here that my train vectors are represented by the all vectors from database and my test vectors are randomly taken. I want to split in 2 each class and the first half to represent the train vectors and the other the test vectors. For example class 1 has 59 vectors: 30 train vectors and 29 test vectors and so on with the class 2 and 3. Then i want to be able to calculate the percentage of error. Can you provide me some code,please?
0 Comments
Answers (0)
See Also
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!