-
Notifications
You must be signed in to change notification settings - Fork 17
/
ftSel_sfs.m
59 lines (49 loc) · 1.61 KB
/
ftSel_sfs.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function [ftSubset,ftScore] = ftSel_sfs(ft,target,param)
%Feature selection using sequential forward selection
% FT : sample matrix, each row is a sample.
% TARGET : a column vector, depend on the wrapper prediction algorithm
% used.
% PARAM: struct of parameters. The beginning part of this code (before
% defParam) explains each parameter, and also sets the default parameters.
% You can change parameter p to x by setting PARAM.p = x. For parameters
% that are not set, default values will be used.
% Return:
% FTSUBSET: ordered selected features (most important first)
% FTSCORE: not returned.
%
% Ke YAN, 2016, Tsinghua Univ. http://yanke23.com, [email protected]
[nSmp,nFt] = size(ft);
nCv = 5; % number of cross-validation when evaluating features
nSel = min(10,nFt); % number of features to select
getErrRateFunc = @test_getErrRate; % user-defined function for computing the score
% of each chromosome
showVerboseInfo = 1;
defParam
cvObj = cvpartition(target,'k',nCv);
userdata.cvObj = cvObj;
userdata.ft = ft;
userdata.target = target;
selFt = [];
perfPerFt = zeros(1,nSel);
perfHistory = zeros(nSel,nFt);
selTimeCost = zeros(1,nSel);
if showVerboseInfo, fprintf('current error: '); end
for sfIdx = 1:nSel
err1 = inf(1,nFt);
tic
for ftIdx = 1:nFt
if any(ftIdx==selFt), continue; end
curFt = [selFt ftIdx];
x = false(1,nFt);
x(curFt) = true;
err1(ftIdx) = getErrRateFunc(x,userdata);
end
selTimeCost(sfIdx) = toc;
perfHistory(sfIdx,:) = err1;
[perfPerFt(sfIdx),selFt(sfIdx)] = min(err1);
if showVerboseInfo,fprintf('%.4f,',perfPerFt(sfIdx)); end
end
fprintf('\n')
ftSubset = selFt;
ftScore = [];
end