Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.StructureIdentification/Evaluation/AccuracyEvaluator.cs @ 13365

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

fixed a bug where AccuracyEvaluator set the value of the discrimination function instead of the predicted class value (ticket #242)

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.Operators;
29using HeuristicLab.Functions;
30using HeuristicLab.DataAnalysis;
31
32namespace HeuristicLab.StructureIdentification {
33  public class AccuracyEvaluator : GPEvaluatorBase {
34    private const double EPSILON = 1.0E-6;
35    private double[] classesArr;
36    private double[] thresholds;
37    private DoubleData accuracy;
38    public override string Description {
39      get {
40        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.";
41      }
42    }
43
44    public AccuracyEvaluator()
45      : base() {
46      AddVariableInfo(new VariableInfo("Accuracy", "The total accuracy of the model (ratio of correctly classified instances to total number of instances)", typeof(DoubleData), 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      accuracy = GetVariableValue<DoubleData>("Accuracy", scope, false, false);
52      if(accuracy == null) {
53        accuracy = new DoubleData();
54        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Accuracy"), accuracy));
55      }
56
57      ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);
58      classesArr = new double[classes.Count];
59      for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data;
60      Array.Sort(classesArr);
61      thresholds = new double[classes.Count - 1];
62      for(int i = 0; i < classesArr.Length - 1; i++) {
63        thresholds[i] = (classesArr[i] + classesArr[i + 1]) / 2.0;
64      }
65
66      return base.Apply(scope);
67    }
68
69    public override void Evaluate(int start, int end) {
70      int nSamples = end - start;
71      int nCorrect = 0;
72      for(int sample = start; sample < end; sample++) {
73        double est = GetEstimatedValue(sample);
74        double origClass = GetOriginalValue(sample);
75        double estClass = double.NaN;
76        // if estimation is lower than the smallest threshold value -> estimated class is the lower class
77        if(est < thresholds[0]) estClass = classesArr[0];
78        // if estimation is larger (or equal) than the largest threshold value -> estimated class is the upper class
79        else if(est >= thresholds[thresholds.Length - 1]) estClass = classesArr[classesArr.Length - 1];
80        else {
81          // otherwise the estimated class is the class which upper threshold is larger than the estimated value
82          for(int k = 0; k < thresholds.Length; k++) {
83            if(thresholds[k] > est) {
84              estClass = classesArr[k];
85              break;
86            }
87          }
88        }
89        SetOriginalValue(sample, estClass);
90        if(Math.Abs(estClass - origClass) < EPSILON) nCorrect++;
91      }
92      accuracy.Data = nCorrect / (double)nSamples;
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.