%% 清空环境变量
warning off % 关闭报警信息
close all % 关闭开启的图窗
clear % 清空变量
clc % 清空命令行
%% 导入数据
result = xlsread('数据集');
%% 数据分析
num_samples = length(result); % 样本个数
kim = 2; % 延时步长(kim个历史数据作为自变量)
zim = 1; % 跨zim个时间点进行预测
nim = size(result, 2) - 1; % 原始数据的特征是数目
%% 划分数据集
for i = 1: num_samples - kim - zim + 1
res(i, :) = [reshape(result(i: i + kim - 1 + zim, 1: end - 1)', 1, ...
(kim + zim) * nim), result(i + kim + zim - 1, end)];
end
%% 数据集分析
outdim = 1; % 最后一列为输出
num_size = 0.85; % 训练集占数据集比例
num_train_s = round(num_size * num_samples); % 训练集样本个数
f_ = size(res, 2) - outdim; % 输入特征长度
num_val = 0.2; % 验证集比例
%% 划分训练集和测试集
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 = p_train'; p_test = p_test';
t_train = t_train'; t_test = t_test';
%% 数据平铺
p_train = reshape(p_train, 1, M, f_);
p_test = reshape(p_test , 1, N, f_);
智能算法及其模型预测