How to estimate by already trained network
    5 views (last 30 days)
  
       Show older comments
    
I utilized ANN-GA to train a network in which X is input and Y is output. Now I want to use the trained network to estimated new Y data (1column in excel) by new X data ( 6 columns in excel). I don't now the code. Please help me to write the code. I also attached the code. 
%% Data Loading
Data = xlsread('pb_2.xlsx');
X = Data(:,1:end-1);
Y = Data(:,end);
DataNum = size(X,1);
InputNum = size(X,2);
OutputNum = size(Y,2);
%% Normalization
MinX = min(X);
MaxX = max(X);
MinY = min(Y);
MaxY = max(Y);
XN = X;
YN = Y;
for ii = 1:InputNum
    XN(:,ii) = Normalize_Fcn(X(:,ii),MinX(ii),MaxX(ii));
end
for ii = 1:OutputNum
    YN(:,ii) = Normalize_Fcn(Y(:,ii),MinY(ii),MaxY(ii));
end
%% Test and Train Data
TrPercent = 80;
TrNum = round(DataNum * TrPercent / 100);
TsNum = DataNum - TrNum;
R = randperm(DataNum);
trIndex = R(1 : TrNum);
tsIndex = R(1+TrNum : end);
Xtr = XN(trIndex,:);
Ytr = YN(trIndex,:);
Xts = XN(tsIndex,:);
Yts = YN(tsIndex,:);
%% Network Structure
pr = [-1 1];
PR = repmat(pr,InputNum,1);
Network = newff(PR,[5 OutputNum],{'tansig' 'tansig'});
%% Training
Network = TrainUsing_GA_Fcn(Network,Xtr,Ytr);
%% Assesment
YtrNet = sim(Network,Xtr')';
YtsNet = sim(Network,Xts')';
MSEtr = mse(YtrNet - Ytr)
MSEts = mse(YtsNet - Yts)
RMSEtr= sqrt(MSEtr)
RMSEts= sqrt(MSEts)
Bias_tr= (sum(Ytr- YtrNet))/ length(Ytr)
Bias_ts= (sum(Yts- YtsNet))/ length(Yts)
SSEtr= sum((Ytr - YtrNet).^2);
SSEts= sum((Yts - YtsNet).^2);
SSTtr= sum((Ytr - mean(Ytr)).^2);
SSTts= sum((Yts - mean(Yts)).^2);
Rsquare_tr= (sum(Ytr .* YtrNet)) / (sqrt(sum(Ytr .^ 2)) * (sum(YtrNet .^ 2)))
Rsquare_ts= (sum(Yts .* YtsNet)) / (sqrt(sum(Yts .^ 2)) * (sum(YtsNet .^ 2)))
0 Comments
Answers (1)
  Purvaja
 on 18 Feb 2025
        I understand that you want to estimate new Y values. As seen from your given code, “YtsNet” variable is storing the estimated results after the model is trained on “Xts” data from “pb_2.xls” sheet, that you loaded initially in the code.   
1. If you wish to store the new estimated values in excel sheet alongside X values, you may try following steps:  
%% Estimated data
YtsNet = sim(Network, Xts')'; 
% Create a column vector of NaNs for all rows  
EstimatedY = nan(DataNum, 1); 
% Insert predictions for the test set rows  
EstimatedY(tsIndex) = YtsNet; 
DataWithEstimatedY = [Data EstimatedY]; 
xlswrite('pb_2_with_estimated.xlsx', DataWithEstimatedY); 
2. If you wish to store the trained model and then load to model to use it on different dataset, you may try following steps: 
%% Training the network  
Network = TrainUsing_GA_Fcn(Network, Xtr, Ytr);  
%% Save the trained network and normalization parameters  
save('trainedModel.mat', 'Network'); 
In another file you can: 
%% Load the trained model and normalization parameters 
load('trainedModel.mat');  
%% Load new input data (assume it has the same number of columns as X) 
NewData = xlsread('new_data.xlsx'); 
%% Predict using the loaded model  
NewY = sim(Network, NewXN')'; 
You can also go through following documentation link to know more about “load” function: 
Hope this helps you! 
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
