%% 清空环境变量
warning off % 关闭报警信息
close all % 关闭开启的图窗
clear % 清空变量
clc % 清空命令行
tic
% restoredefaultpath
%% 读取数据
res = xlsread('数据集.xlsx');
%% 划分训练集和测试集%
P_train = res(1: 250, 1: 12)';
T_train = res(1: 250, 13)';
M = size(P_train, 2);
P_test = res(251: end, 1: 12)';
T_test = res(251: end, 13)';
N = size(P_test, 2);
num_dim = size(P_train, 1); % 特征维度
num_class = length(unique(res(:, end))); % 类别数(Excel最后一列放类别) % 类别数(Excel最后一列放类别)
%% 数据转置
% 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 = categorical(T_train)';
t_test = categorical(T_test )';
%% 格式转换
for i = 1 : M
p_train{i, 1} = P_train(:, i);
end
for i = 1 : N
p_test{i, 1} = P_test(:, i);
end
%% 优化算法参数设置
pop = 6; % 数量
Max_iter = 8; % 最大迭代次数
dim = 3; % 优化参数个数
lb = [1e-3, 10, 1e-4]; % 参数取值下界(学习率,隐藏层节点,正则化系数)
ub = [1e-2, 30, 1e-1]; % 参数取值上界(学习率,隐藏层节点,正则化系数)
load('data.mat');
%% 记录最佳参数
best_lr = Best_pos(1, 1);
best_hd = ceil(Best_pos(1, 2));
best_l2 = Best_pos(1, 3);
%% 权重初始化
D = ones(1, M) / M;
%% 参数设置
K = 7; % 弱回归器个数
H = 6; % 隐藏层节点个数
%% 建立模型
% ---------------------- 修改模型结构时需对应修改fical.m中的模型结构 --------------------------
layers = [
sequenceInputLayer(num_dim) % 输入层
bilstmLayer(best_hd, 'OutputMode', 'last') % BILSTM层
reluLayer % Relu激活层
fullyConnectedLayer(num_class) % 全连接层
softmaxLayer % 损失函数层
classificationLayer]; % 分类层
%% 参数设置
% ---------------------- 修改模型参数时需对应修改fical.m中的模型参数 --------------------------
options = trainingOptions('adam', ... % Adam 梯度下降算法
'MaxEpochs', 200, ... % 最大训练次数 500
'InitialLearnRate', best_lr, ... % 初始学习率 best_lr
'LearnRateSchedule', 'piecewise', ... % 学习率下降
'LearnRateDropFactor', 0.5, ... % 学习率下降因子 0.1
'LearnRateDropPeriod', 150, ... % 经过 400 次训练后 学习率为 best_lr * 0.5
'Shuffle', 'every-epoch', ... % 每次训练打乱数据集
'ValidationPatience', Inf, ... % 关闭验证
'L2Regularization', best_l2, ... % 正则化参数
'Verbose', false);
%% 弱回归器回归
for i = 1 : K
%% 打开最后一次的损失曲线
if K == i
options.Plots = 'training-progress';
end
%% 训练模型
net = trainNetwork(p_train, t_train, layers, options);
%% 仿真预测
t_sim1{i} = predict(net, p_train);
t_sim2{i} = predict(net, p_test );
%% 数据格式转换
ztrain = double(ind2vec(double(t_train)'));
t_sim1{i} = double(t_sim1{i})';
t_sim2{i} = double(t_sim2{i})';
%% 预测误差
Error(i, :) = sum(abs(t_sim1{i} - ztrain)) / num_class;
%% 调整D值
weight(i) = 0;
for j = 1 : M
if abs(Error(i, j)) > 0.1
weight(i) = weight(i) + D(i, j);
D(i + 1, j) = D(i, j) * 1.1;
else
D(i + 1, j) = D(i, j);
end
end
%% 弱分类器i权重
weight(i) = 0.5 / exp(abs(weight(i)));
%% D值归一化
D(i + 1, :) = D(i + 1, :) / sum(D(i + 1, :));
end
%% 强预测器预测
weight = weight / sum(weight);
%% 强分类器分类结果
T_sim1 = zeros(num_class, M);
T_sim2 = zeros(num_class, N);
for i = 1 : K
output1 = (weight(i) * t_sim1{i});
output2 = (weight(i) * t_sim2{i});
T_sim1 = output1 + T_sim1;
T_sim2 = output2 + T_sim2;
end
%% 数据反归一化
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 ;
%% 数据排序
[T_train, index_1] = sort(T_train);
[T_test , index_2] = sort(T_test );
T_sim1 = T_sim1(index_1);
T_sim2 = T_sim2(index_2);
%% 绘图
figure
plot(1: M, T_train, 'r-*', 1: M, T_sim1, 'b-o', 'LineWidth', 1)
legend('真实值', 'BiLSTM-adaboost预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'训练集预测结果对比'; ['准确率=' num2str(error1) '%']};
title(string)
xlim([1, M])
grid
figure
plot(1: N, T_test, 'r-*', 1: N, T_sim2, 'b-o', 'LineWidth', 1)
legend('真实值', 'BiLSTMadaboost预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'测试集预测结果对比'; ['准确率=' num2str(error2) '%']};
title(string)
xlim([1, N])
grid
%% 混淆矩阵
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';