?? knn_light.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 + -