I require matlab code for knn classifier as I am not able to get how to design the same.

2 views (last 30 days)
Matlab code for knn classifier
Thanks in advance

Answers (1)

Star Strider
Star Strider on 30 Mar 2015
The Bioinformatics Toolbox has the knnclassify function.
However the k-nearest neighbors algorithm is relatively easy to write, especially in MATLAB. It essentially involves defining a set of vectors that are known to be of specific classes (and representative of those classes), and comparing each of them to all the data vectors. (The easiest way to do this is to take the Euclidean distance.) This results in a vector of distances for each data vector to each ‘known’ vector. The shortest distance defines the class the data vector belongs to.
For example:
ClassVectors = randi(10, 3, 5); % Class Vectors (3x5)
DataVectors = randi(10, 15, 5);
for k1 = 1:size(ClassVectors,1)
for k2 = 1:size(DataVectors,1)
d(k2,k1) = sqrt(sum((ClassVectors(k1,:)-DataVectors(k2,:)).^2));
end
end
[~,ClassMember] = min(d, [], 2);
Here, the ‘ClassMember’ vector are the classes to which each member of the ‘DataVectors’ are assigned. You can easily adapt this code to your problem.
  2 Comments
Image Analyst
Image Analyst on 30 Mar 2015
min() looks at all the distances. To use just the "k" closest ones, I believe you'd use sort(d, 'Ascend') to find the k closest ones and then take the majority class from those k closest reference points. Like Star said - it's an easy adaptation.
Star Strider
Star Strider on 30 Mar 2015
In my example code, ‘d’ is a (15x3) matrix of the distances, corresponding to the distances from each member of ‘ClassVectors’ (columns) to each member of ‘DataVectors’ (rows). Taking min across each row produces an index vector that is a (15x1) ‘ClassMember’ vector matching the row vector assignments in ‘DataVectors’. I probably should have clarified that initially.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!