需要下载工具箱,我下载的是:LSSVMlabv1_8_R2009b_R2011a,网上随便一搜就可以下载到。
clc
clear all
%% 导入数据
load('S_Data.mat') % 预测S含量所用数据
n = 12; % n 是自变量的个数
m = 1; % m 是因变量的个数
%% 读取训练数据
train_num = 1600; %训练样本数
train_Data = S_Data(1:train_num,:);
% 特征值归一化
[train_Input,minI,maxI] = premnmx(train_Data(:,1:n)');
% 构造输出矩阵
[train_Output,minO,maxO] = premnmx(train_Data(:,n+1:end)');
%% 读取测试数据
test_Data = S_Data(train_num+1:end,:);
% 特征值归一化
test_Input = tramnmx(test_Data(:,1:n)',minI,maxI);
% 构造测试输出矩阵
test_Output = tramnmx(test_Data(:,n+1:end)',minO,maxO);
% 选取合适的参数gam
RMSE = [];
Gam = 1:10:200;
sig = 600;
for q = 1:20
gam = Gam(1,q);
tic; %计时开始
% 用trainlssvm()函数对训练数据进行训练
[alpha,b] = trainlssvm(train_Input',train_Output',gam,sig);
SVMtest_Output = simlssvm(test_Input',train_Input',alpha,b,sig);
toc; %计时结束
test_Output = postmnmx(test_Output,minO,maxO);
SVMtest_Output = postmnmx(SVMtest_Output,minO,maxO);
% 测试数据误差
test_err = test_Output - SVMtest_Output;
n1 = length(SVMtest_Output);
test_RMSE = sqrt(sum((test_err).^2)/n1);
RMSE(1,q) = test_RMSE;
end
x = Gam; y = RMSE;
plot(x,y,'-o')
xlabel('参数gam')
ylabel('S含量预测误差(RMSE)')
clc
clear all
%% 导入数据
load('S_Data.mat') % 预测S含量所用数据
n = 12; % n 是自变量的个数
m = 1; % m 是因变量的个数
%% 读取训练数据
train_num = 1600; %训练样本数
train_Data = S_Data(1:train_num,:);
% 特征值归一化
[train_Input,minI,maxI] = premnmx(train_Data(:,1:n)');
% 构造输出矩阵
[train_Output,minO,maxO] = premnmx(train_Data(:,n+1:end)');
%% 读取测试数据
test_Data = S_Data(train_num+1:end,:);
% 特征值归一化
test_Input = tramnmx(test_Data(:,1:n)',minI,maxI);
% 构造测试输出矩阵
test_Output = tramnmx(test_Data(:,n+1:end)',minO,maxO);
% 选取合适的参数sig
RMSE = [];
gam = 20;
Sig = 1:100:1500;
for q = 1:15
sig = Sig(1,q);
tic; %计时开始
% 用trainlssvm()函数对训练数据进行训练
[alpha,b] = trainlssvm(train_Input',train_Output',gam,sig);
SVMtest_Output = simlssvm(test_Input',train_Input',alpha,b,sig);
toc; %计时结束
test_Output = postmnmx(test_Output,minO,maxO);
SVMtest_Output = postmnmx(SVMtest_Output,minO,maxO);
% 测试数据误差
test_err = test_Output - SVMtest_Output;
n1 = length(SVMtest_Output);
test_RMSE = sqrt(sum((test_err).^2)/n1);
RMSE(1,q) = test_RMSE;
end
x = Sig; y = RMSE;
plot(x,y,'-o')
xlabel('参数sig')
ylabel('S含量预测误差(RMSE)')
clc
clear all
%% 导入数据
load('S_Data.mat') % 预测S含量所用数据
n = 12; % n 是自变量的个数
m = 1; % m 是因变量的个数
%% 读取训练数据
train_num = 1600; %训练样本数
train_Data = S_Data(1:train_num,:);
% 特征值归一化
[train_Input,minI,maxI] = premnmx(train_Data(:,1:n)');
% 构造输出矩阵
[train_Output,minO,maxO] = premnmx(train_Data(:,n+1:end)');
gam = 20;
sig = 700;
type = 'function estimation';
tic; %计时开始
% 用trainlssvm()函数对训练数据进行训练
[alpha,b] = trainlssvm({train_Input',train_Output',type,gam,sig,'RBF_kernel'});
SVMtrain_Output = simlssvm({train_Input',train_Output',type,gam,sig,'RBF_kernel','preprocess'},{alpha,b},train_Input');
toc; %计时结束
train_Output = postmnmx(train_Output',minO,maxO);
SVMtrain_Output = postmnmx(SVMtrain_Output',minO,maxO);
% 训练数据误差
train_err = train_Output - SVMtrain_Output';
n1 = length(SVMtrain_Output);
train_RMSE = sqrt(sum((train_err).^2)/n1);
%% 读取测试数据
test_Data = S_Data(train_num+1:end,:);
% 特征值归一化
test_Input = tramnmx(test_Data(:,1:n)',minI,maxI)';
% 构造测试输出矩阵
test_Output = tramnmx(test_Data(:,n+1:end)',minO,maxO)';
SVMtest_Output = simlssvm({train_Input',train_Output,type,gam,sig,'RBF_kernel','preprocess'},{alpha,b},test_Input);
test_Output = postmnmx(test_Output,minO,maxO);
SVMtest_Output = postmnmx(SVMtest_Output',minO,maxO);
% 测试数据误差
test_err = test_Output - SVMtest_Output';
n2 = length(SVMtest_Output');
test_RMSE = sqrt(sum((test_err).^2)/n2);
%% 预测结果可视化
figure(6); % 绘制图1
subplot(2,1,1); % 图1包含2行1列个子图形,首先绘制子图1
plot(SVMtest_Output,':og'); % 用绿色的o绘制测试数据的预测输出值
hold on;
plot(test_Output','-*b'); % 用蓝色的*绘制测试数据的期望输出值
legend('预测输出','期望输出'); % 子图1的注释
title('最小二乘支持向量机预测S含量结果','fontsize',12) %子图1的标题
% title('BP神经网络Si含量预测输出','fontsize',12) %子图1的标题
ylabel('S含量','fontsize',12); % y轴
xlabel('样本','fontsize',12); % x轴
subplot(2,1,2); % 绘制子图2
plot(abs(test_Output - SVMtest_Output'),'-*'); % 输出测试数据的预测误差
title('最小二乘支持向量机预测S含量误差','fontsize',12) %子图2的标题
% title('BP神经网络Si含量预测误差','fontsize',12) %子图2的标题
ylabel('误差','fontsize',12); % y轴
xlabel('样本','fontsize',12); % x轴
ylim([0,0.01]);