⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 knn_light.m

📁 有关聚类的一些例子
💻 M
字号:
% knn_light: K-Nearest Neighbor classification using euclid distance
%
%  [C] = knn_light(data, proto, protoClass, [K])
%
%  Input and output arguments ([]'s are optional):
%   data  (matrix) of size NxD. N is the number of data (classifiee) vectors, 
%           and D is the dimension of each vector. 
%   proto (matrix) of size PxD. P is the number of prototype vectors,
%           and D is the dimension of each vector. 
%   protoClass (vector) of size Px1 that contains integer class labels of 
%                prototypes. protoClass(j) is the class of jth prototype.
%   [K]   (scalar) the maximum K in K-NN classifier, default is 1
%   C     (vector) of size Nx1: integers indicating the class 
%           decision for data items according to the K-NN rule. 
%           C(i) is the classification for data item i. 
%
% Author : Naotoshi Seo
% Date   : April, 2005

function C = knn_light(data, proto, protoClass, K)
 if nargin < 4 | isempty(K),
   K = 1;
 if size(data,2) ~= data(proto,2)
     error('Dimension of data vectors and prototype vectors do not match.');
 end
 end
 if size(proto,1) ~= size(protoClass,1)
     error('Row # of prototypes and prototype class vector do not match.');
 end
 % Calculate euclidean distances between classifiees and prototypes
 d = eucdist2(data, proto); 

 if K == 1, % sort distances only if K>1
     [mini, proto_index] = min(d, [], 2); % 2 == row
     C = protoClass(proto_index);
 else  
    [sorted, proto_index] = sort(d'); % Nproto x Ndata
     % K closest
     %[mini, proto_index] = min(d, [], 2);
   proto_index = proto_index(1:K,:);
     knn_class = protoClass(proto_index);
     % Find all class labels
     classLabel = unique(protoClass);
     nClass     = length(classLabel);
     for i=1:nClass
        classCounter(i, :) = sum(knn_class == classLabel(i));
     end
     [maxi, winner_label_index] =max(classCounter, [], 1); % 1 == col
     % Future Work: Handle ties somehow
     C = classLabel(winner_label_index);
 end

%%%% Euclidean distance matrix between row vectors in X and Y %%%%%%%
%  Input and output arguments 
%   X: NxDim matrix
%   Y: PxDim matrix
%   d: distance matrix of size NxP 
function d=eucdist2(X,Y);
 U=~isnan(Y); Y(~U)=0;
 V=~isnan(X); X(~V)=0;
 d=abs(X.^2*U'+V*Y'.^2-2*X*Y');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -