matlab函数initzero,MATLAB关于函数initlssvm的用法

:'( 我用的matlab是2014a版本的,输入以下程序

clear

clc

%导入训练数据

train_input=xlsread('Sample Data2',['D2:Y115']);

train_out=xlsread('Sample Data2',['Z2:Z115']);

train_input=train_input';

train_out=train_out';

[train_data,pstrain0]=mapminmax(train_input,0,1);

[train_result,pstrain1]=mapminmax(train_out,0,1);

c1=1.5; %PSO参数局部搜索能力

c2=1.7; %PSO参数全局搜索能力

maxgen=200;  %最大迭代次数Tmax

sizepop=30;  %粒子数m,又称群体规模,过大的m会影响算法的运算速度和收敛性。

%%初始化惩罚因子c和核函数参数 的取值范围

popcmax=1000; %惩罚因子c变化的最大值

popcmin=0.1;  %惩罚因子c变化的最小值

popgmax=100; %核函数参数 变化的最大值

popgmin=0.01; %核函数参数 变化的最小值

k=0.5;  %适应阈值 =0~1

Vcmax=k*popcmax; %惩罚因子c迭代速度的最大值

Vcmin=-Vcmax; %参数c迭代速度的最小值

Vgmax=k*popgmax; %核函数参数 迭代速度的最大值

Vgmin=-Vgmax; %核函数参数 迭代速度的最小值

eps=10^(-8); %机器的浮点运算误差限,当某个数小于它时则记为0

%%定义LSSVM相关参数

type='function estimation'; %回归

%type=’c’,表示classification 分类,type=’f’,表示回归。

kernel='RBF_kernel'; %LSSVM的核函数选取为径向基内核

proprecess='proprecess';

%proprecess表明数据已归一化处理,original表明数据没有进行归一化处理

%%产生初始粒子和速度

for i=1:sizepop %种群规模

%随机产生种群

pop(i,1)=(popcmax-popcmin)*rand(1,1)+popcmin; %初始种群

pop(i,2)=(popgmax-popgmin)*rand(1,1)+popgmin;

V(i,1)=Vcmax*rands(1,1); %初始化速度

V(i,2)=Vgmax*rands(1,1); %rands(1,1)产生介于-1~1之间的1*1的权值矩阵

%计算初始适应度=以训练集的预测值计算的均方差为适应度值

gam=pop(i,1);  %惩罚因子c初始化

sig2=pop(i,2);  %核函数参数 初始化

model=initlssvm(train_data,train_result,type,gam,sig2,kernel,proprecess);

model=trainlssvm(model);

%求出训练集的预测值

[train_predict_y,zt,model]=simlssvm(model,train_data);

%预测数据转置并取反归一化

train_predict=mapminmax('reverse',train_predict_y',pstrain1);

%计算训练集预测值的均方差

trainmse=sum((train_predict-train_out).^2)/length(train_result);

fitness(i)=trainmse;  %以训练集的预测值计算的均方差为适应度值

end

%%找极值和极值点

[global_fitness bestindex]=min(fitness); %全局极值

local_fitness=fitness; %个体极值初始化

global_x=pop(bestindex,:); %全局极值点

local_x=pop; %个体极值点初始化

%每一代种群的平均适应度

avgfitness_gen=zeros(1,maxgen)

tic %%%tic与toc是计时用的

%%迭代寻优

for i=1:maxgen %最大迭代次数

for j=1:sizepop %种群规模即粒子数

%速度更新

wmax=1.2; %初始权重w为1.2

wmin=0.8; %最终权重w为0.8

wV=wmax-i*(wmax-wmin)/maxgen; %带惯性权重的粒子群算法

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)

V(j,1)=Vcmin;

end

if V(j,2)>Vgmax

V(j,2)=Vgmax;

end

if V(j,2)

V(j,2)=Vgmin;

end

%种群位置更新(根据位置更新公式而得)

pop(j,:)=pop(j,:)+V(j,:);

%以下几个不等式是为了限定惩罚因子c在最大值最小值之间

if pop(j,1)>popcmax

pop(j,1)=popcmax;

end

if pop(j,1)

pop(j,1)=popcmin;

end

%以下几个不等式是为了限定核函数参数 在最大值最小值之间

if pop(j,2)>popgmax

pop(j,2)=popgmax;

end

if pop(j,2)

pop(j,2)=popgmin;

end

%%自适应粒子变异

if rand>0.5

k=ceil(2*rand); %ceil是向离它最近的大整数圆整

if k==1

pop(j,k)=(20-1)*rand+1;

end

if k==2

pop(j,k)=(popgmax-popgmin)*rand+popgmin;

end

%%新粒子适应度值

gam=pop(j,1);  %新粒子的惩罚因子c

sig2=pop(j,2);  %新粒子的核函数参数

model=initlssvm(train_data,train_result,type,gam,sig2,kernel,proprecess);

model=trainlssvm(mdoel)

%%求出训练集的预测值

[train_predict_y,zt,model]=simlssvm(model,train_data);

%预测数据转置并取反归一化

train_predict=mapminmax('reverse',train_predict_y',pstrain1);

%计算训练集预测数据的均方差

trainmse=sum((train_predict-train_out).^2)/length(train_result);

fitness(j)=trainmse;

end

%个体最优更新

if fitness(j)

local_x(j,:)=pop(j,:);

local_fitness(j)=fitness(j);

end

if fitness(j)==local_fitness(j)&&pop(j,1)

local_x(j,:)=pop(j,:);

local_fitness(j)=fitness(j);

end

%%群体最优更新

if fitness(j)

global_x=pop(j,:);

global_fitness=fitness(j);

end

if abs(fitness(j)-global_fitness)<=eps&&pop(j,1)

global_x=pop(j,:);

global_fitness=fitness(j);

end

end

fit_gen(i)=global_fitness; %全局极值

avgfitness_gen(i)=sum(fitness)/sizepop;

%个体极值的平均值(每一次迭代的极值之和除以迭代次数)

end

toc  %与前述的tic相结合用来计算优化程序运行的时间

%%结果分析

plot(fit_gen,'LineWidth',5);  %全局极值随迭代次数的变化曲线

title(['适应度曲线','(参数c1=',num2str(c1),',c2=',num2str(c2),',终止迭代次数=',num2str(maxgen),')'],'FontSize',13);

xlabel('进化次数');yabel('适应度');

%%记录粒子群优化结果,最优的惩罚因子c和核函数参数

bestc=global_x(1);

bestg=global_x(2);

gam=bestc

sig2=bestg

%%%利用粒子群算法优化参数结果bestc、bestg建模

model=initlssvm(train_data,train_result,type,gam,sig2,kernel,proprecess);

model=trainlssvm(model);

%save model.mat

%%求出训练集的预测值

[train_predict_y,zt,model]=simlssvm(model,train_data);

%%预测数据转置并取反归一化

train_predict=mapminmax('reverse',train_predict_y',pstrain1);

%%计算训练集预测值均方差

trainmse=sum((train_predict-train_out).^2)/length(train_result)

%%计算训练集预测值的相对误差

trainRelativeError=sum(abs((train_out-train_predict)/train_out))/length(train_out)

%%计算训练集预测值相对误差的均方根误差

trainRMSE=sqrt(sum(((train_out-train_predict)/train_out).^2)/length(train_out))

%%用训练集模型预测测试集

%%导入测试数据

test_input=xlsread('Sample Data2',['D116:Y145']);

test_out=xlsread('Sample Data2',['Z116:Z145']);

%%测试数据取转置

test_input=test_input';

test_out=test_out';

%%使用mapminmax归一化测试数据

[test_data,pstrain0]=mapminmax(test_input,0,1);

%%利用训练集模型预测测试集

%load model.mat

[test_predict_y,zt,model]=simlssvm(model,test_data);

%预测数据转置并取反归一化

test_predict=mapminmax('reverse',test_predict_y',pstrain1)

%%计算预测集预测值的均方差

testmse=sum((test_predict-test_out).^2)/length(test_out)

%%计算预测集预测值的相对误差

testRelativeError=sum(abs((test_out-test_predict)/test_out))/length(test_out)

%%计算预测集预测值相对误差的均方根误差

testRMSE=sqrt(sum(((test_out-test_predict)/test_out).^2)/length(test_out))

%%%训练集与测试集的真值与预测值归一

result=[train_out,test_out];

predict=[train_predict,test_predict];

%%计算所有数据预测值的均方差

testmse=sum((predict-result).^2)/length(result)

%%计算所有数据预测值的相对误差

testRelativeError=sum(abs((resssult- predict)/result))/length(result)

%%计算所有数据预测值相对误差的均方根误差

testRMSE=sqrt(sum(((result-predict)/result).^2)/length(result))

%%画图

figure

plot(result,'k')

hold on

plot(predict,'r')

xlabel('样本/组')

ylabel('工信部百公里燃油消耗量L/100km')

%%%%%%%%%%%

%%%%%%%%%

%%%%%%%%%

%%%%%%%%%

%%%总是提示出错 Untitled10 (line 41)

%%%%%%model=initlssvm(train_data,train_result,type,gam,sig2,kernel,proprecess);

望大神给看看,是怎么回事,急求啊……………………:'(

LSSVM做回归希望对大家有用-源程序以及数据.rar 源代码 %% ==============清除代码窗口和工作空间=============== clc; clear; %% ==================下载数据======================= load surdata1.mat; load surdata2.mat; load wearvol.mat; load workcondition.mat; %% ==================训练数据======================== % 数据预处理 xx=surdata1'; yy=surdata2'; zz=wearvol; ww=workcondition; % 训练数据提取 X=ww; X=xx; Y=yy; %% =====================测试数据===================== Xt=ww; Xt=xx; %% =====================模型初始化===================== type = 'function estimation'; kernel = 'RBF_kernel'; gam = 100;                 % Regularization parameter sig2 = 0.01;              % Kernel parameter (bandwidth in the case of the 'RBF_kernel' % 寻优之后的参数 % gam =  0.0869335 ;                 % Regularization parameter % sig2 =  83.8678 ;              % Kernel parameter (bandwidth in the case of the 'RBF_kernel' %% =====================参数寻优====================== model = initlssvm;                 % 模型初始化 costfun = 'crossvalidatelssvm'; costfun_args = {10,'mse'}; optfun = 'gridsearch'; model = tunelssvm;   % 模型参数优化 %% ======================测试数据======================= model = trainlssvm;  % 训练 Yp = simlssvm; %% ======================结果显示======================== figure; plot,Yp,'ro:') hold on plot,Yp,'b*:') hold on plot,Yp,'k :') grid on; 源程序以及数据.rar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值