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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With