[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 HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.DataAnalysis;
|
---|
[2222] | 26 | using HeuristicLab.GP.Interfaces;
|
---|
[2328] | 27 | using HeuristicLab.Common;
|
---|
[645] | 28 |
|
---|
[668] | 29 | namespace HeuristicLab.GP.StructureIdentification.Classification {
|
---|
[645] | 30 | public class MulticlassOneVsOneAnalyzer : OperatorBase {
|
---|
| 31 |
|
---|
| 32 | private const string DATASET = "Dataset";
|
---|
| 33 | private const string TARGETVARIABLE = "TargetVariable";
|
---|
| 34 | private const string TARGETCLASSVALUES = "TargetClassValues";
|
---|
[1796] | 35 | private const string TRAININGSAMPLESSTART = "TrainingSamplesStart";
|
---|
| 36 | private const string TRAININGSAMPLESEND = "TrainingSamplesEnd";
|
---|
[645] | 37 | private const string SAMPLESSTART = "SamplesStart";
|
---|
| 38 | private const string SAMPLESEND = "SamplesEnd";
|
---|
| 39 | private const string CLASSAVALUE = "ClassAValue";
|
---|
| 40 | private const string CLASSBVALUE = "ClassBValue";
|
---|
| 41 | private const string BESTMODELLSCOPE = "BestValidationSolution";
|
---|
| 42 | private const string BESTMODELL = "FunctionTree";
|
---|
| 43 | private const string VOTES = "Votes";
|
---|
| 44 | private const string ACCURACY = "Accuracy";
|
---|
[2222] | 45 | private const string TREEEVALUATOR = "TreeEvaluator";
|
---|
[645] | 46 |
|
---|
| 47 | public override string Description {
|
---|
| 48 | get { return @"TASK"; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public MulticlassOneVsOneAnalyzer()
|
---|
| 52 | : base() {
|
---|
| 53 | AddVariableInfo(new VariableInfo(DATASET, "The dataset to use", typeof(Dataset), VariableKind.In));
|
---|
[2440] | 54 | AddVariableInfo(new VariableInfo(TARGETVARIABLE, "Target variable", typeof(StringData), VariableKind.In));
|
---|
[645] | 55 | AddVariableInfo(new VariableInfo(TARGETCLASSVALUES, "Class values of the target variable in the original dataset", typeof(ItemList<DoubleData>), VariableKind.In));
|
---|
| 56 | AddVariableInfo(new VariableInfo(CLASSAVALUE, "The original class value of the class A in the subscope", typeof(DoubleData), VariableKind.In));
|
---|
| 57 | AddVariableInfo(new VariableInfo(CLASSBVALUE, "The original class value of the class B in the subscope", typeof(DoubleData), VariableKind.In));
|
---|
| 58 | AddVariableInfo(new VariableInfo(SAMPLESSTART, "The start of samples in the original dataset", typeof(IntData), VariableKind.In));
|
---|
| 59 | AddVariableInfo(new VariableInfo(SAMPLESEND, "The end of samples in the original dataset", typeof(IntData), VariableKind.In));
|
---|
| 60 | AddVariableInfo(new VariableInfo(BESTMODELLSCOPE, "The variable containing the scope of the model (incl. meta data)", typeof(IScope), VariableKind.In));
|
---|
[2222] | 61 | AddVariableInfo(new VariableInfo(BESTMODELL, "The variable in the scope of the model that contains the actual model", typeof(IGeneticProgrammingModel), VariableKind.In));
|
---|
| 62 | AddVariableInfo(new VariableInfo(TREEEVALUATOR, "The evaluator to apply to the function tree", typeof(ITreeEvaluator), VariableKind.In));
|
---|
[645] | 63 | AddVariableInfo(new VariableInfo(VOTES, "Array with the votes for each instance", typeof(IntMatrixData), VariableKind.New));
|
---|
| 64 | AddVariableInfo(new VariableInfo(ACCURACY, "Accuracy of the one-vs-one multi-cass classifier", typeof(DoubleData), VariableKind.New));
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public override IOperation Apply(IScope scope) {
|
---|
| 68 | Dataset dataset = GetVariableValue<Dataset>(DATASET, scope, true);
|
---|
[2440] | 69 | string targetVariable = GetVariableValue<StringData>(TARGETVARIABLE, scope, true).Data;
|
---|
| 70 | int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
|
---|
[1796] | 71 | int trainingSamplesStart = GetVariableValue<IntData>(TRAININGSAMPLESSTART, scope, true).Data;
|
---|
| 72 | int trainingSamplesEnd = GetVariableValue<IntData>(TRAININGSAMPLESEND, scope, true).Data;
|
---|
[645] | 73 | int samplesStart = GetVariableValue<IntData>(SAMPLESSTART, scope, true).Data;
|
---|
| 74 | int samplesEnd = GetVariableValue<IntData>(SAMPLESEND, scope, true).Data;
|
---|
| 75 | ItemList<DoubleData> classValues = GetVariableValue<ItemList<DoubleData>>(TARGETCLASSVALUES, scope, true);
|
---|
| 76 | int[,] votes = new int[samplesEnd - samplesStart, classValues.Count];
|
---|
| 77 |
|
---|
| 78 | foreach(IScope childScope in scope.SubScopes) {
|
---|
| 79 | double classAValue = GetVariableValue<DoubleData>(CLASSAVALUE, childScope, true).Data;
|
---|
| 80 | double classBValue = GetVariableValue<DoubleData>(CLASSBVALUE, childScope, true).Data;
|
---|
| 81 | IScope bestScope = GetVariableValue<IScope>(BESTMODELLSCOPE, childScope, true);
|
---|
[2222] | 82 | IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>(BESTMODELL, bestScope, true);
|
---|
[645] | 83 |
|
---|
[2222] | 84 | ITreeEvaluator evaluator = GetVariableValue<ITreeEvaluator>(TREEEVALUATOR, bestScope, true);
|
---|
[2328] | 85 | evaluator.PrepareForEvaluation(dataset, gpModel.FunctionTree);
|
---|
[645] | 86 | for(int i = 0; i < (samplesEnd - samplesStart); i++) {
|
---|
[1891] | 87 | double est = evaluator.Evaluate(i + samplesStart);
|
---|
[645] | 88 | if(est < 0.5) {
|
---|
| 89 | CastVote(votes, i, classAValue, classValues);
|
---|
| 90 | } else {
|
---|
| 91 | CastVote(votes, i, classBValue, classValues);
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | int correctlyClassified = 0;
|
---|
| 97 | for(int i = 0; i < (samplesEnd - samplesStart); i++) {
|
---|
[2440] | 98 | double originalClassValue = dataset.GetValue(i + samplesStart, targetVariableIndex);
|
---|
[645] | 99 | double estimatedClassValue = classValues[0].Data;
|
---|
| 100 | int maxVotes = votes[i, 0];
|
---|
| 101 | int sameVotes = 0;
|
---|
| 102 | for(int j = 1; j < classValues[j].Data; j++) {
|
---|
| 103 | if(votes[i, j] > maxVotes) {
|
---|
| 104 | maxVotes = votes[i, j];
|
---|
| 105 | estimatedClassValue = classValues[j].Data;
|
---|
| 106 | sameVotes = 0;
|
---|
| 107 | } else if(votes[i, j] == maxVotes) {
|
---|
| 108 | sameVotes++;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
[2328] | 111 | if(originalClassValue.IsAlmost(estimatedClassValue) && sameVotes == 0) correctlyClassified++;
|
---|
[645] | 112 | }
|
---|
| 113 |
|
---|
| 114 | double accuracy = correctlyClassified / (double)(samplesEnd - samplesStart);
|
---|
| 115 |
|
---|
| 116 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(VOTES), new IntMatrixData(votes)));
|
---|
| 117 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(ACCURACY), new DoubleData(accuracy)));
|
---|
| 118 | return null;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | private void CastVote(int[,] votes, int sample, double votedClass, ItemList<DoubleData> classValues) {
|
---|
| 122 | for(int i = 0; i < classValues.Count; i++) {
|
---|
[2328] | 123 | if(classValues[i].Data.IsAlmost(votedClass)) votes[sample, i]++;
|
---|
[645] | 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | }
|
---|