diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/phase2/rfFeatureSelection.m	Thu Feb 09 17:54:49 2017 +0000
@@ -0,0 +1,48 @@
+function features = rfFeatureSelection(data, labels, numFeatures, iterMethod, numTrees)
+%% rfFeatureSelection(data, labels, numFeatures, iterMethod, numTrees)
+%
+%
+%
+%
+
+if(length(labels) ~= size(data,1))
+    error('labels and data do not match up');
+end
+
+if(nargin < 2)
+    error('must pass data and labels into function')
+end
+if(nargin < 3)
+    numFeatures = 5;
+end
+if(nargin < 4)
+    iterMethod = 'onePass';
+end
+if(nargin < 5)
+    numTrees = 200;
+end
+
+
+options = statset('UseParallel', true);
+b = TreeBagger(numTrees, data, labels,'OOBVarImp','On',...
+    'SampleWithReplacement', 'Off','FBoot', 0.632,'Options', options);
+[FI,I] = sort(b.OOBPermutedVarDeltaError,'descend'); 
+features = I;
+
+if(strcmp(iterMethod,'onePass'))
+    disp('cut')
+    features = features(1:numFeatures);
+elseif(strcmp(iterMethod,'cut10'))
+    disp('cut10')
+    cutSize = max(floor(length(features)/10),1);
+    features = features(1:end-cutSize);
+    data = data(:,I);
+    features = rfFeatureSelection(data, labels, numFeatures, iterMethod, numTrees);
+elseif(strcmp(iterMethod,'cut5'))
+    disp('cut5')
+    cutSize = max(floor(length(features)/20),1);
+    features = features(1:end-cutSize);
+    data = data(:,I);
+    features = rfFeatureSelection(data, labels, numFeatures, iterMethod, numTrees);
+end
+end
\ No newline at end of file