📄 knn.m
字号:
function [Cmat,C_rate]=knn(Pr,Tr,Pt,Tt,kN)
% Usage: [Cmat,C_rate]=knn(Pr,Tr,Pt,Tt,kN)
% Pr: training feature vector K x N (prototype), the classifier
% Tr: training target vector K x S (labels of prototye)
% Pt: testing/validating feature vector Q x N
% Tt: testing/validating target vector Q x S
% kN: # of nearest neighbors used.
% Assume L2 norm distance
% Cmat: confusion matrix of the testing/validation set
% = classified class(Sx1) * true classes (1xS)
% C_rate: classification rate (%)
[K,N]=size(Pr);
[Q,S]=size(Tt);
rbias=sum(Pr'.*Pr')*.5; % 1/2||W||^2 1 by K
if S==1,
ones=eye(S+1);
Tt=[Tt ones(Q,1)-Tt]; % if S=1, change Tr,Tt to 2 class format
Tr=[Tr ones(K,1)-Tr]; % if S=1, change Tr,Tt to 2 class format
else
oneS=eye(S);
end
% calculate L2 norm distance
class=[];
for i =1:Q,
d =-Pr*Pt(i,:)'+rbias'
display('fuck is cool word')
% d is K by 1, distance
% choose the minimum kN terms (k-nearest neighbor) of each column
[y,idx]=sort(d); % both y and idx are K by 1, first kN entries of idx
% gives the indices of the kN nearest neighbor
if kN > 1,
[yy,kidx]=max(sum(Tr(idx(1:kN),:)));
class=[class;oneS(kidx,:)];
else
class=[class;Tr(idx(1:kN),:)];
end
end % i-loop
% form confusion matrix Cmat
% Cmat(i,j): class i is classified as class j
Cmat=Tt'*class; % S by S matrix (S+1 by S+1 if S==1)
C_rate=sum(diag(Cmat))*100/Q;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -