Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.StructureIdentification/CrossValidation.cs @ 10145

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

fixed a bug in the cross-validation operator #259

File size: 6.1 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.Operators;
29using HeuristicLab.DataAnalysis;
30
31namespace HeuristicLab.StructureIdentification {
32  public class CrossValidation : OperatorBase {
33
34    private const string DATASET = "Dataset";
35    private const string NFOLD = "n-Fold";
36    private const string TRAININGSAMPLESSTART = "TrainingSamplesStart";
37    private const string TRAININGSAMPLESEND = "TrainingSamplesEnd";
38    private const string VALIDATIONSAMPLESSTART = "ValidationSamplesStart";
39    private const string VALIDATIONSAMPLESEND = "ValidationSamplesEnd";
40    private const string TESTSAMPLESSTART = "TestSamplesStart";
41    private const string TESTSAMPLESEND = "TestSamplesEnd";
42
43    public override string Description {
44      get { return @"TASK"; }
45    }
46
47    public CrossValidation()
48      : base() {
49      AddVariableInfo(new VariableInfo(DATASET, "The original dataset and the new datasets in the newly created subscopes", typeof(Dataset), VariableKind.In));
50      AddVariableInfo(new VariableInfo(NFOLD, "Number of folds for the cross-validation", typeof(IntData), VariableKind.In));
51      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));
52      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));
53      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));
54      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));
55      AddVariableInfo(new VariableInfo(TESTSAMPLESSTART, "The start of the test samples in the new datasets", typeof(IntData), VariableKind.New));
56      AddVariableInfo(new VariableInfo(TESTSAMPLESEND, "The end of the test samples in the new datasets", typeof(IntData), VariableKind.New));
57    }
58
59    public override IOperation Apply(IScope scope) {
60      Dataset origDataset = GetVariableValue<Dataset>(DATASET, scope, true);
61      int nFolds = GetVariableValue<IntData>(NFOLD, scope, true).Data;
62      int origTrainingSamplesStart = GetVariableValue<IntData>(TRAININGSAMPLESSTART, scope, true).Data;
63      int origTrainingSamplesEnd = GetVariableValue<IntData>(TRAININGSAMPLESEND, scope, true).Data;
64      int origValidationSamplesStart = GetVariableValue<IntData>(VALIDATIONSAMPLESSTART, scope, true).Data;
65      int origValidationSamplesEnd = GetVariableValue<IntData>(VALIDATIONSAMPLESEND, scope, true).Data;
66      int n=origDataset.Rows;
67      int origTrainingSamples = (origTrainingSamplesEnd-origTrainingSamplesStart);
68      int origValidationSamples = (origValidationSamplesEnd-origValidationSamplesStart);
69
70      double percentTrainingSamples = origTrainingSamples / (double)(origValidationSamples + origTrainingSamples);
71      int nTestSamples = n / nFolds;
72
73      int newTrainingSamplesStart = 0;
74      int newTrainingSamplesEnd = (int)((n - nTestSamples) * percentTrainingSamples);
75      int newValidationSamplesStart = newTrainingSamplesEnd;
76      int newValidationSamplesEnd = n - nTestSamples;
77      int newTestSamplesStart = n - nTestSamples;
78      int newTestSamplesEnd = n;
79
80      for(int i = 0; i < nFolds; i++) {
81        Scope childScope = new Scope(i.ToString());
82        Dataset rotatedSet = new Dataset();
83
84        double[] samples = new double[origDataset.Samples.Length];
85        Array.Copy(origDataset.Samples, samples, samples.Length);
86        RotateArray(samples, i * nTestSamples * origDataset.Columns);
87
88        rotatedSet.Rows = origDataset.Rows;
89        rotatedSet.Columns = origDataset.Columns;
90        rotatedSet.Samples = samples;
91        childScope.AddVariable(new Variable(scope.TranslateName(DATASET), rotatedSet));
92        childScope.AddVariable(new Variable(scope.TranslateName(TRAININGSAMPLESSTART), new IntData(newTrainingSamplesStart)));
93        childScope.AddVariable(new Variable(scope.TranslateName(TRAININGSAMPLESEND), new IntData(newTrainingSamplesEnd)));
94        childScope.AddVariable(new Variable(scope.TranslateName(VALIDATIONSAMPLESSTART), new IntData(newValidationSamplesStart)));
95        childScope.AddVariable(new Variable(scope.TranslateName(VALIDATIONSAMPLESEND), new IntData(newValidationSamplesEnd)));
96        childScope.AddVariable(new Variable(scope.TranslateName(TESTSAMPLESSTART), new IntData(newTestSamplesStart)));
97        childScope.AddVariable(new Variable(scope.TranslateName(TESTSAMPLESEND), new IntData(newTestSamplesEnd)));
98
99        scope.AddSubScope(childScope);
100      }
101      return null;
102    }
103
104    private void RotateArray(double[] samples, int p) {
105      Array.Reverse(samples, 0, p);
106      Array.Reverse(samples, p, samples.Length - p);
107      Array.Reverse(samples);
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.