Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GpPluginsRefactoringBranch/HeuristicLab.GP.StructureIdentification.Classification/AccuracyEvaluator.cs @ 646

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

renamed directories (#177)

File size: 4.0 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.Classification {
31  public class AccuracyEvaluator : GPEvaluatorBase {
32    private const double EPSILON = 1.0E-6;
33    private double[] classesArr;
34    private double[] thresholds;
35    private DoubleData accuracy;
36    public override string Description {
37      get {
38        return @"Calculates the total accuracy of the model (ratio of correctly classified instances to total number of instances) given a model and the list of possible target class values.";
39      }
40    }
41
42    public AccuracyEvaluator()
43      : base() {
44      AddVariableInfo(new VariableInfo("Accuracy", "The total accuracy of the model (ratio of correctly classified instances to total number of instances)", typeof(DoubleData), VariableKind.New));
45      AddVariableInfo(new VariableInfo("TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));
46    }
47
48    public override IOperation Apply(IScope scope) {
49      accuracy = GetVariableValue<DoubleData>("Accuracy", scope, false, false);
50      if(accuracy == null) {
51        accuracy = new DoubleData();
52        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Accuracy"), accuracy));
53      }
54
55      ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);
56      classesArr = new double[classes.Count];
57      for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data;
58      Array.Sort(classesArr);
59      thresholds = new double[classes.Count - 1];
60      for(int i = 0; i < classesArr.Length - 1; i++) {
61        thresholds[i] = (classesArr[i] + classesArr[i + 1]) / 2.0;
62      }
63
64      return base.Apply(scope);
65    }
66
67    public override void Evaluate(int start, int end) {
68      int nSamples = end - start;
69      int nCorrect = 0;
70      for(int sample = start; sample < end; sample++) {
71        double est = GetEstimatedValue(sample);
72        double origClass = GetOriginalValue(sample);
73        double estClass = double.NaN;
74        // if estimation is lower than the smallest threshold value -> estimated class is the lower class
75        if(est < thresholds[0]) estClass = classesArr[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]) estClass = classesArr[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              estClass = classesArr[k];
83              break;
84            }
85          }
86        }
87        SetOriginalValue(sample, estClass);
88        if(Math.Abs(estClass - origClass) < EPSILON) nCorrect++;
89      }
90      accuracy.Data = nCorrect / (double)nSamples;
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.