%% 清空环境变量
warning off % 关闭报警信息
close all % 关闭开启的图窗
clear % 清空变量
clc % 清空命令行
%% 读取数据
res = xlsread('数据集.xlsx');
%% 分析数据
num_class = length(unique(res(:, end))); % 类别数(Excel最后一列放类别)
num_res = size(res, 1); % 样本数(每一行,是一个样本)
num_size = 0.7; % 训练集占数据集的比例
res = res(randperm(num_res), :); % 打乱数据集(不打乱数据时,注释该行)
flag_conusion = 1; % 标志位为1,打开混淆矩阵(要求2018版本及以上)
%% 设置变量存储数据
P_train = []; P_test = [];
T_train = []; T_test = [];
%% 划分数据集
for i = 1 : num_class
mid_res = res((res(:, end) == i), :); % 循环取出不同类别的样本
mid_size = size(mid_res, 1); % 得到不同类别样本个数
mid_tiran = round(num_size * mid_size); % 得到该类别的训练样本个数
P_train = [P_train; mid_res(1: mid_tiran, 1: end - 1)]; % 训练集输入
T_train = [T_train; mid_res(1: mid_tiran, end)]; % 训练集输出
P_test = [P_test; mid_res(mid_tiran + 1: end, 1: end - 1)]; % 测试集输入
T_test = [T_test; mid_res(mid_tiran + 1: end, end)]; % 测试集输出
end
%% 数据转置
P_train = P_train'; P_test = P_test';
T_train = T_train'; T_test = T_test';
%% 得到训练集和测试样本个数
M = size(P_train, 2);
N = size(P_test , 2);
%% 数据归一化
[p_train, ps_input] = mapminmax(P_train, 0, 1);
p_test = mapminmax('apply', P_test, ps_input );
t_train = ind2vec(T_train);
t_test = ind2vec(T_test );
%% 节点个数
inputnum = size(p_train, 1); % 输入层节点数
hiddennum = 6; % 隐藏层节点数
outputnum = size(t_train,1); % 输出层节点数
%% 建立网络
net = newff(p_train, t_train, hiddennum);
%% 设置训练参数
net.trainParam.epochs = 1000; % 训练次数
net.trainParam.goal = 1e-5; % 目标误差
net.trainParam.lr = 0.01; % 学习率
net.trainParam.showWindow = 0; % 关闭窗口
%% 参数初始化
c1 = 4.494; % 学习因子
c2 = 4.494; % 学习因子
maxgen = 30; % 种群更新次数
sizepop = 5; % 种群规模
Vmax = 1.0; % 最大速度
Vmin = -1.0; % 最小速度
popmax = 2.0; % 最大边界
popmin = -2.0; % 最小边界
%% 节点总数
numsum = inputnum * hiddennum + hiddennum + hiddennum * outputnum + outputnum;
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.95
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
%% 提取最优初始权值和阈值
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.trainParam.showWindow = 1; % 打开窗口
%% 网络训练
net = train(net, p_train, t_train);
%% 仿真预测
t_sim1 = sim(net, p_train);
t_sim2 = sim(net, p_test );
%% 数据反归一化
T_sim1 = vec2ind(t_sim1);
T_sim2 = vec2ind(t_sim2);
%% 性能评价
error1 = sum((T_sim1 == T_train)) / M * 100 ;
error2 = sum((T_sim2 == T_test )) / N * 100 ;
%% 绘图
figure
plot(1: M, T_train, 'r-*', 1: M, T_sim1, 'b-o', 'LineWidth', 1)
legend('真实值', '预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'训练集预测结果对比'; ['准确率=' num2str(error1) '%']};
title(string)
grid
figure
plot(1: N, T_test, 'r-*', 1: N, T_sim2, 'b-o', 'LineWidth', 1)
legend('真实值', '预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'测试集预测结果对比'; ['准确率=' num2str(error2) '%']};
title(string)
grid
%% 误差曲线迭代图
figure
plot(1 : length(BestFit), BestFit, 'LineWidth', 1.5);
xlabel('粒子群迭代次数');
ylabel('适应度值');
xlim([1, length(BestFit)])
string = {'模型迭代误差变化'};
title(string)
grid on
%% 混淆矩阵
if flag_conusion == 1
figure
cm = confusionchart(T_train, T_sim1);
cm.Title = 'Confusion Matrix for Train Data';
cm.ColumnSummary = 'column-normalized';
cm.RowSummary = 'row-normalized';
figure
cm = confusionchart(T_test, T_sim2);
cm.Title = 'Confusion Matrix for Test Data';
cm.ColumnSummary = 'column-normalized';
cm.RowSummary = 'row-normalized';
end