computes Sequential Forward Feature Selection wrapping a nearest neighbor classifier > > @param V: features (dimension iNumFeatures x iNumObservations) > @param ClassIdx: vector with class indices (length iNumObservations) > @param iNumFeatures2Select: target number of features (optional) > > @retval selFeatureIdx vector with ordered feature indices (length: iNumFeatures2Select) > @retval AccPerSubset accuracy for each subset ======================================================================
0001 %computes Sequential Forward Feature Selection wrapping a nearest neighbor 0002 %classifier 0003 %> 0004 %> @param V: features (dimension iNumFeatures x iNumObservations) 0005 %> @param ClassIdx: vector with class indices (length iNumObservations) 0006 %> @param iNumFeatures2Select: target number of features (optional) 0007 %> 0008 %> @retval selFeatureIdx vector with ordered feature indices (length: iNumFeatures2Select) 0009 %> @retval AccPerSubset accuracy for each subset 0010 % ====================================================================== 0011 function [selFeatureIdx, AccPerSubset] = ToolSeqFeatureSel(V, ClassIdx, iNumFeatures2Select) 0012 0013 iNumFeatures = size(V, 1); 0014 if (nargin < 3) 0015 iNumFeatures2Select = iNumFeatures; 0016 end 0017 0018 % initialize 0019 selFeatureIdx = []; 0020 unselectedFeatures = ones(1, iNumFeatures); 0021 AccPerSubset = zeros(1, iNumFeatures); 0022 0023 for f = 1:iNumFeatures 0024 % accuracy of selected features plus current feature f 0025 acc(f) = ToolLooCrossVal(V(f, :), ClassIdx); 0026 end 0027 [maxacc, maxidx] = max(acc); 0028 selFeatureIdx(1) = maxidx; 0029 unselectedFeatures(maxidx) = 0; 0030 AccPerSubset(1) = maxacc; 0031 0032 % iterate until target number of features is reached 0033 for i = 2:iNumFeatures2Select 0034 acc = zeros(1, iNumFeatures); 0035 0036 % iterate over all features not yet selected 0037 for f = 1:iNumFeatures 0038 if (unselectedFeatures(f) > 0) 0039 % accuracy of selected features plus current feature f 0040 acc(f) = ToolLooCrossVal(... 0041 V([selFeatureIdx(1:(i-1)) f], :), ... 0042 ClassIdx); 0043 else 0044 acc(f) = -1; 0045 continue; 0046 end 0047 end 0048 0049 % identify feature maximizing the accuracy 0050 % move feature from unselected to selected 0051 [maxacc, maxidx] = max(acc); 0052 selFeatureIdx(i) = maxidx; 0053 unselectedFeatures(maxidx) = 0; 0054 AccPerSubset(i) = maxacc; 0055 end 0056 end