Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1574 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

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