预测未来 | MATLAB实现Transformer时间序列预测未来
预测效果
基本介绍
1.Matlab实现Transformer时间序列预测未来;
2.运行环境Matlab2023b及以上,data为数据集,单变量时间序列预测;
3.递归预测未来数据,可以控制预测未来大小的数目,适合循环性、周期性数据预测;
4.命令窗口输出R2、MAE、MAPE、MBE、MSE等评价指标;
5.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。
6.适用对象:大学生课程设计、期末大作业和毕业设计。
程序设计
- 代码获取私信回复MATLAB实现Transformer时间序列预测未来
clike
%% 清空环境变量
warning off % 关闭报警信息
close all % 关闭开启的图窗
clear % 清空变量
clc % 清空命令行
%% 导入数据
result = xlsread('data.xlsx');
%% 数据集分析
outdim = 1; % 最后一列为输出
num_size = 0.7; % 训练集占数据集比例
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);
%% 数据平铺
P_train = double(reshape(P_train, f_, 1, 1, M));
P_test = double(reshape(P_test , f_, 1, 1, N));
t_train = t_train';
t_test = t_test' ;
%% 数据格式转换
for i = 1 : M
p_train{i, 1} = P_train(:, :, 1, i);
end
for i = 1 : N
p_test{i, 1} = P_test( :, :, 1, i);
end
参考资料
[1] https://blog.csdn.net/kjm13182345320/article/details/126805601?spm=1001.2014.3001.5501
[2] https://blog.csdn.net/kjm13182345320/article/details/126805183?spm=1001.2014.3001.5501
[3] https://blog.csdn.net/kjm13182345320/article/details/126775607?spm=1001.2014.3001.5501
[4] https://blog.csdn.net/kjm13182345320/article/details/126738853?spm=1001.2014.3001.5501