performs knn classification > > @param TestFeatureVector: features for test observation (length iNumFeatures) > @param TrainFeatureMatrix: features for all train observations (dimension iNumFeatures x iNumObservations) > @param TrainClassIndices: audio signal (length iNumObservations) > @param k: number of points taken into account (default = 3) > > @retval class index of the resulting class ======================================================================
0001 %performs knn classification 0002 %> 0003 %> @param TestFeatureVector: features for test observation (length iNumFeatures) 0004 %> @param TrainFeatureMatrix: features for all train observations (dimension iNumFeatures x iNumObservations) 0005 %> @param TrainClassIndices: audio signal (length iNumObservations) 0006 %> @param k: number of points taken into account (default = 3) 0007 %> 0008 %> @retval class index of the resulting class 0009 % ====================================================================== 0010 function [class] = ToolSimpleKnn(TestFeatureVector, TrainFeatureMatrix, TrainClassIndices, k) 0011 0012 % set order to 3 if not set 0013 if (nargin < 4) 0014 k = 3; 0015 end 0016 0017 % compute distances to all training observations 0018 d = computeEucDist_I(TestFeatureVector, TrainFeatureMatrix); 0019 0020 % sort the distances to find closest 0021 [dummy,idx] = sort(d); 0022 0023 % pick the majority of the k closest training observations 0024 % note that for multi-class problems and even k, this needs to be 0025 % refined 0026 class = mode(TrainClassIndices(idx(1:k))); 0027 end 0028 0029 function d = computeEucDist_I(A, B) 0030 d = sqrt(sum(A.^2, 2)*ones(1,size(B,1)) - ... 0031 2*A*B' + ... 0032 ones(size(A,1),1)*sum(B.^2, 2)'); 0033 end