Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.StructureIdentification/Evaluation/ClassificationMatrixEvaluator.cs @ 630

Last change on this file since 630 was 485, checked in by gkronber, 16 years ago

implemented #243 (GP evaluator that calculates the full classification matrix)

File size: 4.1 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.Operators;
29using HeuristicLab.Functions;
30using HeuristicLab.DataAnalysis;
31
32namespace HeuristicLab.StructureIdentification {
33  public class ClassificationMatrixEvaluator : GPEvaluatorBase {
34    private const double EPSILON = 1.0E-6;
35    private double[] classesArr;
36    private double[] thresholds;
37    private IntMatrixData matrix;
38    public override string Description {
39      get {
40        return @"Calculates the classifcation matrix of the model.";
41      }
42    }
43
44    public ClassificationMatrixEvaluator()
45      : base() {
46      AddVariableInfo(new VariableInfo("ClassificationMatrix", "The resulting classification matrix of the model", typeof(IntMatrixData), VariableKind.New));
47      AddVariableInfo(new VariableInfo("TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));
48    }
49
50    public override IOperation Apply(IScope scope) {
51      ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);
52      classesArr = new double[classes.Count];
53      for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data;
54      Array.Sort(classesArr);
55      thresholds = new double[classes.Count - 1];
56      for(int i = 0; i < classesArr.Length - 1; i++) {
57        thresholds[i] = (classesArr[i] + classesArr[i + 1]) / 2.0;
58      }
59
60      matrix = GetVariableValue<IntMatrixData>("ClassificationMatrix", scope, false, false);
61      if(matrix == null) {
62        matrix = new IntMatrixData(new int[classesArr.Length, classesArr.Length]);
63        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("ClassificationMatrix"), matrix));
64      }
65      return base.Apply(scope);
66    }
67
68    public override void Evaluate(int start, int end) {
69      int nSamples = end - start;
70      for(int sample = start; sample < end; sample++) {
71        double est = GetEstimatedValue(sample);
72        double origClass = GetOriginalValue(sample);
73        int estClassIndex = -1;
74        // if estimation is lower than the smallest threshold value -> estimated class is the lower class
75        if(est < thresholds[0]) estClassIndex = 0;
76        // if estimation is larger (or equal) than the largest threshold value -> estimated class is the upper class
77        else if(est >= thresholds[thresholds.Length - 1]) estClassIndex = classesArr.Length - 1;
78        else {
79          // otherwise the estimated class is the class which upper threshold is larger than the estimated value
80          for(int k = 0; k < thresholds.Length; k++) {
81            if(thresholds[k] > est) {
82              estClassIndex = k;
83              break;
84            }
85          }
86        }
87        SetOriginalValue(sample, classesArr[estClassIndex]);
88
89        int origClassIndex = -1;
90        for(int i = 0; i < classesArr.Length; i++) {
91          if(IsEqual(origClass, classesArr[i])) origClassIndex = i;
92        }
93        matrix.Data[origClassIndex, estClassIndex]++;
94      }
95    }
96
97    private bool IsEqual(double x, double y) {
98      return Math.Abs(x - y) < EPSILON;
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.