annotate phase2/rfFeatureSelection.m @ 3:3ff93b42d454

create random forest feature selection function
author DaveM
date Thu, 09 Feb 2017 17:54:49 +0000
parents
children 7ec9bd8df111
rev   line source
DaveM@3 1 function features = rfFeatureSelection(data, labels, numFeatures, iterMethod, numTrees)
DaveM@3 2 %% rfFeatureSelection(data, labels, numFeatures, iterMethod, numTrees)
DaveM@3 3 %
DaveM@3 4 %
DaveM@3 5 %
DaveM@3 6 %
DaveM@3 7
DaveM@3 8 if(length(labels) ~= size(data,1))
DaveM@3 9 error('labels and data do not match up');
DaveM@3 10 end
DaveM@3 11
DaveM@3 12 if(nargin < 2)
DaveM@3 13 error('must pass data and labels into function')
DaveM@3 14 end
DaveM@3 15 if(nargin < 3)
DaveM@3 16 numFeatures = 5;
DaveM@3 17 end
DaveM@3 18 if(nargin < 4)
DaveM@3 19 iterMethod = 'onePass';
DaveM@3 20 end
DaveM@3 21 if(nargin < 5)
DaveM@3 22 numTrees = 200;
DaveM@3 23 end
DaveM@3 24
DaveM@3 25
DaveM@3 26 options = statset('UseParallel', true);
DaveM@3 27 b = TreeBagger(numTrees, data, labels,'OOBVarImp','On',...
DaveM@3 28 'SampleWithReplacement', 'Off','FBoot', 0.632,'Options', options);
DaveM@3 29 [FI,I] = sort(b.OOBPermutedVarDeltaError,'descend');
DaveM@3 30 features = I;
DaveM@3 31
DaveM@3 32 if(strcmp(iterMethod,'onePass'))
DaveM@3 33 disp('cut')
DaveM@3 34 features = features(1:numFeatures);
DaveM@3 35 elseif(strcmp(iterMethod,'cut10'))
DaveM@3 36 disp('cut10')
DaveM@3 37 cutSize = max(floor(length(features)/10),1);
DaveM@3 38 features = features(1:end-cutSize);
DaveM@3 39 data = data(:,I);
DaveM@3 40 features = rfFeatureSelection(data, labels, numFeatures, iterMethod, numTrees);
DaveM@3 41 elseif(strcmp(iterMethod,'cut5'))
DaveM@3 42 disp('cut5')
DaveM@3 43 cutSize = max(floor(length(features)/20),1);
DaveM@3 44 features = features(1:end-cutSize);
DaveM@3 45 data = data(:,I);
DaveM@3 46 features = rfFeatureSelection(data, labels, numFeatures, iterMethod, numTrees);
DaveM@3 47 end
DaveM@3 48 end