MATLAB: How can i implement the NN algorithm

classificationdatabaseeuclidean distancenearest neighborwine

I have a database http://archive.ics.uci.edu/ml/machine-learning-databases/wine/
There are 3 classes in my database, 1,2 and 3. 178 total instances. I want to be able to choose a single test vector and calculate the euclidian distance ( example: sqrt((x2-x1)^2+(y2-y1)^2) ) between the mean vector from each class and this test vector. Then i aught to compare each calculated distance and choose de smallest. The smallest distance will point to the class where our test vector belongs.
Is there anyone that can give me a piece of advice or some link to a NP example usage?

Best Answer

Some code like this would do the job:
%%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
Sample output of the above code:
Sample #171
Distance:
Class 1: 605.816060
Class 2: 10.319480
Class 3: 119.987114
Based on distance, Sample seems to belong to class 2
According to the database, sample belongs to class 3
Sample #117
Distance:
Class 1: 621.072378
Class 2: 26.007955
Class 3: 135.695649
Based on distance, Sample seems to belong to class 2
According to the database, sample belongs to class 2
Sample #7
Distance:
Class 1: 174.614487
Class 2: 770.521566
Class 3: 660.160087
Based on distance, Sample seems to belong to class 1
According to the database, sample belongs to class 1
Sample #152
Distance:
Class 1: 635.785710
Class 2: 43.956064
Class 3: 150.475079
Based on distance, Sample seems to belong to class 2
According to the database, sample belongs to class 3
Sample #167
Distance:
Class 1: 420.824925
Class 2: 176.469688
Class 3: 66.248364
Based on distance, Sample seems to belong to class 3
According to the database, sample belongs to class 3
Sample #121
Distance:
Class 1: 490.840691
Class 2: 105.514335
Class 3: 8.175599
Based on distance, Sample seems to belong to class 3
According to the database, sample belongs to class 2
Sample #135
Distance:
Class 1: 466.213558
Class 2: 130.910111
Class 3: 25.163417
Based on distance, Sample seems to belong to class 3
According to the database, sample belongs to class 3
Sample #133
Distance:
Class 1: 555.828809
Class 2: 40.964218
Class 3: 69.989176
Based on distance, Sample seems to belong to class 2
According to the database, sample belongs to class 3
Sample #70
Distance:
Class 1: 400.230442
Class 2: 206.399030
Class 3: 102.402864
Based on distance, Sample seems to belong to class 3
According to the database, sample belongs to class 2
Sample #117
Distance:
Class 1: 621.072378
Class 2: 26.007955
Class 3: 135.695649
Based on distance, Sample seems to belong to class 2
According to the database, sample belongs to class 2
Related Question