利用PSO优化SVM参数的一点小探索

标签:
psomatlablibsvm杂谈 |
分类: 编程相关 |
其中PSO代码思路,参照版主 shi01fg
的进行的修改..O(∩_∩)O.
============
利用PSO优化SVM中的c和g
图:
结果:
Elapsed time is 64.799304 seconds.
bestc =
bestg =
bestCVaccuarcy =
Accuracy = 97.7528% (87/89) (classification)
trainacc =
Accuracy = 93.2584% (83/89) (classification)
testacc =
代码:
%% 清空环境
clc
clear
load wine;
train = [wine(1:30,:);wine(60:95,:);wine(131:153,:)];
train_label =
[wine_labels(1:30);wine_labels(60:95);wine_labels(131:153)];
test = [wine(31:59,:);wine(96:130,:);wine(154:178,:)];
test_label =
[wine_labels(31:59);wine_labels(96:130);wine_labels(154:178)];
[train,pstrain] = mapminmax(train');
pstrain.ymin = 0;
pstrain.ymax = 1;
[train,pstrain] = mapminmax(train,pstrain);
[test,pstest] = mapminmax(test');
pstest.ymin = 0;
pstest.ymax = 1;
[test,pstest] = mapminmax(test,pstest);
train = train';
test = test';
%% 参数初始化
%粒子群算法中的两个参数
c1 = 1.6; % c1 belongs to [0,2]
c2 = 1.5; % c2 belongs to [0,2]
maxgen=300;
sizepop=30;
popcmax=10^(2);
popcmin=10^(-1);
popgmax=10^(3);
popgmin=10^(-2);
k = 0.6; % k belongs to [0.1,1.0];
Vcmax = k*popcmax;
Vcmin = -Vcmax ;
Vgmax = k*popgmax;
Vgmin = -Vgmax ;
% SVM参数初始化
v = 3;
%% 产生初始粒子和速度
for i=1:sizepop
end
% 找极值和极值点
[global_fitness bestindex]=min(fitness); % 全局极值
local_fitness=fitness;
global_x=pop(bestindex,:);
local_x=pop;
tic
%% 迭代寻优
for i=1:maxgen