找回密码
 注册
关于网站域名变更的通知
查看: 561|回复: 1
打印 上一主题 下一主题

NSGA2算法特征选择MATLAB实现(多目标)

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2020-8-25 18:06 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x

" F. v* n& x# ]$ V
NSGA2算法特征选择MATLAB实现(多目标)
! q. N. P3 x6 }& U; f0 P0 v% \0 H

9 y% |: g* q7 \6 l( M
( L( d0 Y) h. e8 x  T* l利用nsga2进行进行特征选择其主要思想是:将子集的选择看作是一个搜索寻优问题(wrapper方法),生成不同的组合,对组合进行评价,再与其他的组合进行比较。这样就将子集的选择看作是一个是一个优化问题。/ A: P2 J& D. \0 B9 p

- k7 F5 r) H" X. O+ T4 M需要优化的两个目标为特征数和精度。
9 t( S6 z) A& X4 N2 G0 k' k3 @9 {4 v+ e) P) }2 K) d6 I
nsga2是一个多目标优化算法。
5 |% B  k% o6 ~2 F- Y
$ T! W8 h5 x$ A6 F$ k具体的nsga2通用算法请看:NSGA2算法MATLAB实现(能够自定义优化函数)8 H$ T7 _, f3 a* G/ }$ X$ u2 v) x

# R" i9 u6 R% [/ J3 M2 {具体的特征选择代码在上述代码的基础上改了两个①主函数②评价函数,增加了一个数据分成训练集和测试集的函数:
' a& }0 x* ^1 l+ V5 o) ^& f0 N, R3 C) Q3 p* q6 D+ R$ ~9 J3 N5 ~$ L+ F9 q6 [
  • 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.6 h: Q9 q- N; x2 o1 j/ p
  ! |& @# w0 d3 c5 @5 ?8 ]. t% Z$ b$ K2 f

# n+ u# {. ~. i+ d& \MATLAB代码主函数:. _* q2 z! z! Z! g( H4 j: K% T- L
2 O5 D% ^$ d, p2 f/ [3 l: |
  • %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  • %此处可以更改
  • %更多机器学习内容请访问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');
  • end5 g) @! S, ~* C3 d4 c+ i; T
        
3 l3 C: b* Z0 @" H% F% t- {评价函数(利用林志仁SVM进行训练):
4 [, Y. w! q$ p1 s0 v" J# [; i0 D6 J, M) E
  • 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};
  • end9 w$ @+ P% m# N; B
! H; {. N* M. ^5 v; {# a
选的的数据集请从UCI上下载。
' U: w- L" o8 {
' Q8 k7 {. q4 ]% F" D) F结果:
% ?$ ^; g: g6 F6 M0 j
1 y( I- {" D% o, Y- ]①pareto面% Z" c+ h% O5 L+ j  d3 c
7 T: n) C3 b  }4 C3 E

6 [  S, e) p. D" Z) i' r- L0 z% F) D/ ?" E2 C$ Z
最后粒子的数据(选出的特征数和精确度)
) o, q; P! V% F8 s$ S
# C& I8 e3 e2 Y: K/ b5 x7 {; v9 R3 s1 ?0 d7 w

6 T7 P; g6 E2 v# c8 U6 P& |7 \0 h" a1 x' l: }$ p4 X! M  U/ ?

该用户从未签到

2#
发表于 2020-8-25 18:13 | 只看该作者
NSGA2算法特征选择MATLAB实现(多目标)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-11-24 13:51 , Processed in 0.156250 second(s), 26 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表