matlab K最近邻

CSa*_*awy 5 matlab classification knn

我是matlab的新手.我想实现KNN算法.我试图阅读fitcknn分类器,但我无法得到它.我有矩阵x有4个输入向量(每个向量有3个特征)

     1     2     3
     5    19    20
     1     2     4
     8    19    21
Run Code Online (Sandbox Code Playgroud)

我想得出一个输出矩阵Y,它给出了输入矩阵的每个向量的最近邻居(按顺序).例如:在这种情况下,y将是

      3     2     4
      4     3     1
      1     2     4
      2     3     1
Run Code Online (Sandbox Code Playgroud)

说明:矩阵Y的第一行表示与向量1最接近的向量是:向量3,然后是向量2,然后是向量4.

是否有库进行此分类(使用余弦距离作为相似函数)?谢谢.

Lui*_*ndo 6

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);
Run Code Online (Sandbox Code Playgroud)

为了节省内存,您可以使用以下代码pdist2来处理块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
Run Code Online (Sandbox Code Playgroud)