%% 清空环境变量
warning off % 关闭报警信息
close all % 关闭开启的图窗
clear % 清空变量
clc % 清空命令行
%% 导入数据
res = xlsread('datann.xlsx');
%% 划分训练集和测试集
L=size(res,1);
Ratio=0.7;
M = round(L*Ratio);
N = L-M;
temp=1:L;
P_train = res(temp(1: M), 1: end-1)';
T_train = res(temp(1: M), end)';
M = size(P_train, 2);
P_test = res(temp(M+1: end), 1:end-1)';
T_test = res(temp(M+1: end), end)';
%% 数据平铺
% 将数据平铺成1维数据只是一种处理方式
% 也可以平铺成2维数据,以及3维数据,需要修改对应模型结构
% 但是应该始终和输入层数据结构保持一致
p_train = double(reshape(P_train, 6, 1, 1, M));
p_test = double(reshape(P_test , 6, 1, 1, N));
t_train = double(T_train)';
t_test = double(T_test )';
%% 构造网络结构
layers = [
imageInputLayer([6, 1, 1]) % 输入层 输入数据规模[6, 1, 1]
convolution2dLayer([3, 1], 16) % 卷积核大小 3*1 生成16张特征图
batchNormalizationLayer % 批归一化层
reluLayer % Relu激活层
convolution2dLayer([3, 1], 32) % 卷积核大小 3*1 生成32张特征图
batchNormalizationLayer % 批归一化层
reluLayer % Relu激活层
dropoutLayer(0.2) % Dropout层
fullyConnectedLayer(1) % 全连接层
regressionLayer]; % 回归层
%% 参数设置
options = trainingOptions('adam', ... % ADAM 梯度下降算法
'MiniBatchSize', 60, ... % 批大小,每次训练样本个数
'MaxEpochs', 100, ... % 最大训练次数
'InitialLearnRate', 1e-3, ... % 初始学习率为0.001
'LearnRateSchedule', 'piecewise', ... % 学习率下降
'LearnRateDropFactor', 0.5, ... % 学习率下降因子
'LearnRateDropPeriod', 50, ... % 经过50次训练后 学习率为 0.01 * 0.5
'Shuffle', 'every-epoch', ... % 每次训练打乱数据集
'Plots', 'training-progress', ... % 画出曲线
'Verbose', false);
%% 训练模型
net = trainNetwork(p_train, t_train, layers, options);
%% 模型预测
T_sim1 = predict(net, p_train);
T_sim2 = predict(net, p_test );
%% 均方根误差
error1 = sqrt(sum((T_sim1' - T_train).^2) ./ M);
error2 = sqrt(sum((T_sim2' - T_test ).^2) ./ N);
%% 绘制网络分析图
analyzeNetwork(layers)
%% 绘图
f1 = figure;
plot(1: M, T_train,'-','Color',[255 0 0]./255,'linewidth',1,'Markersize',3,'MarkerFaceColor',[255 0 0]./255)
hold on
plot(1: M, T_sim1,'-.','Color',[0 0 255]./255,'linewidth',1,'Markersize',3,'MarkerFaceColor',[0 0 255]./255)
legend('真实值','预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'训练集预测结果对比'; ['RMSE=' num2str(error1)]};
title(string)
xlim([1, M])
set(gca,'FontSize',12)
set(gca,'TickDir','out');
set(gca,'LineWidth',0.8);
drawnow
movegui('center');
f2 = figure;
plot(1: N, T_test,'-','Color',[255 0 0]./255,'linewidth',1,'Markersize',3,'MarkerFaceColor',[255 0 0]./255)
hold on
plot(1: N, T_sim2,'-.','Color',[0 0 255]./255,'linewidth',1,'Markersize',3,'MarkerFaceColor',[0 0 255]./255)
legend('真实值','预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'测试集预测结果对比';['RMSE=' num2str(error2)]};
title(string)
xlim([1, N])
set(gca,'FontSize',12)
set(gca,'TickDir','out');
set(gca,'LineWidth',0.8);
drawnow
movegui('center');
%% 相关指标计算
% 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)])
% MBE
mbe1 = sum(T_sim1' - T_train) ./ M ;
mbe2 = sum(T_sim2' - T_test ) ./ N ;
disp(['训练集数据的MBE为:', num2str(mbe1)])
disp(['测试集数据的MBE为:', num2str(mbe2)])
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于影响因素和CNN卷积神经网络的交通信息预测(Matlab完整程序和数据) 基于影响因素和CNN卷积神经网络的交通信息预测(Matlab完整程序和数据) 基于影响因素和CNN卷积神经网络的交通信息预测(Matlab完整程序和数据)
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/XLSX.png)
共 2 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/3899c4bd75d14bc2a043f1b2f2416dfc_m0_57362105.jpg!1)
前程算法屋
- 粉丝: 4459
- 资源: 712
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)