Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.StructureIdentification/Evaluation/AccuracyEvaluator.cs @ 475

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

improved AccuracyEvaluator (#241)
calculates the ratio of correctly classified instances to total number of instances with static class threshold (exactly in the middle between two classes)

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.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    public override string Description {
36      get {
37        return @"TASK";
38      }
39    }
40
41    public AccuracyEvaluator()
42      : base() {
43      AddVariableInfo(new VariableInfo("TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));
44    }
45
46    private double[] original = new double[1];
47    private double[] estimated = new double[1];
48    public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) {
49      int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
50      int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
51      int nSamples = trainingEnd-trainingStart;
52      ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);
53      double[] classesArr = new double[classes.Count];
54      for(int i=0;i<classesArr.Length;i++) classesArr[i] = classes[i].Data;
55      Array.Sort(classesArr);
56      double[] thresholds = new double[classes.Count - 1];
57      for(int i=0;i<classesArr.Length-1;i++) {
58        thresholds[i] = (classesArr[i]+classesArr[i+1]) / 2.0;
59      }
60
61      int nCorrect = 0;
62      for(int sample = trainingStart; sample < trainingEnd; sample++) {
63        double est = evaluator.Evaluate(sample);
64        double origClass = dataset.GetValue(sample, targetVariable);
65        double estClass = double.NaN;
66        if(est < classesArr[0]) estClass = classesArr[0];
67        else if(est > classesArr[classesArr.Length - 1]) estClass = classesArr[classesArr.Length - 1];
68        else {
69          for(int k = 0; k < thresholds.Length; k++) {
70            if(thresholds[k] > est) {
71              estClass = classesArr[k + 1];
72              break;
73            }
74          }
75        }
76        if(Math.Abs(estClass - origClass) < EPSILON) nCorrect++;
77      }
78      scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * nSamples;
79      return  nCorrect / (double)nSamples;
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.