EDA365电子论坛网
标题:
PSO算法特征选择MATLAB实现(单目标)
[打印本页]
作者:
ulppknot
时间:
2020-10-20 17:19
标题:
PSO算法特征选择MATLAB实现(单目标)
+ I1 g) @! A6 K% T' w) l0 e V8 _
PSO进行进行特征选择其主要思想是:将子集的选择看作是一个搜索寻优问题(wrapper方法),生成不同的组合,对组合进行评价,再与其他的组合进行比较。这样就将子集的选择看作是一个是一个优化问题。
/ |4 e5 @# r1 S! t2 b* R: G! J& f
; Y9 a: z. @% O$ z+ q: a
下面是PSO进行特征选择的代码(注意:整体代码是单目标只优化错误率,注意训练使用的是林志仁SVM,数据集是Parkinson,可以到UCI上下载,训练的结果是错误率)
7 m ? n2 X. {9 ?! m$ _
1 M! }& L2 }: n/ s& A3 q9 S) w
数据集分割为训练集和测试集:
t0 i& Z) A; v0 f4 t
/ r) _6 P; p1 B* ?- Z
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
4 g7 Y& `- P4 O$ N; K5 G0 U# S% T
6 \- }" |0 W4 O% }# A, R
* V; Y5 I: p8 Y; _9 Y M# L9 R
主函数PSOFS:
4 y- q4 V7 c5 y4 m1 }, I
9 M: }8 \9 Y0 A6 V4 W
clear;
clc;
format long;
%------给定初始化条件----------------------------------------------
c1=2; %学习因子1
c2=2; %学习因子2
w=0.7; %惯性权重
MaxDT=100; %最大迭代次数
D=22; %搜索空间维数(未知数个数)
M=30; %初始化群体个体数目
bound=1;
%eps=10^(-6); %设置精度(在已知最小值时候用)
global answer %最后所有粒子的结果(包括特征与精确度)
answer=cell(M,3);
global choice %选出的特征个数
choice=0.8;
%------初始化种群的个体(可以在这里限定位置和速度的范围)------------
x=randn(M,D); %随机初始化位置
v=randn(M,D); %随机初始化速度
x(x>bound)=bound;
x(x<-bound)=-bound;
%------先计算各个粒子的适应度,并初始化p(i)和gbest--------------------
divide_datasets();
for i=1:M
p(i)=fitness(x(i,:),i);
y(i,:)=x(i,:);
end
gbest=x(1,:); %gbest为全局最优
for i=2:M
if(fitness(x(i,:),i)<fitness(gbest,i))
gbest=x(i,:);
end
end
%------进入主要循环,按照公式依次迭代,直到满足精度要求------------
for t=1:MaxDT
for i=1:M
v(i,:)=w*v(i,:)+c1*rand*(y(i,:)-x(i,:))+c2*rand*(gbest-x(i,:));
x(i,:)=x(i,:)+v(i,:);
if fitness(x(i,:),D)<p(i)
p(i)=fitness(x(i,:),i);
y(i,:)=x(i,:);
end
if p(i)<fitness(gbest,i)
gbest=y(i,:);
end
end
end
%------显示计算结果
disp('*************************************************************')
Solution=gbest';
Result=fitness(gbest,i);
disp('*************************************************************')
. |& k% ]/ p+ Q$ u1 X
8 f9 G- k. M$ m
2 q# V4 U$ i0 D( a G) t8 z! [' K1 ~, k
特征选择评价函数(利用林志仁的SVM进行训练):
9 F% F* l0 o' C1 a; f5 z
' k' P1 E5 I1 [$ z J( b" |( L
function error = fitness(x,i)
global answer
global choice
load train_F.mat;
load train_L.mat;
load test_F.mat;
load test_L.mat;
inmodel = x>choice;%%%%%设定恰当的阈值选择特征
answer(i,1)={sum(inmodel(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);
answer(i,2)={error};
answer(i,3)={inmodel};
end
9 C0 G5 g& @6 `3 M6 Q) y3 w! X
3 j2 s2 a3 v0 ^9 a2 J% |
结果(选出的特征数和错误率):
3 l6 Q0 v! T" ~( Y) I
c& T# @1 E9 s o5 o
2.jpg
(8.65 KB, 下载次数: 7)
下载附件
保存到相册
2020-10-20 17:12 上传
) _) j( L Q, h6 r: L
作者:
NNNei256
时间:
2020-10-20 20:13
PSO算法特征选择MATLAB实现
欢迎光临 EDA365电子论坛网 (https://bbs.eda365.com/)
Powered by Discuz! X3.2