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

nsga2 matlab,NSGA2算法特征选择MATLAB实现(多目标)利用nsga2进行进行特征选择其主要思想是:将子集的选择看作是一个搜索寻优问题(wrapper方法),生成不同的组合,对组合进行评价,再与其他的组合进行比较。这样就将子集的选择看作是一个是一个优化问题。需要优化的两个目标为特征数和精度。nsga2是一个多目标优化算法。具体的特征选择代码在上述代码的基础上改了两个①主函数②评价函数,增加了一个数据分成训练集和测试集的函数:MATLABfunction…

大家好,又见面了,我是你们的朋友全栈君。

利用nsga2进行进行特征选择其主要思想是:将子集的选择看作是一个搜索寻优问题(wrapper方法),生成不同的组合,对组合进行评价,再与其他的组合进行比较。这样就将子集的选择看作是一个是一个优化问题。

需要优化的两个目标为特征数和精度。

nsga2是一个多目标优化算法。

具体的特征选择代码在上述代码的基础上改了两个①主函数②评价函数,增加了一个数据分成训练集和测试集的函数:

MATLAB

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

functiondivide_datasets()

loadParkinson.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);

savetrain_Ftrain_F;

savetrain_Ltrain_L;

savetest_Ftest_F;

savetest_Ltest_L;

end

%what doesn’t kill you makes you stronger, stand a little taller,doesn’t mean i’m over cause you’re gonw.

MATLAB代码主函数:

MATLAB

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%此处可以更改

%更多机器学习内容请访问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

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%此处可以更改

%更多机器学习内容请访问omegaxyz.com

clc;

clear;

pop=500;%种群数量

gen=100;%迭代次数

M=2;%目标数量

V=22;%维度

min_range=zeros(1,V);%下界

max_range=ones(1,V);%上界

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%特征选择

divide_datasets();

globalanswer

answer=cell(M,3);

globalchoice%选出的特征个数

choice=0.8;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

chromosome=initialize_variables(pop,M,V,min_range,max_range);

chromosome=non_domination_sort_mod(chromosome,M,V);

fori=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);

cleartemp

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

ifM==2

plot(chromosome(:,V+1),chromosome(:,V+2),’*’);

xlabel(‘f_1’);ylabel(‘f_2’);

title(‘Pareto Optimal Front’);

elseifM==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

评价函数(利用林志仁SVM进行训练):

MATLAB

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

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

functionf=evaluate_objective(x,M,V,i)

f=[];

globalanswer

globalchoice

loadtrain_F.mat;

loadtrain_L.mat;

loadtest_F.mat;

loadtest_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;

forj=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

选的的数据集请从UCI上下载。

结果:

①pareto面

350a2e25c5c2ed2dadac9717be3fa336.png

最后粒子的数据(选出的特征数和精确度)

1bfb457926d8d8e4ead04a057cf335f9.png

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/144466.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • WPF布局之WrapPanel与StackPanel

    WPF布局之WrapPanel与StackPanel转载:https://www.cnblogs.com/Im-Victor/p/10565030.html三.WrapPanelWrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列,当长度或高度不够是就会自动调整进行换行,后续排序按照从上至下或从右至左的顺序进行。Orientation——根据内容自动换行。当Horizontal选项看上去类似于Windows资源管理器的缩略图视图:元素是从左向右排列的,然后自上至下自动换行。Vertical选项看上去类似于Windows资源..

    2022年7月23日
    12
  • 《欧美剧集观看最佳索引》(US SHOWS GUIDE) 【2005-12-27 转verycd】

    《欧美剧集观看最佳索引》(US SHOWS GUIDE) 【2005-12-27 转verycd】原文地址http://bbs.verycd.com/topics/230847/中文名称:欧美剧集观看最佳索引英文名称:USTVSHOWSGUIDE别名:欧美剧集观看最佳索引版本:2005-2006导演:USTVSHOWSGUIDE演员:USTVSHOWSGUIDE简介:欧美剧集观看最佳索引2005-2006USTVSHOWSGUIDE2005-2006(作者:

    2022年5月6日
    57
  • Java的Scanner输入时,next()和nextLine()的区别[通俗易懂]

    Java的Scanner输入时,next()和nextLine()的区别[通俗易懂]nextLine()不要和其他next方法一起用!!!尤其nextLine()不要放在它们后面!!!这个问题已经坑了我好多次了,但是每次都没有在意,主要是没反应过来出现问题的原因。今天阿里内推测验,又被nextLine()狠狠坑了一下。逻辑思路都是对的,就输入的数据不对。因为限时半小时,所以时间比较紧张,最后还是没弄出来。后来百度查了两者区别,果然问题是出在这里,改完之后问题就解决了。可怜我的…

    2022年6月10日
    40
  • pycharm的scrapy框架-断点调试「建议收藏」

    pycharm的scrapy框架-断点调试「建议收藏」在文件根目录,也就是settings.py的上级目录,scrapy.cfg的同级目录,创建main.py:fromscrapy.cmdlineimportexecuteimportosimportsysif__name__==’__main__’:sys.path.append(os.path.dirname(os.path.abspath(__file__)))execute([‘scrapy’,’crawl’,’你的spider的name’])点

    2022年5月11日
    41
  • 前后端分离架构概述「建议收藏」

    1、背景      前后端分离已成为互联网项目开发的业界标准使用方式,通过nginx+tomcat的方式(也可以中间加一个nodejs)有效的进行解耦,并且前后端分离会为以后的大型分布式架构、弹性计算架构、微服务架构、多端化服务(多种客户端,例如:浏览器,车载终端,安卓,IOS等等)打下坚实的基础。这

    2022年4月6日
    54
  • 基于android的_android studio创建activity

    基于android的_android studio创建activityAndroid如何判断一个应用在运行 在一个应用中,或一个Service、Receiver中判断一个应用是否正在运行,以便进行一些相关的处理。这个时候我们需要得到一个ActivityManager,这个Manager顾名思意就是管理Activity的,它有一个方法叫getRunningTasks,可以得到当前系统正在运行的Task的列表,代码如下:  A

    2022年9月6日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号