Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.StructureIdentification/MulticlassOneVsOneAnalyzer.cs @ 12977

Last change on this file since 12977 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: 6.3 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;
30using HeuristicLab.Functions;
31
32namespace HeuristicLab.StructureIdentification {
33  public class MulticlassOneVsOneAnalyzer : OperatorBase {
34
35    private const string DATASET = "Dataset";
36    private const string TARGETVARIABLE = "TargetVariable";
37    private const string TARGETCLASSVALUES = "TargetClassValues";
38    private const string SAMPLESSTART = "SamplesStart";
39    private const string SAMPLESEND = "SamplesEnd";
40    private const string CLASSAVALUE = "ClassAValue";
41    private const string CLASSBVALUE = "ClassBValue";
42    private const string BESTMODELLSCOPE = "BestValidationSolution";
43    private const string BESTMODELL = "FunctionTree";
44    private const string VOTES = "Votes";
45    private const string ACCURACY = "Accuracy";
46
47    private const double EPSILON = 1E-6;
48    public override string Description {
49      get { return @"TASK"; }
50    }
51
52    public MulticlassOneVsOneAnalyzer()
53      : base() {
54      AddVariableInfo(new VariableInfo(DATASET, "The dataset to use", typeof(Dataset), VariableKind.In));
55      AddVariableInfo(new VariableInfo(TARGETVARIABLE, "Target variable", typeof(IntData), VariableKind.In));
56      AddVariableInfo(new VariableInfo(TARGETCLASSVALUES, "Class values of the target variable in the original dataset", typeof(ItemList<DoubleData>), VariableKind.In));
57      AddVariableInfo(new VariableInfo(CLASSAVALUE, "The original class value of the class A in the subscope", typeof(DoubleData), VariableKind.In));
58      AddVariableInfo(new VariableInfo(CLASSBVALUE, "The original class value of the class B in the subscope", typeof(DoubleData), VariableKind.In));
59      AddVariableInfo(new VariableInfo(SAMPLESSTART, "The start of samples in the original dataset", typeof(IntData), VariableKind.In));
60      AddVariableInfo(new VariableInfo(SAMPLESEND, "The end of samples in the original dataset", typeof(IntData), VariableKind.In));
61      AddVariableInfo(new VariableInfo(BESTMODELLSCOPE, "The variable containing the scope of the model (incl. meta data)", typeof(IScope), VariableKind.In));
62      AddVariableInfo(new VariableInfo(BESTMODELL, "The variable in the scope of the model that contains the actual model", typeof(IFunctionTree), VariableKind.In));
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);
69      int targetVariable = GetVariableValue<IntData>(TARGETVARIABLE, scope, true).Data;
70      int samplesStart = GetVariableValue<IntData>(SAMPLESSTART, scope, true).Data;
71      int samplesEnd = GetVariableValue<IntData>(SAMPLESEND, scope, true).Data;
72      ItemList<DoubleData> classValues = GetVariableValue<ItemList<DoubleData>>(TARGETCLASSVALUES, scope, true);
73      int[,] votes = new int[samplesEnd - samplesStart, classValues.Count];
74
75      foreach(IScope childScope in scope.SubScopes) {
76        double classAValue = GetVariableValue<DoubleData>(CLASSAVALUE, childScope, true).Data;
77        double classBValue = GetVariableValue<DoubleData>(CLASSBVALUE, childScope, true).Data;
78        IScope bestScope = GetVariableValue<IScope>(BESTMODELLSCOPE, childScope, true);
79        IFunctionTree functionTree = GetVariableValue<IFunctionTree>(BESTMODELL, bestScope, true);
80
81        IEvaluator evaluator = functionTree.CreateEvaluator();
82        evaluator.ResetEvaluator(functionTree, dataset);
83
84        for(int i = 0; i < (samplesEnd - samplesStart); i++) {
85          double est = evaluator.Evaluate(i + samplesStart);
86          if(est < 0.5) {
87            CastVote(votes, i, classAValue, classValues);
88          } else {
89            CastVote(votes, i, classBValue, classValues);
90          }
91        }
92      }
93
94      int correctlyClassified = 0;
95      for(int i = 0; i < (samplesEnd - samplesStart); i++) {
96        double originalClassValue = dataset.GetValue(i + samplesStart, targetVariable);
97        double estimatedClassValue = classValues[0].Data;
98        int maxVotes = votes[i, 0];
99        int sameVotes = 0;
100        for(int j = 1; j < classValues[j].Data; j++) {
101          if(votes[i, j] > maxVotes) {
102            maxVotes = votes[i, j];
103            estimatedClassValue = classValues[j].Data;
104            sameVotes = 0;
105          } else if(votes[i, j] == maxVotes) {
106            sameVotes++;
107          }
108        }
109        if(IsEqual(originalClassValue, estimatedClassValue) && sameVotes == 0) correctlyClassified++;
110      }
111
112      double accuracy = correctlyClassified / (double)(samplesEnd - samplesStart);
113
114      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(VOTES), new IntMatrixData(votes)));
115      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(ACCURACY), new DoubleData(accuracy)));
116      return null;
117    }
118
119    private void CastVote(int[,] votes, int sample, double votedClass, ItemList<DoubleData> classValues) {
120      for(int i = 0; i < classValues.Count; i++) {
121        if(IsEqual(classValues[i].Data, votedClass)) votes[sample, i]++;
122      }
123    }
124
125    private bool IsEqual(double x, double y) {
126      return Math.Abs(x - y) < EPSILON;
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.