svm回归matlab工具箱很慢,PSO优化SVM参数进行回归预测,结果很不理想
具體程序代碼如下,主要問題就是不管我怎么調整參數,最終的預測結果都沒有太大的變化,還請了解這兩大算法的幫忙看看問題出在哪里了,十分感謝!
tic;
close all;
clear;
clc;
format compact;
load M2.mat? ? %載入數據
% 提取數據
N=length(M2)-36;
train_tsx=zeros(N,36);
for i=1:N
train_tsx(i,:)=M2(i:i+35);
Y(i)=M2(i+36);? ?? ?? ?? ?? ?%36個月代表貨幣周期,從歷史數據來看,大致與短經濟周期一樣(3-4年)
end
train_ts=Y';
%數據預處理,將原始數據進行歸一化
ts = train_ts';
tsx = train_tsx';
% mapminmax為matlab自帶的映射函數
% 對ts進行歸一化
[TS,TSps] = mapminmax(ts,0,1);
% 對TS進行轉置,以符合libsvm工具箱的數據格式要求
TS = TS';
% mapminmax為matlab自帶的映射函數
% 對tsx進行歸一化
[TSX,TSXps] = mapminmax(tsx,0,1);
% 對TSX進行轉置,以符合libsvm工具箱的數據格式要求
TSX = TSX';
% PSO尋找最佳的SVM參數c&g
% 參數初始化
%粒子群算法中的兩個參數
c1 = 0.5; % c1 belongs to [0,2]
c2 = 0.5; % c2 belongs to [0,2]
maxgen=200;? ?% 進化次數
sizepop=50;? ?% 種群規模
popcmax=10^(2);
popcmin=10^(-1);
popgmax=10^(3);
popgmin=10^(-2);
k = 0.5;? ?? ? % k belongs to [0.1,1.0];
Vcmax = 500;
Vcmin = 1 ;
Vgmax = 10;
Vgmin = 0.1 ;
% SVM參數初始化
v = 5;
% 產生初始粒子和速度
for i=1:sizepop? ???% 隨機產生種群
pop(i,1) = (popcmax-popcmin)*rand+popcmin;? ? % 初始種群
pop(i,2) = (popgmax-popgmin)*rand+popgmin;
V(i,1)=Vcmax*rands(1);??% 初始化速度
V(i,2)=Vgmax*rands(1);
% 計算初始適應度
cmd = ['-v ',num2str(v),' -c ',num2str( pop(i,1) ),' -g ',num2str( pop(i,2) ),' -s 3 -t 2 -p 0.01 '];
fitness(i) = svmtrain(TS, TSX, cmd);
fitness(i) = -fitness(i);
end
% 找極值和極值點
[global_fitness, bestindex]=min(fitness); % 全局極值
local_fitness=fitness;? ?% 個體極值初始化
global_x=pop(bestindex,:);? ?% 全局極值點
local_x=pop;? ? % 個體極值點初始化
% 迭代尋優
for i=1:maxgen
for j=1:sizepop? ?? ? %速度更新
wV = 1;? ?? ?% wV best belongs to [0.8,1.2]
V(j,:) = wV*V(j,:) + c1*rand*(local_x(j,:) - pop(j,:)) + c2*rand*(global_x - pop(j,:));
if V(j,1) > Vcmax
V(j,1) = Vcmax;
end
if V(j,1) < Vcmin
V(j,1) = Vcmin;
end
if V(j,2) > Vgmax
V(j,2) = Vgmax;
end
if V(j,2) < Vgmin
V(j,2) = Vgmin;
end
%種群更新
wP = 0.5;
pop(j,:)=pop(j,:)+wP*V(j,:);
if pop(j,1) > popcmax
pop(j,1) = popcmax;
end
if pop(j,1) < popcmin
pop(j,1) = popcmin;
end
if pop(j,2) > popgmax
pop(j,2) = popgmax;
end
if pop(j,2) < popgmin
pop(j,2) = popgmin;
end
% 自適應粒子變異
if rand>0.5
k=ceil(2*rand);
if k == 1
pop(j,k) = (20-1)*rand+1;
end
if k == 2
pop(j,k) = (popgmax-popgmin)*rand+popgmin;
end
end
%適應度值
cmd = ['-v ',num2str(v),' -c ',num2str( pop(j,1) ),' -g ',num2str( pop(j,2) ),' -s 3 -t 2 -p 0.01 '];
fitness(j) = svmtrain(TS, TSX, cmd);
fitness(j) = -fitness(j);
end
%個體最優更新
if fitness(j) < local_fitness(j)
local_x(j,:) = pop(j,:);
local_fitness(j) = fitness(j);
end
%群體最優更新
if fitness(j) < global_fitness
global_x = pop(j,:);
global_fitness = fitness(j);
end
fit_gen(i)=global_fitness;
end
toc
bestc = global_x(1);
bestg = global_x(2);
bestmse = -fit_gen(maxgen);
% 利用回歸預測分析最佳的參數進行SVM訓練
cmd = ['-c ',num2str( bestc ),' -g ',num2str( bestg ),' -s 3 -t 2 -p 0.01 '];
model = svmtrain(TS,TSX,cmd);
% 進行SVM預測
test_tsx1 = TS(163:198);
test_tsx1=test_tsx1';
[predict1,mse,detesvalue] = svmpredict(8.29,test_tsx1,model);
test_ts1 = mapminmax('reverse',predict1',TSps);
總結
以上是生活随笔為你收集整理的svm回归matlab工具箱很慢,PSO优化SVM参数进行回归预测,结果很不理想的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html中给div设置的属性怎么样才能拿
- 下一篇: qt5.13.2输出中文乱码