|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
$ n/ i# O2 y* f2 @" P
NSGA2算法特征选择MATLAB实现(多目标)
: e6 ?$ x2 F3 q: {6 p
2 y7 b* q7 i: Y8 [' Y
2 G( w- k& s' V3 B0 H' U) I% ?/ K8 @利用nsga2进行进行特征选择其主要思想是:将子集的选择看作是一个搜索寻优问题(wrapper方法),生成不同的组合,对组合进行评价,再与其他的组合进行比较。这样就将子集的选择看作是一个是一个优化问题。2 Q$ Z9 M; w* D8 B) G7 j8 e4 g
. E! c, P$ E5 Q3 Y7 _
需要优化的两个目标为特征数和精度。& P9 N7 I. M) w6 n# j
" i" C- \0 R9 S+ W" r' V( A
nsga2是一个多目标优化算法。
1 T$ P, y$ P8 M, C# d3 g+ Y9 a
& Y& I& B+ t# m0 m. W: v& A6 M具体的nsga2通用算法请看:NSGA2算法MATLAB实现(能够自定义优化函数)8 w6 |/ g) b. ~1 e5 K7 J& [
# l6 E; M5 E5 V( I3 h! H7 j具体的特征选择代码在上述代码的基础上改了两个①主函数②评价函数,增加了一个数据分成训练集和测试集的函数:
. X" [, B$ A9 r& s, j" }6 o* W
" o, W* N% ?7 C9 h' t8 h+ }4 f- function divide_datasets()
- load Parkinson.mat;
- dataMat=Parkinson_f;
- len=size(dataMat,1);
- %归一化
- maxV = max(dataMat);
- minV = min(dataMat);
- range = maxV-minV;
- newdataMat = (dataMat-repmat(minV,[len,1]))./(repmat(range,[len,1]));
- Indices = crossvalind('Kfold', length(Parkinson_label), 10);
- site = find(Indices==1|Indices==2|Indices==3);
- train_F = newdataMat(site,:);
- train_L = Parkinson_label(site);
- site2 = find(Indices~=1&Indices~=2&Indices~=3);
- test_F = newdataMat(site2,:);
- test_L =Parkinson_label(site2);
- save train_F train_F;
- save train_L train_L;
- save test_F test_F;
- save test_L test_L;
- end
- %what doesn't kill you makes you stronger, stand a little taller,doesn't mean i'm over cause you're gonw.
8 J4 d2 \, _8 z
* T8 R" [7 v9 p8 z! f4 z( e0 T1 G& m2 M# J& x! q
MATLAB代码主函数:" Y& j6 B7 J, M1 \) U; m
: R4 W4 R" M5 l1 q- b- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %此处可以更改
- %更多机器学习内容请访问omegaxyz.com
- clc;
- clear;
- pop = 500; %种群数量
- gen = 100; %迭代次数
- M = 2; %目标数量
- V = 22; %维度
- min_range = zeros(1, V); %下界
- max_range = ones(1,V); %上界
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %特征选择
- divide_datasets();
- global answer
- answer=cell(M,3);
- global choice %选出的特征个数
- choice=0.8;
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- chromosome = initialize_variables(pop, M, V, min_range, max_range);
- chromosome = non_domination_sort_mod(chromosome, M, V);
- for i = 1 : gen
- pool = round(pop/2);
- tour = 2;
- parent_chromosome = tournament_selection(chromosome, pool, tour);
- mu = 20;
- mum = 20;
- offspring_chromosome = genetic_operator(parent_chromosome,M, V, mu, mum, min_range, max_range);
- [main_pop,~] = size(chromosome);
- [offspring_pop,~] = size(offspring_chromosome);
- clear temp
- intermediate_chromosome(1:main_pop,:) = chromosome;
- intermediate_chromosome(main_pop + 1 : main_pop + offspring_pop,1 : M+V) = offspring_chromosome;
- intermediate_chromosome = non_domination_sort_mod(intermediate_chromosome, M, V);
- chromosome = replace_chromosome(intermediate_chromosome, M, V, pop);
- if ~mod(i,100)
- clc;
- fprintf('%d generations completed\n',i);
- end
- end
- if M == 2
- plot(chromosome(:,V + 1),chromosome(:,V + 2),'*');
- xlabel('f_1'); ylabel('f_2');
- title('Pareto Optimal Front');
- elseif M == 3
- plot3(chromosome(:,V + 1),chromosome(:,V + 2),chromosome(:,V + 3),'*');
- xlabel('f_1'); ylabel('f_2'); zlabel('f_3');
- title('Pareto Optimal SuRFace');
- end! D/ C! g- v( z; q: ]- J$ B" S1 T
e. ~& r9 {& K( T1 S评价函数(利用林志仁SVM进行训练):
- @& d+ H3 Q( p3 T: N3 r7 {- j& Q8 }( D3 @! X; j& J
- function f = evaluate_objective(x, M, V, i)
- f = [];
- global answer
- global choice
- load train_F.mat;
- load train_L.mat;
- load test_F.mat;
- load test_L.mat;
- temp_x = x(1:V);
- inmodel = temp_x>choice;%%%%%设定恰当的阈值选择特征
- f(1) = sum(inmodel(1,:));
- answer(i,1)={f(1)};
- model = libsvmtrain(train_L,train_F(:,inmodel), '-s 0 -t 2 -c 1.2 -g 2.8');
- [predict_label, ~, ~] = libsvmpredict(test_L,test_F(:,inmodel),model,'-q');
- error=0;
- for j=1:length(test_L)
- if(predict_label(j,1) ~= test_L(j,1))
- error = error+1;
- end
- end
- error = error/length(test_L);
- f(2) = error;
- answer(i,2)={error};
- answer(i,3)={inmodel};
- end
* R5 d1 p; E' a
& L( X G8 P( x8 c选的的数据集请从UCI上下载。
2 F6 _( B* W2 D) ]0 G2 s" M6 N H+ s" V% M
结果:' g# I- I" j1 M9 t
5 j0 v: F+ h' U/ Q8 {+ g①pareto面
! P/ B1 G4 G, A( K5 s' q$ T
! j* G7 @# Y$ ?" T2 k
$ p! J( Z1 |$ V- Z
4 r% S n) u( [# s最后粒子的数据(选出的特征数和精确度)8 i' n3 E3 N' Q) x/ s& }
3 h+ u6 X: b& [! H0 Z% U
5 x5 i- w1 j ?0 \( d5 s/ U) O" N) Q9 m
$ H5 l3 J- v! v/ q0 ~% d/ L+ { |
|