Leave One Out Cross Validation with Nearest Neighbor Classifier > > @param FeatureMatrix: features (dimension iNumFeatures x iNumObservations) > @param ClassIdx: vector with class indices (length iNumObservations, starting from 0) > > @retval Acc overall accuracy after Cross-Validation ======================================================================
0001 %Leave One Out Cross Validation with Nearest Neighbor Classifier 0002 %> 0003 %> @param FeatureMatrix: features (dimension iNumFeatures x iNumObservations) 0004 %> @param ClassIdx: vector with class indices (length iNumObservations, starting from 0) 0005 %> 0006 %> @retval Acc overall accuracy after Cross-Validation 0007 % ====================================================================== 0008 function [Acc, conf_mat] = ToolLooCrossVal(FeatureMatrix, ClassIdx) 0009 0010 % initialize 0011 TP = 0; 0012 0013 conf_mat = zeros(length(unique(ClassIdx))); 0014 0015 % loop over observations 0016 for o = 1:size(FeatureMatrix, 2) 0017 % remove current observation from 'training set' 0018 v_train = [FeatureMatrix(:, 1:o-1) FeatureMatrix(:, o+1:end)]'; 0019 C_train = [ClassIdx(1:o-1) ClassIdx(:, o+1:end)]'; 0020 0021 % compute result of Nearest Neighbor Classifier given the traindata 0022 res = ToolSimpleKnn(FeatureMatrix(:, o)', v_train, C_train, 1); 0023 0024 conf_mat(ClassIdx(o)+1, res+1) = conf_mat(ClassIdx(o)+1, res+1) + 1; 0025 0026 % if result is correct increment number of true positives 0027 if (res == ClassIdx(o)) 0028 TP = TP+1; 0029 end 0030 end 0031 0032 % compute overall (micro) accuracy 0033 Acc = TP / length(ClassIdx); 0034 end