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