【lssvm预测】基于鲸鱼算法优化lssvm数据预测matlab源码

135 阅读1分钟

1 模型介绍

模型参考这里。

2 部分代码

%=====================================================================
%初始化
clc
close all
clear
format long
tic
%==============================================================
%%导入数据
data=xlsread('数值.xlsx','Sheet1','A2:E41');%训练
data1=xlsread('数值.xlsx','Sheet1','G2:J31');%测试
[row,col]=size(data);
train_x=data(:,1:col-1);
train_y=data(:,col);
test_x=data(:,1:col-1);
% test_y=data(:,col);
​
train_x=train_x';
train_y=train_y';
test_x=test_x';
% test_y=test_y';
​
%%数据归一化
[train_x,minx,maxx, train_yy,miny,maxy] =premnmx(train_x,train_y);
test_x=tramnmx(test_x,minx,maxx);
train_x=train_x';
train_yy=train_yy';
train_y=train_y';
test_x=test_x';
% test_y=test_y';
%% 参数初始化
eps = 10^(-6);
%%定义lssvm相关参数
type='f';
kernel = 'RBF_kernel';
proprecess='proprecess';
lb=[0.01 0.02];%参数c、g的变化的下限
ub=[1000 100];%参数c、g的变化的上限
dim=2;%维度,即一个优化参数
SearchAgents_no=20; % Number of search agents
Max_iter=50; % Maximum numbef of iterations
% initialize position vector and score for the leader
Leader_pos=zeros(1,dim);
Leader_score=inf; %change this to -inf for maximization problems
%Initialize the positions of search agents
% Positions=initialization(SearchAgents_no,dim,ub,lb);
Positions(:,1)=ceil(rand(SearchAgents_no,1).*(ub(1)-lb(1))+lb(1));
Positions(:,2)=ceil(rand(SearchAgents_no,1).*(ub(2)-lb(2))+lb(2));
Convergence_curve=zeros(1,Max_iter);
t=0;% Loop counter
% Main loop
​


woa1;
%% 结果分析
plot( Convergence_curve,'LineWidth',2);
title(['鲸鱼优化算法适应度曲线','(参数c1=',num2str(Leader_pos(1)),',c2=',num2str(Leader_pos(2)),',终止代数=',num2str(Max_iter),')'],'FontSize',13);
xlabel('进化代数');ylabel('误差适应度');
​
bestc = Leader_pos(1);
bestg = Leader_pos(2);
​
gam=bestc;
sig2=bestg;
model=initlssvm(train_x,train_yy,type,gam,sig2,kernel,proprecess);%原来是显示
model=trainlssvm(model);%原来是显示
%求出训练集和测试集的预测值
[train_predict_y,zt,model]=simlssvm(model,train_x);
[test_predict_y,zt,model]=simlssvm(model,test_x);
​
%预测数据反归一化
train_predict=postmnmx(train_predict_y,miny,maxy);%预测输出
test_predict=postmnmx(test_predict_y,miny,maxy);
figure
plot(train_predict,':og')
hold on
plot(train_y,'- *')
legend('预测输出','期望输出')
title('鲸鱼优化svm网络预测输出','fontsize',12)
ylabel('函数输出','fontsize',12)
xlabel('样本','fontsize',12)
disp(['预测输出'])
  YPred_best
toc   %计算时间

3 仿真结果

img

img

4 参考文献

[1]张儒, 叶向荣, 王可,等. 基于最小二乘支持向量机和粒子群法的水煤浆性能优化.

5 MATLAB代码与数据下载地址

见博客主页