MATLAB Answers

0

Unknown Future Prediction by Using ANN

Hi, I am trying to predict future consumption values (2018-2038). But something is wrong with my code I guess. This is a time series of consumption. I can fit real samples and predicted samples with a high score (like %99). But when I come to the end (predict part) after nearly 2 or 4 steps ahead values start to being same. I think it is about normalization, becouse consumption have to increase because of its past and the maximum limit is 1 so values cant go further and it stays under 1. when I use another code for normalization like Xn=normalize(Xn) network cant find any correlation (NaN).
Apart from these when I normalize by using this way how can I denormalize the samples. what will be the maximum and minimum values after training. I am writing the code below. Thanks in advance.
xv=load('elektriktek.txt');
xn=xv';
xn=(xn-min(xn))/((max(xn)-min(xn)));
figure,plot(xn)
data=[xn(1,1:end-10);xn(1,2:end-9);xn(1,3:end-8);xn(1,4:end-7);xn(1,5:end-6);xn(1,6:end-5);xn(1,7:end-4);xn(1,8:end-3);xn(1,9:end-2);xn(1,10:end-1)];%%%%5 giriþ 1 ileri tahmin
groups=xn(1,11:end);
data=data';
groups=groups';
indices = crossvalind('Kfold',length(data),3);%%%%
TestMSE_toplam=zeros(3,1);
TestMAE_toplam=zeros(3,1);
for i = 1:3
testy = (indices == i);
testy=testy;
trainy = (~testy);
trainy=trainy;
TestInputData=data(testy,:)';
TrainInputData=data(trainy,:)';
TestOutputData=groups(testy,:)';
TrainOutputData=groups(trainy,:)';
Tahmin=zeros(size(TestOutputData));
Ref=zeros(size(TestOutputData));
for m=1:10
rand('seed',1);
randn('seed',1);
net=newff(minmax(TrainInputData(:,:)),[m 1],{'tansig','tansig','purelin'},'trainlm');
net.trainParam.epochs=300;
net.trainParam.show=20;
%net.trainparam.lr=0.01;
net.trainparam.goal=0.00001;
[net,tr]=train(net,TrainInputData,TrainOutputData);
TrainOutput=sim(net,TrainInputData);
TrainMSE(m,1)=mse(TrainOutputData-TrainOutput);
TrainMAE(m,1)=mae(TrainOutputData-TrainOutput);
TrainReport=[TrainOutput'];
TestOutput=sim(net,TestInputData);
TestMSE(m,1)=mse(TestOutputData-TestOutput);
TestMAE(m,1)=mae(TestOutputData-TestOutput);
traintime(m,1)=sum(tr.time);
end;
[a2,b2]=find(TestMSE==min(min(TestMSE)));
[ax,bx]=find(a2==min(a2))
a2=a2(ax,bx)
b2=b2(ax,bx)
axx(i,1)=a2;
bxx(i,1)=b2;
TestMSE_son(i,1)=TestMSE(a2,b2);
TestMAE_son(i,1)=TestMAE(a2,b2);
traintime_son(i,1)=traintime(a2,b2);
%figure,plot(TestOutputData,'O'); hold on; plot(TestOutput,'*'); hold off
rand('seed',1);
randn('seed',1);
net=newff(minmax(TrainInputData(:,:)),[a2 1],{'tansig','tansig','purelin'},'trainlm');
net.trainParam.epochs=500;
net.trainParam.show=20;
%net.trainparam.lr=0.01;
net.trainparam.goal=0.00001;
[net,tr]=train(net,TrainInputData,TrainOutputData);
TestOutput=sim(net,TestInputData);
TestMSE_son(i,1)=mse(TestOutputData-TestOutput)
TestMAE_son(i,1)=mae(TestOutputData-TestOutput)
TrainMSE_son(i,1)=mse(TrainOutputData-TrainOutput)
TrainMAE_son(i,1)=mae(TrainOutputData-TrainOutput)
R2 = corrcoef(TestOutputData,TestOutput);
R22(i,1)= R2(1,2)^2
[m1,n1,r]=postreg(TestOutputData,TestOutput);
Reg(i,1)=r;
figure,plot(TestOutputData,'-O'); hold on; plot(TestOutput,'-*'); hold off
Tahmin=Tahmin+TestOutput;
Ref=Ref+TestOutputData;
end;
Tahmin_Test=Tahmin/3;
Ref_Data=Ref/3;
%%%%%%%%%%%%%%%%%%
TestMSE=(sum(TestMSE_son))/3
TestMAE=(sum(TestMAE_son))/3
TrainMSE=(sum(TrainMSE_son))/3
TrainMAE=(sum(TrainMAE_son))/3
Corr_R=(sum(R22))/3
Reg_R=sum((Reg))/3
figure,plot(Ref_Data);hold on; plot(Tahmin_Test,'r')
fid1 = fopen('hatalar_ysa.txt','w+');
fprintf(fid1,'TestMSE = %12.6f\n',TestMSE);
fprintf(fid1,'TestMAE = %12.6f\n',TestMAE);
fprintf(fid1,'TrainMSE = %12.6f\n',TrainMSE);
fprintf(fid1,'Corr_R = %12.6f\n',Corr_R);
fprintf(fid1,'Reg_R = %12.6f\n',Reg_R);
miktar=20 %miktar means number of step
tahmin=zeros(miktar,1); %Tahmin means prediction
test1=[xn(1,end-9);xn(1,end-8);xn(1,end-7);xn(1,end-6);xn(1,end-5);xn(1,end-4);xn(1,end-3);xn(1,end-2);xn(1,end-1);xn(1,end)];
for i=1:miktar;
tahmin(i,1)=sim(net,test1);
testn(1,1)=test1(2,1);
testn(2,1)=test1(3,1);
testn(3,1)=test1(4,1);
testn(4,1)=test1(5,1);
testn(5,1)=test1(6,1);
testn(6,1)=test1(7,1);
testn(7,1)=test1(8,1);
testn(8,1)=test1(9,1);
testn(9,1)=test1(10,1);
testn(10,1)=tahmin(i,1);
test1=testn
end;
figure,plot(tahmin)

  0 Comments

Sign in to comment.

1 Answer

Answer by BERGHOUT Tarek on 16 Apr 2019
 Accepted Answer

I attached this file ,use this function it is great for norlmalization, but befor you normalize your data between 0 and 1 , i mean befor training try to do that:
1, load you data
2 save the bellow parameters because you will need them later in denormalizing :
- if your data is a matrix then Xmin=(min(min(data)));Xmax=(max(max(data)));
- if your data is a vector just use one max and one min;
3- normalize your data between 0 and 1;
4- train your data and get your outputs,
5- denormalize your data by: output=scaledata(output,Xmin,Xmax);
please accept my answer if it is helpful.

  1 Comment

Thank you,I put the code after loading data and I used both way for matrix and vextor. but I couldnt get correlation by using both of them
I wrote it like below;
Xmin=(min(min(xn)));Xmax=(max(max(xn)))
normalize2.png

Sign in to comment.