%% 清空环境变量
warning off % 关闭报警信息
close all % 关闭开启的图窗
clear % 清空变量
clc % 清空命令行
%% 导入数据
res = xlsread('数据集.xlsx');
%% 划分训练集和测试集
temp = randperm(103);
P_train = res(temp(1: 80), 1: 7)';
T_train = res(temp(1: 80), 8)';
M = size(P_train, 2);
P_test = res(temp(81: end), 1: 7)';
T_test = res(temp(81: end), 8)';
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);
% GMM参数设置
iter = 25; % 迭代次数
Cov_type = 'full'; %协方差矩阵的类型
N_inputs = min(size(p_train'));%输入维度
N_outputs = min(size(t_train'));%输出维度
N_mixtures = 3;%高斯混合模型混合数
%% 仿真预测
t_sim1 = GMM( p_train', N_inputs, N_outputs, N_mixtures, Cov_type);
t_sim2 = GMM( p_test', N_inputs, N_outputs, N_mixtures, Cov_type);
%% 数据反归一化
T_sim1 = mapminmax('reverse', t_sim1, ps_output)';
T_sim2 = mapminmax('reverse', t_sim2, ps_output)';
%% 均方根误差
error1 = sqrt(sum((T_sim1 - T_train).^2) ./ M);
error2 = sqrt(sum((T_sim2 - T_test ).^2) ./ N);
%% 绘图
figure()
subplot(2,1,1)
plot(1: M, T_train, 'r-*', 1: M, T_sim1, 'b-o', 'LineWidth', 1)
legend('真实值', '预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'GMM训练集预测结果对比'; ['RMSE=' num2str(error1)]};
title(string)
xlim([1, M])
grid
subplot(2,1,2)
plot(1: N, T_test, 'r-*', 1: N, T_sim2, 'b-o', 'LineWidth', 1)
legend('真实值', '预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'GMM测试集预测结果对比'; ['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)) ./ M ;
mae2 = sum(abs(T_sim2 - T_test )) ./ N ;
disp(['训练集数据的MAE为:', num2str(mae1)])
disp(['测试集数据的MAE为:', num2str(mae2)])
% MAPE mape = mean(abs((YReal - YPred)./YReal));
mape1 = mean(abs((T_train - T_sim1)./T_train));
mape2 = mean(abs((T_test - T_sim2 )./T_test));
disp(['训练集数据的MAPE为:', num2str(mape1)])
disp(['测试集数据的MAPE为:', num2str(mape2)])
后台私信回复“61期”即可获取下载链接。