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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 哈佛的幸福课笔记

    哈佛的幸福课笔记觉得对本人而言有新信息的是第四课和第六课。第八课关于感恩,很好!(Tal和外婆的真实的故事,感人,几次热泪盈眶)。第10课关于改变当然很有用。第12、13课讨论关于建立自我和谐的目标和应对压力问题。哈佛的幸福课 http://v.163.com/special/positivepsychology/ (提醒:每课一个半小时) 第一课什么是积极心理学笔记:(简介)1、解读比

    2022年7月25日
    5
  • MATLAB插值函数interp1

    MATLAB插值函数interp1插值法    插值法又称“内插法”,是利用函数f(x)在某区间中已知的若干点的函数值,作出适当的特定函数,在区间的其他点上用这特定函数的值作为函数f(x)的近似值,这种方法称为插值法。如果这特定函数是多项式,就称它为插值多项式。线性插值法    线性插值法是指使用连接两个已知量的直线来确定在这两个已知量之间的一个未知量的值的方法。    

    2022年6月13日
    116
  • LTE学习之路(15)——QCI

    LTE学习之路(15)——QCIQCI表示QoS类别指示。这是一种特定标识,其定义了LTE数据包通信的质量QCI类别范围:1~9(每个类别的定义如下表所示)注意:通过‘ActivatedefaultEPSbearercon

    2022年8月4日
    2
  • 列车调度 思路解析

    列车调度 思路解析火车站的列车调度铁轨的结构如下图所示。两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道。每趟列车从入口可以选择任意一条轨道进入,最后从出口离开。在图中有9趟列车,在入口处按照{8,4,2,5,3,9,1,6,7}的顺序排队等待进入。如果要求它们必须按序号递减的顺序从出口离开,则至少需要多少条平行铁轨用于调度?输入格式:输入第一行给出一个…

    2022年7月26日
    4
  • 你应该知道的10个奇特的 HTML5 单页网站「建议收藏」

    你应该知道的10个奇特的 HTML5 单页网站「建议收藏」网页设计师努力寻找新的方式来展现内容。其中一个大的趋势是单页网站,现在被世界上的一些大的品牌广泛采用,使用它们来为用户提供一个快速,干净和简单的而且​​美丽的网站。下面是10个令人惊叹的单页H​​T

    2022年8月3日
    3
  • TP5 关联模型使用(嵌套关联、动态排序以及隐藏字段)

    TP5 关联模型使用(嵌套关联、动态排序以及隐藏字段)

    2021年11月7日
    40

发表回复

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

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