Contents
I. 清空环境变量
clear all
clc
II. 训练集/测试集产生
1. 导入数据
load concrete_data.mat
2. 随机产生训练集和测试集
temp = randperm(size(attributes,2)); % 训练集——80个样本 P_train = attributes(:,temp(1:80)); T_train = strength(:,temp(1:80)); % 测试集——23个样本 P_test = attributes(:,temp(81:end)); T_test = strength(:,temp(81:end)); N = size(P_test,2);
III. 数据归一化
[p_train, ps_input] = mapminmax(P_train,0,1);
p_test = mapminmax(‘apply‘,P_test,ps_input);
[t_train,ps_output] = mapminmax(T_train,0,1);
IV. BP神经网络创建、训练及仿真测试
1. 创建网络
net = newff(p_train,t_train,11);
2. 设置训练参数
net.trainParam.epochs = 1000; net.trainParam.goal = 1e-3; net.trainParam.lr = 0.01;
3. 训练网络
net = train(net,p_train,t_train);
4. 仿真测试
t_sim = sim(net,p_test);
5. 数据反归一化
T_sim = mapminmax(‘reverse‘,t_sim,ps_output);
V. 性能评价
1. 绝对误差error
error = abs(T_sim - T_test)./T_test;
2. 决定系数R^2
R2 = (N * sum(T_sim .* T_test) - sum(T_sim) * sum(T_test))^2 / ((N * sum((T_sim).^2) - (sum(T_sim))^2) * (N * sum((T_test).^2) - (sum(T_test))^2));
3. 结果对比
result = [T_test‘ T_sim‘ error‘]
result = 28.1600 31.1291 0.1054 52.6500 53.0643 0.0079 30.9700 26.2469 0.1525 38.4600 38.0986 0.0094 41.1400 39.8364 0.0317 30.8300 30.8207 0.0003 36.1900 35.7525 0.0121 32.7100 33.8095 0.0336 41.0100 40.0879 0.0225 32.8400 33.3301 0.0149 33.9100 31.6726 0.0660 38.1900 38.3960 0.0054 26.4200 27.5678 0.0434 17.1900 18.4602 0.0739 35.5200 35.2201 0.0084 49.9700 51.0821 0.0223 48.7700 48.8715 0.0021 46.3600 46.2920 0.0015 31.5000 29.0295 0.0784 42.0800 42.0742 0.0001 36.4600 39.2970 0.0778 44.4800 44.6282 0.0033 58.5300 53.3348 0.0888
VI. 画图
figure plot(1:N,T_test,‘b:*‘,1:N,T_sim,‘r-o‘) legend(‘真实值‘,‘预测值‘) xlabel(‘预测样本‘) ylabel(‘混凝土抗压强度‘) string = {‘测试集混凝土抗压强度预测结果对比‘;[‘R^2=‘ num2str(R2)]}; title(string)