Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MCCEvaluator.cs @ 453

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

fixed a small problem in classification evaluators (#225)

File size: 3.7 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 MCCEvaluator : GPEvaluatorBase {
34    public override string Description {
35      get {
36        return @"TASK";
37      }
38    }
39
40    public MCCEvaluator()
41      : base() {
42      AddVariableInfo(new VariableInfo("ClassSeparation", "The value of separation between negative and positive target classification values (for instance 0.5 if negative=0 and positive=1).", typeof(DoubleData), VariableKind.In));
43    }
44
45    private double[] original = new double[1];
46    private double[] estimated = new double[1];
47    public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) {
48      int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
49      int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
50      int nSamples = trainingEnd-trainingStart;
51      double limit = GetVariableValue<DoubleData>("ClassSeparation", scope, true).Data;
52      if(estimated.Length != nSamples) {
53        estimated = new double[nSamples];
54        original = new double[nSamples];
55      }
56
57      double positive = 0;
58      double negative = 0;
59      double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);
60      for(int sample = trainingStart; sample < trainingEnd; sample++) {
61        double est = evaluator.Evaluate(sample);
62        double orig = dataset.GetValue(sample, targetVariable);
63        if(double.IsNaN(est) || double.IsInfinity(est)) {
64          est = targetMean + maximumPunishment;
65        } else if(est > targetMean + maximumPunishment) {
66          est = targetMean + maximumPunishment;
67        } else if(est < targetMean - maximumPunishment) {
68          est = targetMean - maximumPunishment;
69        }
70        estimated[sample-trainingStart] = est;
71        original[sample-trainingStart] = orig;
72        if(orig >= limit) positive++;
73        else negative++;
74      }
75      Array.Sort(estimated, original);
76      double best_mcc = -1.0;
77      double tp = 0;
78      double fn = positive;
79      double tn = negative;
80      double fp = 0;
81      for(int i = original.Length-1; i >= 0 ; i--) {
82        if(original[i] >= limit) {
83          tp++; fn--;
84        } else {
85          tn--; fp++;
86        }
87        double mcc = (tp * tn - fp * fn) / Math.Sqrt(positive * (tp + fn) * (tn + fp) * negative);
88        if(best_mcc < mcc) {
89          best_mcc = mcc;
90        }
91      }
92      scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (trainingEnd - trainingStart);
93      return best_mcc;
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.