Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/CrossValidation.cs @ 656

Last change on this file since 656 was 656, checked in by gkronber, 16 years ago

merged changesets r644:647 and r651:655 from the GpPluginsRefactoringBranch back into the trunk (#177)

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.DataAnalysis;
29
30namespace HeuristicLab.GP.Classification {
31  public class CrossValidation : OperatorBase {
32
33    private const string DATASET = "Dataset";
34    private const string NFOLD = "n-Fold";
35    private const string TRAININGSAMPLESSTART = "TrainingSamplesStart";
36    private const string TRAININGSAMPLESEND = "TrainingSamplesEnd";
37    private const string VALIDATIONSAMPLESSTART = "ValidationSamplesStart";
38    private const string VALIDATIONSAMPLESEND = "ValidationSamplesEnd";
39    private const string TESTSAMPLESSTART = "TestSamplesStart";
40    private const string TESTSAMPLESEND = "TestSamplesEnd";
41
42    public override string Description {
43      get { return @"TASK"; }
44    }
45
46    public CrossValidation()
47      : base() {
48      AddVariableInfo(new VariableInfo(DATASET, "The original dataset and the new datasets in the newly created subscopes", typeof(Dataset), VariableKind.In));
49      AddVariableInfo(new VariableInfo(NFOLD, "Number of folds for the cross-validation", typeof(IntData), VariableKind.In));
50      AddVariableInfo(new VariableInfo(TRAININGSAMPLESSTART, "The start of training samples in the original dataset and starts of training samples in the new datasets", typeof(IntData), VariableKind.In | VariableKind.New));
51      AddVariableInfo(new VariableInfo(TRAININGSAMPLESEND, "The end of training samples in the original dataset and ends of training samples in the new datasets", typeof(IntData), VariableKind.In | VariableKind.New));
52      AddVariableInfo(new VariableInfo(VALIDATIONSAMPLESSTART, "The start of validation samples in the original dataset and starts of validation samples in the new datasets", typeof(IntData), VariableKind.In | VariableKind.New));
53      AddVariableInfo(new VariableInfo(VALIDATIONSAMPLESEND, "The end of validation samples in the original dataset and ends of validation samples in the new datasets", typeof(IntData), VariableKind.In | VariableKind.New));
54      AddVariableInfo(new VariableInfo(TESTSAMPLESSTART, "The start of the test samples in the new datasets", typeof(IntData), VariableKind.New));
55      AddVariableInfo(new VariableInfo(TESTSAMPLESEND, "The end of the test samples in the new datasets", typeof(IntData), VariableKind.New));
56    }
57
58    public override IOperation Apply(IScope scope) {
59      Dataset origDataset = GetVariableValue<Dataset>(DATASET, scope, true);
60      int nFolds = GetVariableValue<IntData>(NFOLD, scope, true).Data;
61      int origTrainingSamplesStart = GetVariableValue<IntData>(TRAININGSAMPLESSTART, scope, true).Data;
62      int origTrainingSamplesEnd = GetVariableValue<IntData>(TRAININGSAMPLESEND, scope, true).Data;
63      int origValidationSamplesStart = GetVariableValue<IntData>(VALIDATIONSAMPLESSTART, scope, true).Data;
64      int origValidationSamplesEnd = GetVariableValue<IntData>(VALIDATIONSAMPLESEND, scope, true).Data;
65      int n=origDataset.Rows;
66      int origTrainingSamples = (origTrainingSamplesEnd-origTrainingSamplesStart);
67      int origValidationSamples = (origValidationSamplesEnd-origValidationSamplesStart);
68
69      double percentTrainingSamples = origTrainingSamples / (double)(origValidationSamples + origTrainingSamples);
70      int nTestSamples = n / nFolds;
71
72      int newTrainingSamplesStart = 0;
73      int newTrainingSamplesEnd = (int)((n - nTestSamples) * percentTrainingSamples);
74      int newValidationSamplesStart = newTrainingSamplesEnd;
75      int newValidationSamplesEnd = n - nTestSamples;
76      int newTestSamplesStart = n - nTestSamples;
77      int newTestSamplesEnd = n;
78
79      for(int i = 0; i < nFolds; i++) {
80        Scope childScope = new Scope(i.ToString());
81        Dataset rotatedSet = new Dataset();
82
83        double[] samples = new double[origDataset.Samples.Length];
84        Array.Copy(origDataset.Samples, samples, samples.Length);
85        RotateArray(samples, i * nTestSamples * origDataset.Columns);
86
87        rotatedSet.Rows = origDataset.Rows;
88        rotatedSet.Columns = origDataset.Columns;
89        rotatedSet.Samples = samples;
90        childScope.AddVariable(new Variable(scope.TranslateName(DATASET), rotatedSet));
91        childScope.AddVariable(new Variable(scope.TranslateName(TRAININGSAMPLESSTART), new IntData(newTrainingSamplesStart)));
92        childScope.AddVariable(new Variable(scope.TranslateName(TRAININGSAMPLESEND), new IntData(newTrainingSamplesEnd)));
93        childScope.AddVariable(new Variable(scope.TranslateName(VALIDATIONSAMPLESSTART), new IntData(newValidationSamplesStart)));
94        childScope.AddVariable(new Variable(scope.TranslateName(VALIDATIONSAMPLESEND), new IntData(newValidationSamplesEnd)));
95        childScope.AddVariable(new Variable(scope.TranslateName(TESTSAMPLESSTART), new IntData(newTestSamplesStart)));
96        childScope.AddVariable(new Variable(scope.TranslateName(TESTSAMPLESEND), new IntData(newTestSamplesEnd)));
97
98        scope.AddSubScope(childScope);
99      }
100      return null;
101    }
102
103    private void RotateArray(double[] samples, int p) {
104      Array.Reverse(samples, 0, p);
105      Array.Reverse(samples, p, samples.Length - p);
106      Array.Reverse(samples);
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.