Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Modeling/3.2/SimpleConfusionMatrixEvaluator.cs @ 3177

Last change on this file since 3177 was 2357, checked in by gkronber, 15 years ago

Fixed #740 (SimpleEvaluators in HL.GP.StructId and HL.SVM are not compatible with evaluators in HL.Modeling).

File size: 3.7 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 HeuristicLab.Core;
23using HeuristicLab.Data;
24
25namespace HeuristicLab.Modeling {
26  public class SimpleConfusionMatrixEvaluator : OperatorBase {
27    protected const int ORIGINAL_INDEX = 0;
28    protected const int ESTIMATION_INDEX = 1;
29    public override string Description {
30      get {
31        return @"Calculates the classifcation matrix of the model.";
32      }
33    }
34
35    public SimpleConfusionMatrixEvaluator()
36      : base() {
37      AddVariableInfo(new VariableInfo("Values", "Original and predicted target values generated by a model", typeof(DoubleMatrixData), VariableKind.In));
38      AddVariableInfo(new VariableInfo("ConfusionMatrix", "The confusion matrix of the model", typeof(IntMatrixData), VariableKind.New));
39    }
40
41    public override IOperation Apply(IScope scope) {
42      double[,] values = GetVariableValue<DoubleMatrixData>("Values", scope, true).Data;
43      int[,] confusionMatrix = Calculate(values);
44      IntMatrixData matrix = GetVariableValue<IntMatrixData>("ConfusionMatrix", scope, false, false);
45      if (matrix == null) {
46        matrix = new IntMatrixData(confusionMatrix);
47        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("ConfusionMatrix"), matrix));
48      }
49
50      return null;
51    }
52
53    public static int[,] Calculate(double[,] values) {
54      double[] classes = SimpleAccuracyEvaluator.CalculateTargetClasses(values);
55      double[] thresholds = SimpleAccuracyEvaluator.CalculateThresholds(classes);
56      int nSamples = values.GetLength(0);
57      int[,] confusionMatrix = new int[classes.Length, classes.Length];
58      for (int sample = 0; sample < nSamples; sample++) {
59        double est = values[sample, ESTIMATION_INDEX];
60        double origClass = values[sample, ORIGINAL_INDEX];
61        int estClassIndex = -1;
62        // if estimation is lower than the smallest threshold value -> estimated class is the lower class
63        if (est < thresholds[0]) estClassIndex = 0;
64        // if estimation is larger (or equal) than the largest threshold value -> estimated class is the upper class
65        else if (est >= thresholds[thresholds.Length - 1]) estClassIndex = classes.Length - 1;
66        else {
67          // otherwise the estimated class is the class which upper threshold is larger than the estimated value
68          for (int k = 0; k < thresholds.Length; k++) {
69            if (thresholds[k] > est) {
70              estClassIndex = k;
71              break;
72            }
73          }
74        }
75
76        // find the first threshold index that is larger to the original value
77        int origClassIndex = classes.Length - 1;
78        for (int i = 0; i < thresholds.Length; i++) {
79          if (origClass < thresholds[i]) {
80            origClassIndex = i;
81            break;
82          }
83        }
84        confusionMatrix[origClassIndex, estClassIndex]++;
85      }
86      return confusionMatrix;
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.