神经网络预测时间序列示例(水痘模型)
clc,clear;
close all;
%% Importing Data and plotting
data=chickenpox_dataset;
data=[data{:}];
figure
plot(data)
xlabel("Month")
ylabel("Cases")
title("Monthy Cases of Chickenpox")
%% Converting matrix to cell
[t1,t2]=size(data);
Target=mat2cell(data,t1,ones(1,t2));
T=Target;
%% Creating open loop network
trainFcn='trainbr';
feedbackdelays=1:2;
hiddenLayerSize=10;
net=narnet(feedbackdelays,hiddenLayerSize,'open',trainFcn);
[x,xi,ai,t]=preparets(net,{},{},T);
net.divideFcn='divideind';
net.divideMode='time';
net.divideParam.trainInd=1:400;
net.divideParam.valInd=401:450;
net.divideParam.testInd=451:498;
net.performFcn='mse';
[net,tr]=train(net,x,t,xi,ai);
y=net(x,xi,ai);
%% Calculating performance
trainTargets=gmultiply(t,tr.trainMask);
valTargets=gmultiply(t,tr.valMask);
testTargets=gmultiply(t,tr.testMask);
trainPerformance=perform(net,trainTargets,y)
valPerformance=perform(net,valTargets,y)
testPerformance=perform(net,testTargets,y)
%% Closing the network
view(net)
netc=closeloop(net);
netc.name=[net.name '- Closed Loop'];
view(netc)
[xc,xic,aic,tc]=preparets(netc,{},{},T);
yc=netc(xc,xic,aic);
closedLoopPerformance=perform(net,tc,yc)
%% Multi step prediction
[x1,xio,aio,t]=preparets(net,{},{},T);
[y1,xfo,afo]=net(x1,xio,aio);
[netc,xic,aic]=closeloop(net,xfo,afo);
[y2,xfc,afc]=netc(cell(0,20),xic,aic);
y1_1=cell2mat(y1);
y2_1=cell2mat(y2);
plot(1:496,y1_1,'-b');
hold on
plot(496:515,y2_1,'-k');
%% Step-Ahead prediction
nets=removedelay(net);
nets.name=[net.name '- Predict One Step Ahead'];
view(nets)
[xs,xis,ais,ts]=preparets(nets,{},{},T);
ys=nets(xs,xis,ais);
stepAheadPerformance=perform(nets,ts,ys)
|