利用Matlab_libsvm构造支持向量机回归算法

tic
clear;
clc;

Fnum = 4;       %输入个数/输出个数只能为1个
TnumX = 0.8;    %训练数据比例
Terror = 0.2;   %误差小于Terror的预测比例
eps = 10^(-7);  %误差阈值 
v=6;            %交互检验
% 寻找最佳c参数/g参数  CV
cmax = 10;
cmin = -10;
gmax = 10;
gmin = -10;
cstep = 1;
gstep = 1;
msestep = 0.05;
local = 'C:\Users\37989\Desktop\2688.xlsx';%数据文件地址



data = xlsread(local);
[Ynum,Xnum] = size(data);
Train = data(randperm(Ynum),:);
Tnum = round(TnumX * Ynum);
TSnum = Ynum - Tnum;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%主要修改以上数据%以下代码不要修改
% 训练集
p_train = Train(1:Tnum,1:Fnum);%2:4 是回弹后的零件  5列是分组标记
t_train = Train(1:Tnum,Fnum+1);
% 测试集
p_test = Train(Tnum + 1:Ynum,1:Fnum);  %这个改了 R8000生成的只有3列
t_test = Train(Tnum + 1:Ynum,Fnum+1); 
%无所谓


%% 数据归一化
% 训练集
[pn_train,inputps] = mapminmax(p_train');    %归一化方法?
pn_train = pn_train';
[tn_train,outputps] = mapminmax(t_train');
tn_train = tn_train';


% 测试集
pn_test = mapminmax('apply',p_test',inputps);
pn_test = pn_test';
tn_test = mapminmax('apply',t_test',outputps);
tn_test = tn_test';


%% SVM模型创建/训练
% 寻找最佳c参数/g参数  CV
[c,g] = meshgrid(cmin:cstep:cmax,gmin:gstep:gmax);
[m,n] = size(c);
cg = zeros(m,n);
bestc = 0;
bestg = 0;
error1= Inf;
count=1;

%循环前
h=waitbar(0,'正在计算....');
for i = 1:m
    a=i;
    for j = 1:n
        %寻优的cmd参数应该与下文训练的cmd相符
        cmd = ['-v ',num2str(v),' -t 2',' -c ',num2str(2^c(a,j)),' -g ',num2str(2^g(a,j) ),' -s 3 -p 0.1 -q'];%0.01
        cg(a,j) = svmtrain(tn_train,pn_train,cmd)*10000; 
        if cg(a,j) < error1
            error1 = cg(a,j);  %回归均方根误差
            bestc = 2^c(a,j);
            bestg = 2^g(a,j);
        end
        if abs(cg(a,j) - error1) <= eps && bestc > 2^c(a,j)    %两次的mse相差很小 取c较小的
            error1 = cg(a,j);
            bestc = 2^c(a,j);
            bestg = 2^g(a,j);
        end
        waitbar(count/(m*n),h);
        count=count+1;
    end
end

[cg,ps] = mapminmax(cg,0,1);
figure;
set(gcf,'position',[0,100,1366,500]);%1 3 是宽度 2 4 是高度
subplot(1,2,1)
[C,h] = contour(c,g,cg,0.0023:msestep:0.5);
clabel(C,h,'FontSize',14,'Color','r');
xlabel('log_2C','FontName','Times','FontSize',14);
ylabel('log_2g','FontName','Times','FontSize',14);
firstline = 'SVR参数选择结果图(等高线图)[GridSearchMethod]';
secondline = ['Best C=',num2str(bestc),' g=',num2str(bestg), ...
    ' CVmse=',num2str(error1)];
title({firstline;secondline},'Fontsize',14);
grid on;
set(gca,'xtick',[cmin:cstep:cmax]);
set(gca,'ytick',[gmin:gstep:gmax]);

subplot(1,2,2)
meshc(c,g,cg);
% mesh(X,Y,cg);
% surf(X,Y,cg);
axis([cmin,cmax,gmin,gmax,0,1]);
xlabel('log_2C','FontName','Times','FontSize',14);
ylabel('log_2g','FontName','Times','FontSize',14);
zlabel('MSE','FontName','Times','FontSize',14);
firstline = 'SVR参数选择结果图(3D视图)[GridSearchMethod]';
secondline = ['Best C=',num2str(bestc),' g=',num2str(bestg), ...
    ' CVmse=',num2str(error1)];
title({firstline;secondline},'Fontsize',14);

%% 创建/训练SVM
%参考libsvm使用说明对cmd参数修改
cmd=[' -t 2',' -c ',num2str(bestc),' -g ',num2str(bestg),' -s 3 -p 0.001 '];
model = svmtrain(tn_train,pn_train,cmd);

%% SVM仿真预测
[Predict_2,error_2,decision_values] = svmpredict(tn_test,pn_test,model);
% 反归一化
F= mapminmax('reverse',Predict_2,outputps);
toc

%% 输出结果
E = F - t_test;
percent=sum(E<Terror)/TSnum*100;
MSE=sum(E.^2)/TSnum;
mse_norm =error_2(2);
R2=error_2(3);
SD=std(E);
Result=[t_test,F,abs(t_test-F)];
fprintf('%4.3f\t%4.3f\t%4.3f\n',Result');
fprintf(['\nc=%4.4f g=%4.4f\nSTD=%4.4f MSE=%4.4f R^2 =%4.5f Max=%4.4f',...
    ' Min =%4.4f Range=%4.4f 误差在%s内=%4.2f%%\n'],bestc,bestg,SD,MSE,...
    R2,max(E),min(E),max(E)-min(E),num2str(Terror),percent)

猜你喜欢

转载自blog.csdn.net/qq_28969139/article/details/80714354