Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/3.3/ConfusionMatrixEvaluator.cs @ 2249

Last change on this file since 2249 was 2222, checked in by gkronber, 16 years ago

Merged changes from GP-refactoring branch back into the trunk #713.

File size: 3.2 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.GP.StructureIdentification.Classification {
26  public class ConfusionMatrixEvaluator : GPClassificationEvaluatorBase {
27    public override string Description {
28      get {
29        return @"Calculates the classifcation matrix of the model.";
30      }
31    }
32
33    public ConfusionMatrixEvaluator()
34      : base() {
35      AddVariableInfo(new VariableInfo("ConfusionMatrix", "The confusion matrix of the model", typeof(IntMatrixData), VariableKind.New));
36    }
37
38    public override void Evaluate(IScope scope, ITreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, double[] classes, double[] thresholds, int start, int end) {
39      IntMatrixData matrix = GetVariableValue<IntMatrixData>("ConfusionMatrix", scope, false, false);
40      if (matrix == null) {
41        matrix = new IntMatrixData(new int[classes.Length, classes.Length]);
42        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("ConfusionMatrix"), matrix));
43      }
44
45      int nSamples = end - start;
46      for (int sample = start; sample < end; sample++) {
47        double est = evaluator.Evaluate(sample);
48        double origClass = dataset.GetValue(sample, targetVariable);
49        int estClassIndex = -1;
50        // if estimation is lower than the smallest threshold value -> estimated class is the lower class
51        if (est < thresholds[0]) estClassIndex = 0;
52        // if estimation is larger (or equal) than the largest threshold value -> estimated class is the upper class
53        else if (est >= thresholds[thresholds.Length - 1]) estClassIndex = classes.Length - 1;
54        else {
55          // otherwise the estimated class is the class which upper threshold is larger than the estimated value
56          for (int k = 0; k < thresholds.Length; k++) {
57            if (thresholds[k] > est) {
58              estClassIndex = k;
59              break;
60            }
61          }
62        }
63
64        // find the first threshold index that is larger to the original value
65        int origClassIndex = classes.Length - 1;
66        for (int i = 0; i < thresholds.Length; i++) {
67          if (origClass < thresholds[i]) {
68            origClassIndex = i;
69            break;
70          }
71        }
72        matrix.Data[origClassIndex, estClassIndex]++;
73      }
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.