📄 inputselectexhaustive.m
字号:
function [bestSelectedInput, bestRecogRate, allSelectedInput, allRecogRate, elapsedTime] = inputSelectExhaustive(DS, inputNum, classifier, param, plotOpt)
% inputSelectExhaustive: Input selection via sequential forward selection using leave-one-out
% Usage: [bestSelectedInput, allSelectedInput, allRecogRate, elapsedTime] = inputSelectExhaustive(DS, inputNum, classifier, param, plotOpt)
%
% Input:
% DS: design set
% inputNum: up to inputNum inputs are selected
% classifier: classifier for input selection
% param: parameters for classifier
% plotOpt: 0 for not plotting (default: 1)
% Output:
% bestSelectedInput: overall selected input index
% bestRecogRate: recognition rate based on the final selected input
% allSelectedInput: all selected input during the process
% allRecogRate: all recognition rate
% elapseTime: elapsed time
% Roger Jang, 19971227, 20041102
if nargin<1, selfdemo; return; end
[dim, dataNum]=size(DS.input);
if nargin<2, inputNum=dim; end
if nargin<3, classifier='knnrLoo'; end
if nargin<4, param=1; end
if nargin<5, plotOpt=1; end
DS.output=classConvert(DS.output); % Convert the output to be intergers from 1 to classNum
inputName=DS.inputName;
t0=clock;
% Construct all input indices for possible models
allSelectedInput={};
for i=1:inputNum
x=combine(1:dim, i);
thisInputIndex=mat2cell(x, ones(1,size(x,1)), size(x,2));
allSelectedInput={allSelectedInput{:}, thisInputIndex{:}};
end
modelNum=length(allSelectedInput);
fprintf('\nConstruct %d KNN models, each with up to %d inputs selected from %d candidates...\n', modelNum, inputNum, dim);
for i=1:modelNum
DS2=DS;
DS2.input=DS.input(allSelectedInput{i}, :);
allRecogRate(i) = feval(classifier, DS2, param);
fprintf('modelIndex %d/%d: %s --> Recognition rate = %f%%\n', i, modelNum, inputNameList(allSelectedInput{i}, inputName), allRecogRate(i)*100);
end
[bestRecogRate, b] = max(allRecogRate);
bestSelectedInput = allSelectedInput{b};
fprintf('\nOverall max recognition rate = %.1f%%.\n', bestRecogRate*100);
fprintf('Overall selected inputs: %s\n', inputNameList(bestSelectedInput, inputName));
elapsedTime=etime(clock, t0);
if plotOpt
inputSelectPlot(allRecogRate*100, allSelectedInput, inputName, mfilename);
end
% ====== Subfunction
function out = combine(obj, n)
% out = combine(obj, n) returns combinations of obj with n distinct
% elements.
% For instance: combine([1 2 3 4 5], 2) or combine('abcde', 3).
% Roger Jang, Sept-21-1996
if n>length(obj)
out=[];
return;
end
if n==1
out = obj(:);
return;
end
if n==length(obj)
out = obj(:)';
return;
end
out = [];
for i = 1:length(obj)-1,
first = obj(i);
tail = obj(i+1:end);
tail_combinat = combine(tail, n-1);
loop_out = [first*ones(size(tail_combinat,1), 1), tail_combinat];
out = [out; loop_out];
end
% ====== Self demo
function selfdemo
DS=prData('random6');
feval(mfilename, DS);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -