matlab K最近邻

时间:2014-03-09 16:14:23

标签: matlab classification knn

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

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

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

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

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

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

1 个答案:

答案 0 :(得分: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);

为了节省内存,您可以使用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