Discussion:
Pls Help: ANN for prediction in Matlab
(too old to reply)
QUÂN HỒNG
2013-11-14 17:42:09 UTC
Permalink
Dear all!
I have a question would like request to your kindness.
I am studying ANN for predicting water level in an area.
The data I have just simply like this: I have 6 Columns included
( Date| Rainfall | Min-Temperature| Max-Temperature| Water lever| Solar Radian)and 365 rows (for 1 year observed).

--Question is that I need to predict data for 1 or many year later base on the historical data given (actually I need to have data for ~20 years ago.

I tried some Code of Matlab like Time Series Prediction, but the Matlab could not defined some terms. I am using Matlab 7.8 R2009a. Anyway, I have a code wrote below.
-But here I don't understand which values are Inputs and which are Targets in my data set?
-How to show the graph presents the prediction data?
- Which parameters set for the results getting better?
- When the regression graph appeared after trained, How to know it is good result?

here is my unknowledge code, Pls Fix again help me thanks:
-----------------------------
%NEURAL NETWORK
%Feed-Forward Network
%Reading data from an Excel file
%Using function 'xlsread' to read an Excel file
% For MLP training


%Import Inputs data
Data_Inputs=xlsread('data.xlsx'); % For training data (6 col x 356 row)
Testing_Data=xlsread('data.xlsx'); % For testing data (6 col x 356 row)


%The training data sample are randmonized by using the function'randperm'
Shuffling_Inputs=Data_Inputs(randperm(365),2:6); % integers (training sample) from 2-6 columns
Training_Set=Shuffling_Inputs(1:365,2:5)%specific training set, 2:5 column 2-5
Target_Set=Shuffling_Inputs(1:365,6) %specific target set, 6 is the last columns

Testing_Set=Testing_Data(1:365,2:5)%specific testing set
Testing_Target_Set=Testing_Data(1:365,6)% Specific testing set, target


%Convert data to row vectors
[pn,ps]=mapstd(Training_Set');
[tn,ts]=mapstd(Target_Set');

%pn and tn contain normalized values of inputs and output respectively
%ps and ts contain the mean and standard deviations of the orginal inputs
%and targets

%Creates a Feed-Forward Back-propagation network, using function'newff'
net=newff(pn,tn,[4,100],{'logsig','purelin'});

%pn and tn are normalized input and output.
%[4] implies that the network structure consists of one hidden layer and
%'4' neurons.
%For '100' hiddend layers we use [4,100]
%{tf} denotes the transfer function of the i layers
%'tagsig','purelin','logsig'

%Configured network
net.trainFcn='trainlm';
net.trainparam.min_grad=0.00000001;
net.trainParam.epochs=1000;
net.trainParam.lr=0.4;
net.trainParam.max_fail=20;


%'trainFcn': defined the function used to train the network. It can be set
%the name of any training function, for example LM='trainlm';%
%:Lavenberg-Marquardt back-propagation
%'trainparam.min_grad': denotes the minimum performance gradient
%'trainParam.epochs': denotes the maximum number of epochs to train
%'trainParam.lr': denotes the learning rates
%'trainParam.max_fail': denotes the maximum validation failures

%-------------------------------------------------
%For RBF training
%Use the function 'newrb' to creating an RBF network
%net=newrb(pn,tn,0.01,50,200,50);
%where
%pn and tn are input and target, respectively.
%'goal': detones the mean square error goal, it set here tobe 0.01
%'spread': presents the spread of radial basis function, changed between
%1-60 and donoted here is 'i'
%'mn': maximum number of neurons to add between 5 and 600 and denoted here
%'j'.
%'df': represents the number of neurons to add between displays and it is
%set to 50
%-------------------------------------------------


% MLP network trained using the 'trainFcn' and 'trainParam' train function
% MLP network trainning
[net,tr]=train(net,pn,tn);
%save(NetworkName,'net'); % saved in to the MATLAB, NetworkName can use set depending the designers

%Simulate Network
Outputs=sim(net,pn); % Simulate network

%Created a Progression
plotperf(tr)
plotfit(net,pn,tn)
plotregression(tn,Outputs); % plot trainning data
Greg Heath
2013-11-15 23:51:05 UTC
Permalink
Post by QUÂN HỒNG
Dear all!
I have a question would like request to your kindness.
I am studying ANN for predicting water level in an area.
The data I have just simply like this: I have 6 Columns included
( Date| Rainfall | Min-Temperature| Max-Temperature| Water lever| Solar Radian)and 365 rows (for 1 year observed).
--Question is that I need to predict data for 1 or many year later base on the historical data given (actually I need to have data for ~20 years ago.
I tried some Code of Matlab like Time Series Prediction, but the Matlab could not defined some terms. I am using Matlab 7.8 R2009a. Anyway, I have a code wrote below.
-But here I don't understand which values are Inputs and which are Targets in my data set?
-How to show the graph presents the prediction data?
- Which parameters set for the results getting better?
- When the regression graph appeared after trained, How to know it is good result?
-----------------------------
%NEURAL NETWORK
%Feed-Forward Network
%Reading data from an Excel file
%Using function 'xlsread' to read an Excel file
% For MLP training
%Import Inputs data
Data_Inputs=xlsread('data.xlsx'); % For training data (6 col x 356 row)
Testing_Data=xlsread('data.xlsx'); % For testing data (6 col x 356 row)
%The training data sample are randmonized by using the function'randperm'
Shuffling_Inputs=Data_Inputs(randperm(365),2:6); % integers (training sample) from 2-6 columns
Training_Set=Shuffling_Inputs(1:365,2:5)%specific training set, 2:5 column 2-5
Target_Set=Shuffling_Inputs(1:365,6) %specific target set, 6 is the last columns
Testing_Set=Testing_Data(1:365,2:5)%specific testing set
Testing_Target_Set=Testing_Data(1:365,6)% Specific testing set, target
%Convert data to row vectors
[pn,ps]=mapstd(Training_Set');
[tn,ts]=mapstd(Target_Set');
%pn and tn contain normalized values of inputs and output respectively
%ps and ts contain the mean and standard deviations of the orginal inputs
%and targets
%Creates a Feed-Forward Back-propagation network, using function'newff'
net=newff(pn,tn,[4,100],{'logsig','purelin'});
%pn and tn are normalized input and output.
%[4] implies that the network structure consists of one hidden layer and
%'4' neurons.
%For '100' hiddend layers we use [4,100]
%{tf} denotes the transfer function of the i layers
%'tagsig','purelin','logsig'
%Configured network
net.trainFcn='trainlm';
net.trainparam.min_grad=0.00000001;
net.trainParam.epochs=1000;
net.trainParam.lr=0.4;
net.trainParam.max_fail=20;
%'trainFcn': defined the function used to train the network. It can be set
%the name of any training function, for example LM='trainlm';%
%:Lavenberg-Marquardt back-propagation
%'trainparam.min_grad': denotes the minimum performance gradient
%'trainParam.epochs': denotes the maximum number of epochs to train
%'trainParam.lr': denotes the learning rates
%'trainParam.max_fail': denotes the maximum validation failures
%-------------------------------------------------
%For RBF training
%Use the function 'newrb' to creating an RBF network
%net=newrb(pn,tn,0.01,50,200,50);
%where
%pn and tn are input and target, respectively.
%'goal': detones the mean square error goal, it set here tobe 0.01
%'spread': presents the spread of radial basis function, changed between
%1-60 and donoted here is 'i'
%'mn': maximum number of neurons to add between 5 and 600 and denoted here
%'j'.
%'df': represents the number of neurons to add between displays and it is
%set to 50
%-------------------------------------------------
% MLP network trained using the 'trainFcn' and 'trainParam' train function
% MLP network trainning
[net,tr]=train(net,pn,tn);
%save(NetworkName,'net'); % saved in to the MATLAB, NetworkName can use set depending the designers
%Simulate Network
Outputs=sim(net,pn); % Simulate network
%Created a Progression
plotperf(tr)
plotfit(net,pn,tn)
plotregression(tn,Outputs); % plot trainning data
You have too many errors.
Reread the newff documentation to better understand the input syntax;
MATLAB has examples that you should try before trying to write your own
Do not include statements that specify defaults

help newff

help nndatasests

help greg newff

Choose one that is that is the same type as your problem. I will compare results with mine.

Hope this helps.

Greg
Greg Heath
2013-11-16 04:26:06 UTC
Permalink
% Thread Subject: Pls Help: ANN for prediction in Matlab
% Subject: Pls Help: ANN for prediction in Matlab
% From: QUÂN HNG
% Date: 14 Nov, 2013 17:42:09

Dear all!
I have a question would like request to your kindness.
I am studying ANN for predicting water level in an area.
The data I have just simply like this: I have 6 Columns included
( Date| Rainfall | Min-Temperature| Max-Temperature| Water lever| Solar Radian)and 365 rows (for 1 year observed).

%% Transpose input Matrix ASAP

--Question is that I need to predict data for 1 or many year later base on the historical data given (actually I need to
have data for ~20 years ago.

%% This is obvoiusly a time series problem.

I tried some Code of Matlab like Time Series Prediction, but the Matlab could not defined some terms.

%% Why didn't you post the code with comments and error messages?

I am using Matlab 7.8 R2009a. Anyway, I have a code wrote below.

-But here I don't understand which values are Inputs and which are Targets in my data set?

%% Output is water level; Inputs are everything else

-How to show the graph presents the prediction data?

%% Plot target and output (different colors) vs time

- Which parameters set for the results getting better?

%% Input and feedback lags
%% Number of hidden nodes

- When the regression graph appeared after trained, How to know it is good result?

% % If the normalized MSE is << 1; for example NMSE <= 1/100
% % or, equivalently, if the coefficient of determination, R^2 >= 0.99
% %
% % e = t-y;
% % MSE00 = mean(var(t',1)) % also mse( t - repmat(mean(t,2), 1, N) )
% % MSE = mean(var(e';1) % also mse( t -y )
% % NMSE = MSE/MSE00
% % R2 = 1-MSE
% % The plots of the y vs t points should be clustered about the 45 degree line.

here is my unknowledge code, Pls Fix again help me thanks:
-----------------------------
%NEURAL NETWORK
%Feed-Forward Network
%Reading data from an Excel file
%Using function 'xlsread' to read an Excel file
% For MLP training


%Import Inputs data
Data_Inputs = xlsread('data.xlsx'); % For training data (6 col x 356 row)
Testing_Data = xlsread('data.xlsx'); % For testing data (6 col x 356 row)


%The training data sample are randmonized by using the function'randperm'
Shuffling_Inputs = Data_Inputs(randperm(365),2:6); % integers (training sample) from 2-6 columns
Training_Set = Shuffling_Inputs(1:365,2:5)%specific training set, 2:5 column 2-5
Target_Set = Shuffling_Inputs(1:365,6) %specific target set, 6 is the last columns

% % INCORRECT: Input 5 is water level, Input 6 is solar radiation
%% INCLUDE Input1

Testing_Set = Testing_Data(1:365,2:5)%specific testing set
Testing_Target_Set = Testing_Data(1:365,6)% Specific testing set, target

% % Unnecessary. This the same as the training Data


%Convert data to row vectors
[pn,ps]=mapstd(Training_Set');
[tn,ts]=mapstd(Target_Set');

%pn and tn contain normalized values of inputs and output respectively
%ps and ts contain the mean and standard deviations of the orginal inputs
%and targets

%Creates a Feed-Forward Back-propagation network, using function'newff'
net=newff(pn,tn,[4,100],{'logsig','purelin'});

%% Incorrect. For I-H-O net
%% Neq = N*O No. of training equations
%% Nw = (I+1)*H+(H+1)*O No. of unknown weights

[ I N ] = size(pn) % [ 5 356 ]
[O N ] = size(tn) % [ 1 356 ]

%% No overfitting Neq > Nw when H <= Hub (upper bound)

Hub = -1 + ceil( (N*O-O) / (I+O+1) ) % 51

%% First try Hmin=0, dH =1, Hmax = round(Hub/10) = 5
%% If unsatisfctory, increase H and either use a validation stopping subset
%% or use TRAINBR

%pn and tn are normalized input and output.
%[4] implies that the network structure consists of one hidden layer and
%'4' neurons.
%For '100' hiddend layers we use [4,100]

%% NO, NO, NO. For one hidden layer with H nodes

net=newff( pn, tn, [ H, O ], {'tansig','purelin'});

%% or, using defaults

net=newff( pn, tn, [ H, O ]);

%{tf} denotes the transfer function of the i layers
%'tagsig','purelin','logsig'

%Configured network
net.trainFcn='trainlm';
net.trainparam.min_grad=0.00000001;
net.trainParam.epochs=1000;
net.trainParam.lr=0.4;
net.trainParam.max_fail=20;

%% Delete above 5 specificatins and use defaults. Didn't you see the example
%%
%% help newff

%'trainFcn': defined the function used to train the network. It can be set
%the name of any training function, for example LM='trainlm';%
%:Lavenberg-Marquardt back-propagation
%'trainparam.min_grad': denotes the minimum performance gradient
%'trainParam.epochs': denotes the maximum number of epochs to train
%'trainParam.lr': denotes the learning rates
%'trainParam.max_fail': denotes the maximum validation failures

%%Again, just use the defaults

%-------------------------------------------------
%For RBF training
%Use the function 'newrb' to creating an RBF network
%net=newrb(pn,tn,0.01,50,200,50);
%where
%pn and tn are input and target, respectively.
%'goal': detones the mean square error goal, it set here tobe 0.01
%'spread': presents the spread of radial basis function, changed between
%1-60 and donoted here is 'i'
%'mn': maximum number of neurons to add between 5 and 600 and denoted here
%'j'.
%'df': represents the number of neurons to add between displays and it is
%set to 50
%-------------------------------------------------

%%No, No, No. Search on greg newrb

% MLP network trained using the 'trainFcn' and 'trainParam' train function
% MLP network trainning
[net,tr]=train(net,pn,tn);
%save(NetworkName,'net'); % saved in to the MATLAB, NetworkName can use set depending the designers

%Simulate Network
Outputs=sim(net,pn); % Simulate network

%% error = targets - Outputs;
%
%% See above for NMSE and R2

%Created a Progression

%% You mean a Regression. See wikipeda for the difference

plotperf(tr)
plotfit(net,pn,tn)
plotregression(tn,Outputs); % plot trainning data

%% Unnecessary; These plot are defaults
QUÂN HỒNG
2013-11-16 08:30:52 UTC
Permalink
Thank you Greg Heath so much.
Can you please send for me a full code example, sorry for less understanding. I am also just starting with ANN and mathlab. Thank you Sir! Also wanted to know why we need to training and testing ... and what are they connection to prediction data. And wanted to show the graph as an example. Thanks
Greg Heath
2013-11-16 21:35:07 UTC
Permalink
Post by QUÂN HỒNG
Thank you Greg Heath so much.
Can you please send for me a full code example, sorry for less understanding. I am also just starting with ANN and mathlab. Thank you Sir! Also wanted to know why we need to training and testing ... and what are they connection to prediction data. And wanted to show the graph as an example. Thanks
1. The code you have written is not for prediction. The prediction function you need is narxnet
2. Read the help file and try the example
help narxnet
3. Search NEWGROUP and ANSWERS for more complete code
greg narxnet
4. Try some of the the narxnet prediction examples in the nndataset file
help nndatasets
5. search for my data division explanation
greg data division
http://www.mathworks.com/matlabcentral/newsreader/view_thread/331993#912331


Hope this helps.

Greg
QUÂN HỒNG
2013-11-18 04:21:16 UTC
Permalink
Dear Greg ! Can you please send me a full example starting from import data to showing the graph? Actually I am not study ANN before, I just used it to be a part in my research. Even I try to learn it so much but in fact to say i am blanking knowledge about ANN and Matlab.I will be happy if u can help me like that. Best regards!
RATAN THAKUR
2022-05-01 13:56:42 UTC
Permalink
Post by QUÂN HỒNG
Thank you Greg Heath so much.
Can you please send for me a full code example, sorry for less understanding. I am also just starting with ANN and mathlab. Thank you Sir! Also wanted to know why we need to training and testing ... and what are they connection to prediction data. And wanted to show the graph as an example. Thanks
Respected Sir,
with due respect I want to inform you that currently I am working on a Radial basis neural network for prediction purposes on matlab software. Sir, I have 7 input data arranged in 7 columns and 1 target data arranged in 1 column. Sir I want a predicted value using a radial basis neural network with 7 hidden neurons. Sir, kindly help me by providing the matlab code for the same to get the predicted value of target value with Mean square error, R squared and plot between actual and predicted target.
I shall be highly obliged to you for this act of kindness.

Loading...