Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab K Nearest Neighbor

I am super new to matlab. I want to implement the KNN algorithm. I tried to read the fitcknn classifier but I can't get it. I have matrix x that has 4 input vectors (each vector has 3 features)

     1     2     3
     5    19    20
     1     2     4
     8    19    21

I want to get out an output matrix Y that gives me the nearest neighbors (in order) for each vector of the input matrix. For example: y in this case will be

      3     2     4
      4     3     1
      1     2     4
      2     3     1

Explanation: the first row of matrix Y shows that the closest vectors to vector 1 are: vector 3 then vector 2 then vector 4.

Is there a library to do this classification (using the cosine distance as a similarity function)? Thanks.

like image 525
CSawy Avatar asked Mar 09 '14 16:03

CSawy


1 Answers

n = size(x,1);
dist = squareform(pdist(x,'cosine')); %// distance matrix
dist(1:n+1:end) = inf; %// self-distance doesn't count
[~, y] = sort(dist,2);
y = y(:,1:n-1);

To save memory, you can work in chunks using pdist2 instead of pdist:

n = size(x,1);
m = 100; %// chunk size. As large as memory allows. Divisor of n
y = NaN(n,n-1); %// pre-allocate results
for ii = 0:m:size(x,1)-1
    ind = ii+(1:m); %// current chunk: these rows
    dist_chunk = pdist2(x(ind,:),x,'cosine'); %// results for this chunk
    [~, y_chunk] = sort(dist_chunk,2);
    y(ind,:) = y_chunk(:,2:end); %// fill results, except self-distance
end
like image 87
Luis Mendo Avatar answered Oct 23 '22 12:10

Luis Mendo