forked from Electromaxim/electricity_load_forecast
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trainAndTestNN.m
32 lines (25 loc) · 996 Bytes
/
trainAndTestNN.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function [errors, net] = trainAndTestNN(trainX, trainY, testX, testY, ind, net, baselineErr)
% Train and test a neural network for a given set of training and test
% data. The MAE metric is used to train a feed forward neural network with
% 20 neurons in the hidden layer. The mean absolute error is also reported
% for the test set
if nargin < 5
ind = 1:size(trainX,2);
numAllPred = 14;
else
assert(max(ind) <= size(trainX,2));
numAllPred = size(trainX,2);
end
if nargin < 6 || isempty(net)
net = newfit(trainX(:,ind)', trainY', 20);
net.performFcn = 'mae';
net = train(net, trainX(:,ind)', trainY');
end
predY = sim(net, testX(:,ind)')';
err = testY - predY;
errpct = abs(err)./testY*100;
errors = [mean(abs(err)) mean(errpct)];
if nargin > 6
fprintf('\nBaseline error with %2d predictors: MAE = $%0.2f, MAPE = %0.2f%%\n', numAllPred, baselineErr);
end
fprintf('Error with selected %2d predictors: MAE = $%0.2f, MAPE = %0.2f%%\n\n', length(ind), errors);