《量化投资:以MATLAB为工具》

MATLAB技术论坛

 找回密码
 注册账号
查看: 526|回复: 1
收起左侧

[提问] 关于LSSVM程序运行问题

[复制链接]
发表于 2017-3-26 12:21:14 | 显示全部楼层 |阅读模式
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%最小二乘支持向量机(LSSVM)软测量,崔宇,200901
%该函数的作用:用训练样本集进行训练,然后对测试样本集进行预测,返回预测输出值
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%【输入】: XTrain   - 训练集样本_锅炉工况数据
%          yTrain   - 训练集样本_飞灰含碳(或烟气含氧)真实值
%          XTest    - 测试集样本_锅炉工况
%          Gama     - 惩罚系数(LS-SVM参数,也叫正规化参数)
%          Sigma    - RBF核函数参数(LS-SVM参数)
%【输出】:yPredict - 飞灰含碳(或烟气含氧)软测量模型预测值


function yPredict= LSSVM(XTrain,yTrain,XTest,Gama,Sigma);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%初始化
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% t = cputime;
SampleNumber = size(XTrain,1);   %训练样本数(各论文中的“L”)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%LSSVM方法中,线性方程组等号左边的系数矩阵
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
InputMatrix =ones(SampleNumber + 1,SampleNumber + 1);  %初始化线性方程组等号左边的矩阵
InputMatrix(1,1) =0;
KernelXTrain = zeros(SampleNumber,SampleNumber);  %初始化线性方程组等号左边的矩阵中的子阵
%对线性方程等号左边的矩阵中的子阵进行赋值
for i = 1:SampleNumber
   
    for j = 1:SampleNumber
        
        if i < j,                  
            KernelXTrain(i,j) = sum((XTrain(j,:) - XTrain(i,:)).^2); %欧式距离的平方
        else
            KernelXTrain(i,j) = KernelXTrain(j,i);
        end
        
    end


end
%计算线性方程组等号左边矩阵
KernelXTrain = exp(KernelXTrain/-Sigma);
KernelXTrain =KernelXTrain + 1 / Gama * eye(SampleNumber);
InputMatrix(2:SampleNumber + 1,2:SampleNumber + 1) = KernelXTrain;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%LSSVM方法中,线性方程组等号右边的系数矩阵
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
OutMatrix = zeros(SampleNumber + 1,size(yTrain,2)); %初始化线性方程组等号右边的矩阵
%计算线性方程组右边矩阵
for i = 1:size(yTrain,2)
    OutMatrix(:,i) = [0;yTrain(:,i)];
end   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%求解LSSVM中的线性方程组
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Coefficient = inv(InputMatrix) * OutMatrix;
a = Coefficient(2:SampleNumber + 1,:);
b=zeros(size(XTest,1),1);
for i = 1:size(XTest,1)
    b(i,:) = Coefficient(1,:);
end


% cputime - t
% t1= cputime;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%计算测试集锅炉工况核函数
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
KernelXTest= zeros(size(XTest,1),SampleNumber);
for i = 1:size(XTest,1)
   
    for j = 1:SampleNumber
        KernelXTest(i,j) = sum((XTrain(j,:) - XTest(i,:)).^2);  %欧式距离的平方
    end
   
end
KernelXTest = exp(KernelXTest / -Sigma);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%使用训练得到的软测量模型,对某一个(或几个)工况下的飞灰含碳(或烟气含氧)进行预测
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yPredict = KernelXTest * a + b;


% (cputime - t1)/SampleNumber


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%存在的若干问题
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%




以上为LSSVM预测数据的程序,我输入相应的模拟数据,程序运行得不到结果

   命令窗口输入:XTrain=[2,1,3,2]
   yTrain=[2,3,2,1]
   XTest=[2,1,0,1]
   Gama=2
   Sigma=3
   yPredict= LSSVM(XTrain,yTrain,XTest,Gama,Sigma)


XTrain =


     2     1     3     2




yTrain =


     2     3     2     1




XTest =


     2     1     0     1




Gama =


     2




Sigma =


     3


未定义与 'double' 类型的输入参数相对应的函数 'LSSVM'。

>> lssvm
找不到 'lssvm' 的完全匹配项(区分大小写)。


最接近的匹配项为: LSSVM(在 E:\毕设\程序\LSSVM\LSSVM.m 中)
发表于 2017-5-15 10:04:08 | 显示全部楼层
咱们做的东西好像,我做炉膛燃烧建模,可以分享下吗
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

QQ|网站地图|MATLAB技术论坛|Simulink仿真论坛 ( 蜀ICP备19014457号 

GMT+8, 2020-4-3 01:04 , Processed in 0.049105 second(s), 12 queries , Gzip On, MemCached On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表