1 ## Copyright (C) 2009 Luiz Angelo Daros de Luca <luizluca@gmail.com>
4 ## This program is free software; you can redistribute it and/or modify it
5 ## under the terms of the GNU General Public License as published by
6 ## the Free Software Foundation; either version 2, or (at your option)
9 ## This program is distributed in the hope that it will be useful, but
10 ## WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ## General Public License for more details.
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program; see the file COPYING. If not, see
16 ## <http://www.gnu.org/licenses/>.
19 ## @deftypefn {Function File} [@var{trainVectors},@var{validationVectors},@var{testVectors},@var{indexOfTrain},@var{indexOfValidation},@var{indexOfTest}] = dividerand (@var{allCases},@var{trainRatio},@var{valRatio},@var{testRatio})
20 ## Divide the vectors in training, validation and test group according to
21 ## the informed ratios
26 ## [trainVectors,validationVectors,testVectors,indexOfTrain,indexOfValidatio
27 ## n,indexOfTest] = dividerand(allCases,trainRatio,valRatio,testRatio)
29 ## The ratios are normalized. This way:
31 ## dividerand(xx,1,2,3) == dividerand(xx,10,20,30)
38 function [trainVectors,validationVectors,testVectors,indexOfTrain,indexOfValidation,indexOfTest] = dividerand(allCases,trainRatio,valRatio,testRatio)
40 # Divide the vectors in training, validation and test group according to
43 # [trainVectors,validationVectors,testVectors,indexOfTrain,indexOfValidatio
44 # n,indexOfTest] = dividerand(allCases,trainRatio,valRatio,testRatio)
46 # The ratios are normalized. This way:
48 # dividerand(xx,1,2,3) == dividerand(xx,10,20,30)
52 total = trainRatio + valRatio + testRatio;
53 #trainRatio = trainRatio / total; not used
54 validationRatio = valRatio / total;
55 testRatio = testRatio / total;
57 ## Calculate the number of cases for each type
58 numerOfCases = size(allCases,2);
59 numberOfValidation = floor(validationRatio*numerOfCases);
60 numberOfTest = floor(testRatio*numerOfCases);
61 numberOfTrain = numerOfCases - numberOfValidation - numberOfTest;
64 indexOfAll=randperm(numerOfCases);
65 indexOfValidation=sort(indexOfAll(1:numberOfValidation));
66 indexOfTest=sort(indexOfAll((1:numberOfTest)+numberOfValidation));
67 indexOfTrain=sort(indexOfAll((1:numberOfTrain)+numberOfTest+numberOfValidation));
70 trainVectors = allCases(:,indexOfTrain);
71 testVectors = allCases(:,indexOfTest);
72 validationVectors = allCases(:,indexOfValidation);