How to reach train and test and their predictions in nftool?

15 views (last 30 days)
I have conducted neural network on my dataset with 228 rows and 7 columns but don't know how to obtain my training and testing datasets and their prediction values. I want to export this values.
% Solve an Input-Output Fitting problem with a Neural Network
% Script generated by Neural Fitting app
% Created 20-Jul-2024 16:31:01
%
% This script assumes these variables are defined:
%
% data - input data.
% data_1 - target data.
x = data';
t = data_1';
% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.
% 'trainbr' takes longer but may be better for challenging problems.
% 'trainscg' uses less memory. Suitable in low memory situations.
trainFcn = 'trainlm'; % Levenberg-Marquardt backpropagation.
% Create a Fitting Network
hiddenLayerSize = 4;
net = fitnet(hiddenLayerSize,trainFcn);
% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Train the Network
[net,tr] = train(net,x,t);
% Test the Network
y = net(x);
e = gsubtract(t,y);
performance = perform(net,t,y)
% View the Network
view(net)
% Plots
% Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, ploterrhist(e)
%figure, plotregression(t,y)
%figure, plotfit(net,x,t)

Accepted Answer

Muskan
Muskan on 22 Jul 2024
Edited: Muskan on 22 Jul 2024
Hi,
As per my understanding you can follow the following steps to obtain the training and testing datasets along with their prediction values:
  1. Extract Training and Testing Indices: Use the "tr" structure returned by the "train" function to get the indices of the training, validation, and testing datasets.
  2. Export Data and Predictions: Use these indices to extract the corresponding data and predictions, then save them to a file.
Here's how you can modify your script to achieve this:
% Extract Training, Validation, and Testing Indices
trainInd = tr.trainInd;
valInd = tr.valInd;
testInd = tr.testInd;
% Extract Training, Validation, and Testing Data
x_train = x(:, trainInd);
t_train = t(:, trainInd);
y_train = y(:, trainInd);
x_val = x(:, valInd);
t_val = t(:, valInd);
y_val = y(:, valInd);
x_test = x(:, testInd);
t_test = t(:, testInd);
y_test = y(:, testInd);
% Export Data and Predictions
train_data = [x_train' t_train' y_train'];
val_data = [x_val' t_val' y_val'];
test_data = [x_test' t_test' y_test'];
After this, if you want to save the results in any file, you can use the "writematrix" function. Kindly refer to the documentation of "writematrix" for more information: https://www.mathworks.com/help/matlab/ref/writematrix.html

More Answers (0)

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!