timeseries forecasting for 10 years ahead ANN

3 ビュー (過去 30 日間)
Nur Farahin Che Nordin
Nur Farahin Che Nordin 2025 年 1 月 15 日
編集済み: Nur Farahin Che Nordin 2025 年 1 月 16 日
Hi,
i would like to forecast the Cl parameter for 10 years ahead. But keep getting error : Index in position 2 exceeds array bounds. Index must not exceed 1. by the way , when running the code, i have remove the heading, words from the excel. but still getting the error
Error in preparets (line 317)
xi = xx(:,FBS+((1-net.numInputDelays):0));
Error in timee (line 25)
[X,inputStates,layerStates,T] = preparets(net,inputSeries,{},targetSeries);
Please help to check which part that i did wrong.
data = readmatrix ("104CPr.csv");
Input = data(:,1:(end-1));
Target = data(:,end);
X = con2seq(Input)';
T = con2seq(Target)';
%% Data Preparation
N = 10; % Multi-step ahead prediction
% Input and target series are divided in two groups of data:
% 1st group: used to train the network
inputSeries = X(1:end-N);
targetSeries = T(1:end-N);
%% Network architecture
delay = 2;
inputDelays = 1:2;
feedbackDelays = 1:2;
hiddenLayerSize = 10;
trainFcn = 'trainbr';
% Network Creation
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize,'open', trainFcn);
[X,inputStates,layerStates,T] = preparets(net,inputSeries,{},targetSeries);
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
[net,Y] = train(net,X,T,inputStates,layerStates);
outputs = net(X,inputStates,layerStates);
errors = gsubtract(T,Y);
performance = perform(net,T,Y);
view(net)
delay = 2;
netc = closeloop(net);
netc.name = [net.name ' - Closed Loop'];
view(netc)
[xc,xic,aic,tc] = preparets(netc,inputSeries,{},targetSeries);
yc = netc(xc,xic,aic);
closedLoopPerformance = perform(netc,tc,yc);
% Early Prediction Network
nets = removedelay(net);
nets.name = [net.name ' - Predict One Step Ahead'];
view(nets)
[xs,xis,ais,ts] = preparets(nets,inputSeries,{},targetSeries);
ys = nets(xs,xis,ais);
earlyPredictPerformance = perform(nets,ts,ys);
%%5. Multi-step ahead prediction
inputSeriesPred = [inputSeries(end-delay+1:end),inputSeriesVal];
targetSeriesPred = [targetSeries(end-delay+1:end), con2seq(nan(1,N))];
[Xs,Xi,Ai,Ts] = preparets(netc,inputSeriesPred,{},targetSeriesPred);
yPred = netc(Xs,Xi,Ai);
perf = perform(net,yPred,targetSeriesVal);
figure;
plot([cell2mat(targetSeries),nan(1,N);
nan(1,length(targetSeries)),cell2mat(yPred);
nan(1,length(targetSeries)),cell2mat(targetSeriesVal)]')
legend('Original Targets','Network Predictions','Expected Outputs');

回答 (1 件)

Sandeep Mishra
Sandeep Mishra 2025 年 1 月 15 日
Hi Nur,
Upon debugging the code snippet, I realised that the variable 'X' is a 7*1 cell array while the variable 'T' is 1*1 cell array.
The root cause of the issue arises due to the variable ‘N’ (N=10) used for computing 'inputSeries' and 'targetSeries', which exceeds the sizes of variable 'X' and 'T', resulting in empty arrays (0x1 and 1x0).
To resolve the issue, you can adjust the value of ‘N’ so that 'inputSeries' and 'targetSeries' are appropriately sized.
For a detailed example on using the 'preparets' function, you can refer to the following MathWorks documentation: https://www.mathworks.com/help/releases/R2024b/deeplearning/ref/preparets.html#f8-3421485
I hope this helps!
  4 件のコメント
Nur Farahin Che Nordin
Nur Farahin Che Nordin 2025 年 1 月 16 日
i have change to 2, still getting the same error.
then, i tried to use this code instead of previous code
N = 10; % Multi-step ahead prediction
% Input and target series are divided in two groups of data:
% 1st group: used to train the network
i = N+1: length (data)
inputSeries = [X;data(i-N:i-1)];
targetSeries = [T;data(i)];
now, im getting the error Error using preparets
Inputs{1,8} and Inputs{1,1} have different numbers of rows.
Error in timee (line 26)
[X,inputStates,layerStates,T] = preparets(net,inputSeries,{},targetSeries);
please help, im drained and lost right now
Nur Farahin Che Nordin
Nur Farahin Che Nordin 2025 年 1 月 16 日
編集済み: Nur Farahin Che Nordin 2025 年 1 月 16 日
i tried changing to simple code as below but still not working, the code stucked half way with error Error using network/sim
Input data sizes do not match net.inputs{1}.size.
Error in indexing (line 15)
otherwise, v = sim(vin,subs{:});
Error in timeseriess (line 24)
forecast(i) = net(lastValue);
data = readmatrix ("104CPr.csv");
% Define the number of years to forecast
forecastYears = 10;
% Prepare the data for training
X = data(1:end-1); % Inputs
T = data(2:end); % Targets
% Create and configure the neural network
net = feedforwardnet(10); % 10 hidden neurons
net = configure(net, X', T');
% Train the neural network
[net, tr] = train(net, X', T');
% Forecast the next 10 years
forecast = zeros(1, forecastYears);
lastValue = data(end);
for i = 1:forecastYears
forecast(i) = net(lastValue);
lastValue = forecast(i);
end

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by