天天看點

MAT之SVM/BP:SVR(better)和BP兩種方法比較且實作建築物鋼筋混凝土抗壓強度預測

輸出結果

MAT之SVM/BP:SVR(better)和BP兩種方法比較且實作建築物鋼筋混凝土抗壓強度預測

代碼設計

load concrete_data.mat

n = randperm(size(attributes,2));

p_train = attributes(:,n(1:80))';

t_train = strength(:,n(1:80))';

p_test = attributes(:,n(81:end))';

t_test = strength(:,n(81:end))';

[pn_train,inputps] = mapminmax(p_train');

pn_train = pn_train';

pn_test = mapminmax('apply',p_test',inputps);

pn_test = pn_test';

[tn_train,outputps] = mapminmax(t_train');

tn_train = tn_train';

tn_test = mapminmax('apply',t_test',outputps);

tn_test = tn_test';

[c,g] = meshgrid(-10:0.5:10,-10:0.5:10);

[m,n] = size(c);

cg = zeros(m,n);

eps = 10^(-4);

v = 5;

bestc = 0;

bestg = 0;

error = Inf;

for i = 1:m

   for j = 1:n

       cmd = ['-v ',num2str(v),' -t 2',' -c ',num2str(2^c(i,j)),' -g ',num2str(2^g(i,j) ),' -s 3 -p 0.1'];

       cg(i,j) = svmtrain(tn_train,pn_train,cmd);

       if cg(i,j) < error

           error = cg(i,j);

           bestc = 2^c(i,j);

           bestg = 2^g(i,j);

       end

       if abs(cg(i,j) - error) <= eps && bestc > 2^c(i,j)

   end

end

cmd = [' -t 2',' -c ',num2str(bestc),' -g ',num2str(bestg),' -s 3 -p 0.01'];

model = svmtrain(tn_train,pn_train,cmd);

[Predict_1,error_1] = svmpredict(tn_train,pn_train,model);

[Predict_2,error_2] = svmpredict(tn_test,pn_test,model);

predict_1 = mapminmax('reverse',Predict_1,outputps);

predict_2 = mapminmax('reverse',Predict_2,outputps);

result_1 = [t_train predict_1];

result_2 = [t_test predict_2];

figure(1)

plot(1:length(t_train),t_train,'r-*',1:length(t_train),predict_1,'b:o')

grid on

legend('真實值','預測值')

xlabel('樣本編号')

ylabel('耐壓強度')

string_1 = {'訓練集預測結果對比(SVM之SVR)—Jason niu';

          ['mse = ' num2str(error_1(2)) ' R^2 = ' num2str(error_1(3))]};

title(string_1)

figure(2)

plot(1:length(t_test),t_test,'r-*',1:length(t_test),predict_2,'b:o')

string_2 = {'SVM之SVR測試集預測結果對比(SVM之SVR)—Jason niu';

          ['mse = ' num2str(error_2(2)) ' R^2 = ' num2str(error_2(3))]};

title(string_2)

%BP神經網絡

net = newff(pn_train,tn_train,10);

net.trainParam.epochs = 1000;

net.trainParam.goal = 1e-3;

net.trainParam.show = 10;

net.trainParam.lr = 0.1;

net = train(net,pn_train,tn_train);

tn_sim = sim(net,pn_test);

E = mse(tn_sim - tn_test);

N = size(t_test,1);

R2=(N*sum(tn_sim.*tn_test)-sum(tn_sim)*sum(tn_test))^2/((N*sum((tn_sim).^2)-(sum(tn_sim))^2)*(N*sum((tn_test).^2)-(sum(tn_test))^2));

t_sim = mapminmax('reverse',tn_sim,outputps);

figure(3)

plot(1:length(t_test),t_test,'r-*',1:length(t_test),t_sim,'b:o')

string_3 = {'測試集預測結果對比(BP神經網絡)—Jason niu';

          ['mse = ' num2str(E) ' R^2 = ' num2str(R2)]};

title(string_3)

繼續閱讀