%% 参数初始化
c1 = 4.494; % 学习因子
c2 = 4.494; % 学习因子
maxgen = 50; % 种群更新次数
sizepop = 5; % 种群规模
Vmax = 1.0; % 最大速度
Vmin = -1.0; % 最小速度
popmax = 1.0; % 最大边界
popmin = -1.0; % 最小边界
for i = 1 : sizepop
pop(i, :) = rands(1, numsum); % 初始化种群
V(i, :) = rands(1, numsum); % 初始化速度
fitness(i) = fun(pop(i, :), hiddennum, net, p_train, t_train);
end
%% 个体极值和群体极值
[fitnesszbest, bestindex] = min(fitness);
zbest = pop(bestindex, :); % 全局最佳
gbest = pop; % 个体最佳
fitnessgbest = fitness; % 个体最佳适应度值
BestFit = fitnesszbest; % 全局最佳适应度值
%% 迭代寻优
for i = 1 : maxgen
for j = 1 : sizepop
% 速度更新
V(j, :) = V(j, :) + c1 * rand * (gbest(j, :) - pop(j, :)) + c2 * rand * (zbest - pop(j, :));
V(j, (V(j, :) > Vmax)) = Vmax;
V(j, (V(j, :) < Vmin)) = Vmin;
% 种群更新
pop(j, :) = pop(j, :) + 0.2 * V(j, :);
pop(j, (pop(j, :) > popmax)) = popmax;
pop(j, (pop(j, :) < popmin)) = popmin;
% 自适应变异
pos = unidrnd(numsum);
if rand > 0.85
pop(j, pos) = rands(1, 1);
end
% 适应度值
fitness(j) = fun(pop(j, :), hiddennum, net, p_train, t_train);
end
for j = 1 : sizepop
% 个体最优更新
if fitness(j) < fitnessgbest(j)
gbest(j, :) = pop(j, :);
fitnessgbest(j) = fitness(j);
end
% 群体最优更新
if fitness(j) < fitnesszbest
zbest = pop(j, :);
fitnesszbest = fitness(j);
end
end
BestFit = [BestFit, fitnesszbest];
end
%% 节点个数
inputnum = size(p_train, 1); % 输入层节点数
hiddennum = 5; % 隐藏层节点数
outputnum = size(t_train,1); % 输出层节点数
%% 建立网络
net = newff(p_train, t_train, hiddennum);
%% 设置训练参数
net.trainParam.epochs = 1000; % 训练次数
net.trainParam.goal = 1e-6; % 目标误差
net.trainParam.lr = 0.01; % 学习率
net.trainParam.showWindow = 0; % 关闭窗口
%% 导入数据
res = xlsread('数据集.xlsx');
%% 数据分析
num_size = 0.7; % 训练集占数据集比例
outdim = 1; % 最后三列为输出
num_samples = size(res, 1); % 样本个数
num_train_s = round(num_size * num_samples); % 训练集样本个数
f_ = size(res, 2) - outdim; % 输入特征维度
%% 划分训练集和测试集
P_train = res(1: num_train_s, 1: f_)';
T_train = res(1: num_train_s, f_ + 1: end)';
M = size(P_train, 2);
P_test = res(num_train_s + 1: end, 1: f_)';
T_test = res(num_train_s + 1: end, f_ + 1: end)';
N = size(P_test, 2);
%% 数据归一化
[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);
t_test = mapminmax('apply', T_test, ps_output);
%% 节点个数
inputnum = size(p_train, 1); % 输入层节点数
hiddennum = 5; % 隐藏层节点数
outputnum = size(t_train,1); % 输出层节点数
%% 计算节点总数
numsum = inputnum * hiddennum + hiddennum + hiddennum * outputnum + outputnum;
%% 参数初始化
c1 = 4.494; % 学习因子
c2 = 4.494; % 学习因子
maxgen = 50; % 种群更新次数
sizepop = 5; % 种群规模
Vmax = 1.0; % 最大速度
Vmin = -1.0; % 最小速度
popmax = 1.0; % 最大边界
popmin = -1.0; % 最小边界
%% 提取最优初始权值和阈值
w1 = zbest(1 : inputnum * hiddennum);
B1 = zbest(inputnum * hiddennum + 1 : inputnum * hiddennum + hiddennum);
w2 = zbest(inputnum * hiddennum + hiddennum + 1 : inputnum * hiddennum ...
+ hiddennum + hiddennum * outputnum);
B2 = zbest(inputnum * hiddennum + hiddennum + hiddennum * outputnum + 1 : ...
inputnum * hiddennum + hiddennum + hiddennum * outputnum + outputnum);
%% 最优值赋值
net.Iw{1, 1} = reshape(w1, hiddennum, inputnum);
net.Lw{2, 1} = reshape(w2, outputnum, hiddennum);
net.b{1} = reshape(B1, hiddennum, 1);
net.b{2} = B2';
%% 网络训练
net = train(net, p_train, t_train);
%% 仿真预测
t_sim1 = sim(net, p_train);
t_sim2 = sim(net, p_test );
%% 绘图
figure
plot(1: M, T_train, 'r-*', 1: M, T_sim1, 'b-o', 'LineWidth', 1)
legend('真实值', '预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'训练集预测结果对比'; ['RMSE=' num2str(error1)]};
title(string)
xlim([1, M])
grid
figure
plot(1: N, T_test, 'r-*', 1: N, T_sim2, 'b-o', 'LineWidth', 1)
legend('真实值', '预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'测试集预测结果对比'; ['RMSE=' num2str(error2)]};
title(string)
xlim([1, N])
grid
%% 相关指标计算
% R2
R1 = 1 - norm(T_train - T_sim1)^2 / norm(T_train - mean(T_train))^2;
R2 = 1 - norm(T_test - T_sim2)^2 / norm(T_test - mean(T_test ))^2;
disp(['训练集数据的R2为:', num2str(R1)])
disp(['测试集数据的R2为:', num2str(R2)])
% MAE
mae1 = sum(abs(T_sim1 - T_train), 2)' ./ M ;
mae2 = sum(abs(T_sim2 - T_test ), 2)' ./ N ;
disp(['训练集数据的MAE为:', num2str(mae1)])
disp(['测试集数据的MAE为:', num2str(mae2)])
% MBE
mbe1 = sum(T_sim1 - T_train, 2)' ./ M ;
mbe2 = sum(T_sim2 - T_test , 2)' ./ N ;
disp(['训练集数据的MBE为:', num2str(mbe1)])
disp(['测试集数据的MBE为:', num2str(mbe2)])
免费!!!!! 没有套路
点击链接:百度网盘链接
只希望可以帮忙点个关注,感谢!!!!
后续会继续分享免费程序
https://pan.baidu.com/s/1YmZgEJXEuYIT1Hwuwv9fvg?pwd=gdou
也可以进入一下主页,有更多数据回归、分类、时间序列预测算法程序
机器不会学习主页