Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/3.3/AccuracyEvaluator.cs @ 2227

Last change on this file since 2227 was 2222, checked in by gkronber, 15 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 System;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.DataAnalysis;
26
27namespace HeuristicLab.GP.StructureIdentification.Classification {
28  public class AccuracyEvaluator : GPClassificationEvaluatorBase {
29    private const double EPSILON = 1.0E-6;
30    public override string Description {
31      get {
32        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.";
33      }
34    }
35
36    public AccuracyEvaluator()
37      : base() {
38      AddVariableInfo(new VariableInfo("Accuracy", "The total accuracy of the model (ratio of correctly classified instances to total number of instances)", typeof(DoubleData), VariableKind.New));
39    }
40
41    public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, double[] classes, double[] thresholds, int start, int end) {
42      DoubleData accuracy = GetVariableValue<DoubleData>("Accuracy", scope, false, false);
43      if (accuracy == null) {
44        accuracy = new DoubleData();
45        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Accuracy"), accuracy));
46      }
47
48      int nSamples = end - start;
49      int nCorrect = 0;
50      for (int sample = start; sample < end; sample++) {
51        double est = evaluator.Evaluate(sample);
52        double origClass = dataset.GetValue(sample, targetVariable);
53        double estClass = double.NaN;
54        // if estimation is lower than the smallest threshold value -> estimated class is the lower class
55        if (est < thresholds[0]) estClass = classes[0];
56        // if estimation is larger (or equal) than the largest threshold value -> estimated class is the upper class
57        else if (est >= thresholds[thresholds.Length - 1]) estClass = classes[classes.Length - 1];
58        else {
59          // otherwise the estimated class is the class which upper threshold is larger than the estimated value
60          for (int k = 0; k < thresholds.Length; k++) {
61            if (thresholds[k] > est) {
62              estClass = classes[k];
63              break;
64            }
65          }
66        }
67        if (Math.Abs(estClass - origClass) < EPSILON) nCorrect++;
68      }
69      accuracy.Data = nCorrect / (double)nSamples;
70    }
71  }
72}
Note: See TracBrowser for help on using the repository browser.