資源描述:
《BP神經(jīng)網(wǎng)絡預測代碼》由會員上傳分享,免費在線閱讀,更多相關內容在行業(yè)資料-天天文庫。
1、x=[54167???55196???56300???57482???58796???60266???61465???62828???64653???65994???67207???66207???65859???67295???69172???70499???72538???74542???76368???78534???80671???82992???85229???87177???89211????90859????92420????93717????94974????96259????975
2、42????98705???100072???101654???103008???104357???105851???107507???109300???111026???112704???114333???115823???117171???118517???119850???121121???122389???123626???124761???125786???126743???127627???128453???129227???129988???130756???131448???1321
3、29???132802???134480???135030???135770???136460???137510]';%該腳本用來做NAR神經(jīng)網(wǎng)絡預測%作者:Macer程lag=3;???%自回歸階數(shù)iinput=x;%x為原始序列(行向量)n=length(iinput);%準備輸入和輸出數(shù)據(jù)inputs=zeros(lag,n-lag);fori=1:n-lag???inputs(:,i)=iinput(i:i+lag-1)';endtargets=x(lag+1:end);%創(chuàng)建網(wǎng)絡hidde
4、nLayerSize=10;%隱藏層神經(jīng)元個數(shù)net=fitnet(hiddenLayerSize);%避免過擬合,劃分訓練,測試和驗證數(shù)據(jù)的比例net.divideParam.trainRatio=70/100;net.divideParam.valRatio=15/100;net.divideParam.testRatio=15/100;%訓練網(wǎng)絡[net,tr]=train(net,inputs,targets);%%根據(jù)圖表判斷擬合好壞yn=net(inputs);errors=target
5、s-yn;figure,ploterrcorr(errors)?????????????????????%繪制誤差的自相關情況(20lags)figure,parcorr(errors)?????????????????????????%繪制偏相關情況%[h,pValue,stat,cValue]=lbqtest(errors)????????%Ljung-BoxQ檢驗(20lags)figure,plotresponse(con2seq(targets),con2seq(yn))%看預測的趨勢與原
6、趨勢%figure,ploterrhist(errors)?????????????????????%誤差直方圖%figure,plotperform(tr)?????????????????????????%誤差下降線%%下面預測往后預測幾個時間段fn=7;?%預測步數(shù)為fn。f_in=iinput(n-lag+1:end)';f_out=zeros(1,fn);?%預測輸出%多步預測時,用下面的循環(huán)將網(wǎng)絡輸出重新輸入fori=1:fn???f_out(i)=net(f_in);???f_in=[
7、f_in(2:end);f_out(i)];end%畫出預測圖figure,plot(1949:2013,iinput,'b',2013:2020,[iinput(end),f_out],'r')