Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.GP.StructureIdentification.Classification/3.3/AccuracyEvaluator.cs @ 4900

Last change on this file since 4900 was 1891, checked in by gkronber, 15 years ago

Fixed #645 (Tree evaluators precompile the model for each evaluation of a row).

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