来大神帮我改下程序最小二乘支持向量机100组数据用于训练50组数据用于校验。
%LS-SVM预测模型
clc
clear
%装载数据
load wyx;
x=wyx;
%M=200; %程序调试时减少样本以节省时间
%找出最大最小值
a1s=min(x);
b1s=max(x);
%归一化数据(1)
for i=1:3
g1=x(:,i)-a1s(:,i);
g2=b1s(:,i)-a1s(:,i);
eval(['f',int2str(i),'=g1/g2',';']);
end
A=[f1,f2,f3];
%x1=x(1:906,2);
save a1s a1s
save b1s b1s
P=[f1 f2]';%用此建立模型的输入
T=f3';
[trainV,testV] = dividevec(P,T,0.25);
% %初始化数据
X_IS=trainV.P;
Y_IS=trainV.T;
X_IS1= X_IS(:,1:100)';
Y_IS1=Y_IS(:,1:100)';
xs1=testV.P;
tts1=testV.T;
xs2=xs1(:,1:50)';
tts2=tts1(:,1:50)';
dj1=X_IS1;
yw1=Y_IS1;
%tt1=tt1';x1=x1';
type = 'function estimation';%表示进行函数拟合,若分类的话此处应改为分为函数
% 参数寻优过程
[gam,sig2]=tunelssvm({dj1,yw1,type,[],[],'RBF_kernel'},'simplex','leaveoneoutlssvm',{'mse'});%利用内部自带的寻优函数进行参数寻找
% %采用径向基函数训练模型
% [alpha,b] = trainlssvm ({x,tt, type, gam, sig2,'RBF_kernel','preprocess'}) ;
model = trainlssvm({dj1, yw1, type, gam, sig2, 'RBF_kernel', 'preprocess'});
save dj1 dj1;
save yw1 yw1;
save gam gam;
save sig2 sig2;
save model model;
%调入测试样本
tys=simlssvm({xs2,tts2,type,gam,sig2,'RBF_kernel','preprocess'},{model.alpha, model.b},xs2);
%ty1=tys*(b1s(3)-a1s(3))+a1s(3);%反归一化
ys=tts2*(b1s(3)-a1s(3))+a1s(3);%训练目标
tE2=tys-ys;
% tE=yw-yf;
tMAE=mae(tE2);
tMSE=mse(tE2);
save tE2 tE2;
figure;
plot(tE2,'r');%wucha
hold on;
figure;
plot(tys,'g');%moxing
hold on;
figure;
plot(ys,'b');%jiaoyan
hold on;
%LS-SVM预测模型
clc
clear
%装载数据
load wyx;
x=wyx;
%M=200; %程序调试时减少样本以节省时间
%找出最大最小值
a1s=min(x);
b1s=max(x);
%归一化数据(1)
for i=1:3
g1=x(:,i)-a1s(:,i);
g2=b1s(:,i)-a1s(:,i);
eval(['f',int2str(i),'=g1/g2',';']);
end
A=[f1,f2,f3];
%x1=x(1:906,2);
save a1s a1s
save b1s b1s
P=[f1 f2]';%用此建立模型的输入
T=f3';
[trainV,testV] = dividevec(P,T,0.25);
% %初始化数据
X_IS=trainV.P;
Y_IS=trainV.T;
X_IS1= X_IS(:,1:100)';
Y_IS1=Y_IS(:,1:100)';
xs1=testV.P;
tts1=testV.T;
xs2=xs1(:,1:50)';
tts2=tts1(:,1:50)';
dj1=X_IS1;
yw1=Y_IS1;
%tt1=tt1';x1=x1';
type = 'function estimation';%表示进行函数拟合,若分类的话此处应改为分为函数
% 参数寻优过程
[gam,sig2]=tunelssvm({dj1,yw1,type,[],[],'RBF_kernel'},'simplex','leaveoneoutlssvm',{'mse'});%利用内部自带的寻优函数进行参数寻找
% %采用径向基函数训练模型
% [alpha,b] = trainlssvm ({x,tt, type, gam, sig2,'RBF_kernel','preprocess'}) ;
model = trainlssvm({dj1, yw1, type, gam, sig2, 'RBF_kernel', 'preprocess'});
save dj1 dj1;
save yw1 yw1;
save gam gam;
save sig2 sig2;
save model model;
%调入测试样本
tys=simlssvm({xs2,tts2,type,gam,sig2,'RBF_kernel','preprocess'},{model.alpha, model.b},xs2);
%ty1=tys*(b1s(3)-a1s(3))+a1s(3);%反归一化
ys=tts2*(b1s(3)-a1s(3))+a1s(3);%训练目标
tE2=tys-ys;
% tE=yw-yf;
tMAE=mae(tE2);
tMSE=mse(tE2);
save tE2 tE2;
figure;
plot(tE2,'r');%wucha
hold on;
figure;
plot(tys,'g');%moxing
hold on;
figure;
plot(ys,'b');%jiaoyan
hold on;