matlab动态神经网络进行时间序列预测分析

本文介绍使用Matlab中的动态神经网络进行时间序列预测的方法,重点讲解NAR和NARX模型,并通过实例演示如何设定参数、构建及评估模型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

matlab动态神经网络进行时间序列预测分析

时间序列预测问题分类

  • 有y,无x,即 y(t)=f(y(t1),y(t2),...) y ( t ) = f ( y ( t − 1 ) , y ( t − 2 ) , . . . ) (NAR)
  • 有x,有y,即 y(t)=f(x(t1),x(t2),...,y(t1),y(t2),...) y ( t ) = f ( x ( t − 1 ) , x ( t − 2 ) , . . . , y ( t − 1 ) , y ( t − 2 ) , . . . ) (NARX)
  • 有x,无y,即 y(t)=f(x(t1),x(t2),... y ( t ) = f ( x ( t − 1 ) , x ( t − 2 ) , . . . (比较少见)

matlab中的动态神经网络

  • matlab中有narnet、narxnet等函数可以用于时间序列的预测分析,下面主要对narnet尽心说明
  • narnet需要设置当前值依赖过去值的个数k,即依赖多少个过去的值,同时还需要设置隐含层节点的个数,搭建之后就可以进行构建模型了
  • 预测的时候,需要首先给出预测初始时刻的前k个值,因为当前值是由前k个值决定的,将此作为输入,就可以依次给出后面的预测值。

代码

%%
clc,clear,close all
t = -5:.01:5;
pressure = sin(10*t)';
figure
plot( pressure )
%% 训练集与测试集的个数
num_all_data = length( pressure );
% 前75%的数据作为训练数据
num_train = floor( num_all_data * 0.75 );
% 后25%的数据作为测试数据
num_test = num_all_data - num_train;
% 转化为narnet需要的序列数据
y_train_nn = num2cell( pressure(1:num_train) )';
y_test_nn = num2cell( pressure(1+num_train:end))';
%% 延迟,即当前值依赖于过去的多少个值
feedback_delays = 1:10;
% 隐含层节点的个数
num_hd_neuron = 10;
% narnet构建
net = narnet(feedback_delays, num_hd_neuron);
[Xs,Xi,Ai,Ts] = preparets(net,{},{}, y_train_nn);
net = train(net,Xs,Ts,Xi,Ai);
view(net)
Y = net(Xs,Xi);
perf = perform(net,Ts,Y);
fprintf( 'neural network: mse on training set : %.6f\n', perf );
%% 神经网络进行进行预测
yini = y_train_nn(end-max(feedback_delays)+1:end);
[Xs,Xi,Ai] = preparets(net,{},{},[yini y_test_nn]);
y_pred_nn = net(Xs,Xi,Ai)';
y_pred_nn = cell2mat( y_pred_nn );
y_test_nn = cell2mat( y_test_nn )';

%% 画图,计算mse
figure
title('NARNET预测')
hold on
plot( y_test_nn, 'r', 'linewidth', 2 );
plot( y_pred_nn, 'b--', 'linewidth', 2 );
legend({ '真实值', '神经网络预测值'})
nn_per_error = mean(abs(y_pred_nn-y_test_nn) ./ y_test_nn);
nn_mse_error = mean( (y_pred_nn - y_test_nn).^2 );
fprintf('nn model: relative error on test set: %.6f\n', nn_per_error);
fprintf('nn model: mse on test set: %.6f\n', nn_mse_error);

结果

这里写图片描述

这里写图片描述

参考链接

评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

littletomatodonkey

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值