Free cookie consent management tool by TermsFeed Policy Generator

source: branches/plugins/HeuristicLab.StructureIdentification/3.2/MulticlassModeller.cs @ 2585

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

implemented #259 (Operator for cross-validation). Also added operators for multi-class one-vs-one modeling.

File size: 8.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.Operators;
29using HeuristicLab.DataAnalysis;
30
31namespace HeuristicLab.StructureIdentification {
32  public class MulticlassModeller : OperatorBase {
33
34    private const string DATASET = "Dataset";
35    private const string TARGETVARIABLE = "TargetVariable";
36    private const string TARGETCLASSVALUES = "TargetClassValues";
37    private const string TRAININGSAMPLESSTART = "TrainingSamplesStart";
38    private const string TRAININGSAMPLESEND = "TrainingSamplesEnd";
39    private const string VALIDATIONSAMPLESSTART = "ValidationSamplesStart";
40    private const string VALIDATIONSAMPLESEND = "ValidationSamplesEnd";
41    private const string CLASSAVALUE = "ClassAValue";
42    private const string CLASSBVALUE = "ClassBValue";
43    private const double EPSILON = 1E-6;
44    public override string Description {
45      get { return @"TASK"; }
46    }
47
48    public MulticlassModeller()
49      : base() {
50      AddVariableInfo(new VariableInfo(DATASET, "The original dataset and the new dataset parts in the newly created subscopes", typeof(Dataset), VariableKind.In));
51      AddVariableInfo(new VariableInfo(TARGETVARIABLE, "TargetVariable", typeof(IntData), VariableKind.In));
52      AddVariableInfo(new VariableInfo(TARGETCLASSVALUES, "Class values of the target variable in the original dataset and in the new dataset parts", typeof(ItemList<DoubleData>), VariableKind.In | VariableKind.New));
53      AddVariableInfo(new VariableInfo(CLASSAVALUE, "The original class value of the new class A", typeof(DoubleData), VariableKind.New));
54      AddVariableInfo(new VariableInfo(CLASSBVALUE, "The original class value of the new class B", typeof(DoubleData), VariableKind.New));
55      AddVariableInfo(new VariableInfo(TRAININGSAMPLESSTART, "The start of training samples in the original dataset and starts of training samples in the new dataset parts", typeof(IntData), VariableKind.In | VariableKind.New));
56      AddVariableInfo(new VariableInfo(TRAININGSAMPLESEND, "The end of training samples in the original dataset and ends of training samples in the new dataset parts", typeof(IntData), VariableKind.In | VariableKind.New));
57      AddVariableInfo(new VariableInfo(VALIDATIONSAMPLESSTART, "The start of validation samples in the original dataset and starts of validation samples in the new dataset parts", typeof(IntData), VariableKind.In | VariableKind.New));
58      AddVariableInfo(new VariableInfo(VALIDATIONSAMPLESEND, "The end of validation samples in the original dataset and ends of validation samples in the new dataset parts", typeof(IntData), VariableKind.In | VariableKind.New));
59    }
60
61    public override IOperation Apply(IScope scope) {
62      Dataset origDataset = GetVariableValue<Dataset>(DATASET, scope, true);
63      int targetVariable = GetVariableValue<IntData>(TARGETVARIABLE, scope, true).Data;
64      ItemList<DoubleData> classValues = GetVariableValue<ItemList<DoubleData>>(TARGETCLASSVALUES, scope, true);
65      int origTrainingSamplesStart = GetVariableValue<IntData>(TRAININGSAMPLESSTART, scope, true).Data;
66      int origTrainingSamplesEnd = GetVariableValue<IntData>(TRAININGSAMPLESEND, scope, true).Data;
67      int origValidationSamplesStart = GetVariableValue<IntData>(VALIDATIONSAMPLESSTART, scope, true).Data;
68      int origValidationSamplesEnd = GetVariableValue<IntData>(VALIDATIONSAMPLESEND, scope, true).Data;
69      ItemList<DoubleData> binaryClassValues = new ItemList<DoubleData>();
70      binaryClassValues.Add(new DoubleData(0.0));
71      binaryClassValues.Add(new DoubleData(1.0));
72      for(int i = 0; i < classValues.Count-1; i++) {
73        for(int j = i+1; j < classValues.Count; j++) {
74          Dataset dataset = new Dataset();
75          dataset.Columns = origDataset.Columns;
76          double classAValue = classValues[i].Data;
77          double classBValue = classValues[j].Data;
78          int trainingSamplesStart;
79          int trainingSamplesEnd;
80          int validationSamplesStart;
81          int validationSamplesEnd;
82
83          trainingSamplesStart = 0;
84          List<double[]> rows = new List<double[]>();
85          for(int k = origTrainingSamplesStart; k < origTrainingSamplesEnd; k++) {
86            double[] row = new double[dataset.Columns];
87            double targetValue = origDataset.GetValue(k, targetVariable);
88            if(IsEqual(targetValue, classAValue)) {
89              for(int l = 0; l < row.Length; l++) {
90                row[l] = origDataset.GetValue(k, l);
91              }
92              row[targetVariable] = 0;
93              rows.Add(row);
94            } else if(IsEqual(targetValue, classBValue)) {
95              for(int l = 0; l < row.Length; l++) {
96                row[l] = origDataset.GetValue(k, l);
97              }
98              row[targetVariable] = 1.0;
99              rows.Add(row);
100            }
101          }
102          trainingSamplesEnd = rows.Count;
103          validationSamplesStart = rows.Count;
104          for(int k = origValidationSamplesStart; k < origValidationSamplesEnd; k++) {
105            double[] row = new double[dataset.Columns];
106            double targetValue = origDataset.GetValue(k, targetVariable);
107            if(IsEqual(targetValue, classAValue)) {
108              for(int l = 0; l < row.Length; l++) {
109                row[l] = origDataset.GetValue(k, l);
110              }
111              row[targetVariable] = 0;
112              rows.Add(row);
113            } else if(IsEqual(targetValue, classBValue)) {
114              for(int l = 0; l < row.Length; l++) {
115                row[l] = origDataset.GetValue(k, l);
116              }
117              row[targetVariable] = 1.0;
118              rows.Add(row);
119            }
120          }
121          validationSamplesEnd = rows.Count;
122
123          dataset.Rows = rows.Count;
124          dataset.Samples = new double[dataset.Rows * dataset.Columns];
125          for(int k = 0; k < dataset.Rows; k++) {
126            for(int l = 0; l < dataset.Columns; l++) {
127              dataset.SetValue(k, l, rows[k][l]);
128            }
129          }
130
131          Scope childScope = new Scope(classAValue+" vs. "+classBValue);
132
133          childScope.AddVariable(new Variable(scope.TranslateName(TARGETCLASSVALUES), binaryClassValues));
134          childScope.AddVariable(new Variable(scope.TranslateName(CLASSAVALUE), new DoubleData(classAValue)));
135          childScope.AddVariable(new Variable(scope.TranslateName(CLASSBVALUE), new DoubleData(classBValue)));
136          childScope.AddVariable(new Variable(scope.TranslateName(TRAININGSAMPLESSTART), new IntData(trainingSamplesStart)));
137          childScope.AddVariable(new Variable(scope.TranslateName(TRAININGSAMPLESEND), new IntData(trainingSamplesEnd)));
138          childScope.AddVariable(new Variable(scope.TranslateName(VALIDATIONSAMPLESSTART), new IntData(validationSamplesStart)));
139          childScope.AddVariable(new Variable(scope.TranslateName(VALIDATIONSAMPLESEND), new IntData(validationSamplesEnd)));
140          childScope.AddVariable(new Variable(scope.TranslateName(DATASET), dataset));
141          scope.AddSubScope(childScope);
142        }
143      }
144      return null;
145    }
146
147    private bool IsEqual(double x, double y) {
148      return Math.Abs(x - y) < EPSILON;
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.