Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Modeling/3.2/SimpleAccuracyEvaluator.cs @ 3215

Last change on this file since 3215 was 2357, checked in by gkronber, 15 years ago

Fixed #740 (SimpleEvaluators in HL.GP.StructId and HL.SVM are not compatible with evaluators in HL.Modeling).

File size: 3.4 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;
26using System.Linq;
27using HeuristicLab.Common;
28
29namespace HeuristicLab.Modeling {
30  public class SimpleAccuracyEvaluator : SimpleEvaluatorBase {
31    public override string OutputVariableName {
32      get {
33        return "Accuracy";
34      }
35    }
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 override double Evaluate(double[,] values) {
43      return Calculate(values);
44    }
45
46    public static double Calculate(double[,] values) {
47      int nSamples = values.GetLength(0);
48      int nCorrect = 0;
49      double[] classes = CalculateTargetClasses(values);
50      double[] thresholds = CalculateThresholds(classes);
51
52      for (int sample = 0; sample < nSamples; sample++) {
53        double est = values[sample, ESTIMATION_INDEX];
54        double origClass = values[sample, ORIGINAL_INDEX];
55        double estClass = double.NaN;
56        // if estimation is lower than the smallest threshold value -> estimated class is the lower class
57        if (est < thresholds[0]) estClass = classes[0];
58        // if estimation is larger (or equal) than the largest threshold value -> estimated class is the upper class
59        else if (est >= thresholds[thresholds.Length - 1]) estClass = classes[classes.Length - 1];
60        else {
61          // otherwise the estimated class is the class which upper threshold is larger than the estimated value
62          for (int k = 0; k < thresholds.Length; k++) {
63            if (thresholds[k] > est) {
64              estClass = classes[k];
65              break;
66            }
67          }
68        }
69        if (estClass.IsAlmost(origClass)) nCorrect++;
70      }
71      return nCorrect / (double)nSamples;
72    }
73
74    public static double[] CalculateTargetClasses(double[,] values) {
75      int n = values.GetLength(0);
76      double[] original = new double[n];
77      for (int i = 0; i < n; i++) original[i] = values[i, ORIGINAL_INDEX];
78      return original.OrderBy(x => x).Distinct().ToArray();
79    }
80
81    public static double[] CalculateThresholds(double[] targetClasses) {
82      double[] thresholds = new double[targetClasses.Length - 1];
83      for (int i = 1; i < targetClasses.Length; i++) {
84        thresholds[i - 1] = (targetClasses[i - 1] + targetClasses[i]) / 2.0;
85      }
86      return thresholds;
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.