📄 knnclass.m
字号:
function [class,index,dist] = knnclass(tst,X,I,K)% [class,index,dist] = knnclass(tst,X,I,K)%% KNNCLASS is an implementation of K-Nearest Neighbours % classifier. The Euclidean metric is used.%% Input:% tst [DxNtst] Ntst test points of dimension D.% X [D,Ntrn] Ntrn training points of dimension D.% I [1,Ntrn] Ntrn labels of the training points.% K [1x1] number of nearest neighbours.%% Output:% class [1xNtst] class labels for each test point.% index [1xNtst] index of the nearest point from the traing set.% dist [1xNtst] distance from the nearest point from the traing set.%% Statistical Pattern Recognition Toolbox, Vojtech Franc, Vaclav Hlavac% (c) Czech Technical University Prague, http://cmp.felk.cvut.cz% Written Vojtech Franc (diploma thesis)% Modifications% 26-feb-2001 V.Francntst=size(tst,2);ntrn=size(X,2);nclass=max(I)-min(I)+1;class=zeros(1,ntst);index=zeros(1,ntst);dist=zeros(1,ntst);for i=1:ntst, d = diag((X-repmat(tst(:,i),1,ntrn))'*(X-repmat(tst(:,i),1,ntrn))); [d,inx]=sort(d); % sort distances in ascending order knn=I(inx); index(i)=inx(1); % get distance of the nearest point and its index dist(i)=d(1); h=hist(knn(1:K),[1:nclass]); [cnt,class(i)]=max(h);enddist=sqrt(dist); return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -