[422] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Functions;
|
---|
| 30 | using HeuristicLab.DataAnalysis;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.StructureIdentification {
|
---|
| 33 | public class AccuracyEvaluator : GPEvaluatorBase {
|
---|
[474] | 34 | private const double EPSILON = 1.0E-6;
|
---|
[479] | 35 | private double[] classesArr;
|
---|
| 36 | private double[] thresholds;
|
---|
[482] | 37 | private DoubleData accuracy;
|
---|
[422] | 38 | public override string Description {
|
---|
| 39 | get {
|
---|
[482] | 40 | 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.";
|
---|
[422] | 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public AccuracyEvaluator()
|
---|
| 45 | : base() {
|
---|
[482] | 46 | AddVariableInfo(new VariableInfo("Accuracy", "The total accuracy of the model (ratio of correctly classified instances to total number of instances)", typeof(DoubleData), VariableKind.New));
|
---|
[474] | 47 | AddVariableInfo(new VariableInfo("TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));
|
---|
[422] | 48 | }
|
---|
| 49 |
|
---|
[479] | 50 | public override IOperation Apply(IScope scope) {
|
---|
[482] | 51 | accuracy = GetVariableValue<DoubleData>("Accuracy", scope, false, false);
|
---|
| 52 | if(accuracy == null) {
|
---|
| 53 | accuracy = new DoubleData();
|
---|
| 54 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Accuracy"), accuracy));
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[474] | 57 | ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);
|
---|
[479] | 58 | classesArr = new double[classes.Count];
|
---|
| 59 | for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data;
|
---|
[474] | 60 | Array.Sort(classesArr);
|
---|
[479] | 61 | thresholds = new double[classes.Count - 1];
|
---|
| 62 | for(int i = 0; i < classesArr.Length - 1; i++) {
|
---|
| 63 | thresholds[i] = (classesArr[i] + classesArr[i + 1]) / 2.0;
|
---|
[474] | 64 | }
|
---|
| 65 |
|
---|
[479] | 66 | return base.Apply(scope);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[482] | 69 | public override void Evaluate(int start, int end) {
|
---|
| 70 | int nSamples = end - start;
|
---|
[474] | 71 | int nCorrect = 0;
|
---|
[479] | 72 | for(int sample = start; sample < end; sample++) {
|
---|
| 73 | double est = GetEstimatedValue(sample);
|
---|
| 74 | double origClass = GetOriginalValue(sample);
|
---|
[474] | 75 | double estClass = double.NaN;
|
---|
[476] | 76 | // if estimation is lower than the smallest threshold value -> estimated class is the lower class
|
---|
| 77 | if(est < thresholds[0]) estClass = classesArr[0];
|
---|
| 78 | // if estimation is larger (or equal) than the largest threshold value -> estimated class is the upper class
|
---|
[482] | 79 | else if(est >= thresholds[thresholds.Length - 1]) estClass = classesArr[classesArr.Length - 1];
|
---|
[474] | 80 | else {
|
---|
[476] | 81 | // otherwise the estimated class is the class which upper threshold is larger than the estimated value
|
---|
[474] | 82 | for(int k = 0; k < thresholds.Length; k++) {
|
---|
| 83 | if(thresholds[k] > est) {
|
---|
[476] | 84 | estClass = classesArr[k];
|
---|
[474] | 85 | break;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
[422] | 88 | }
|
---|
[484] | 89 | SetOriginalValue(sample, estClass);
|
---|
[474] | 90 | if(Math.Abs(estClass - origClass) < EPSILON) nCorrect++;
|
---|
[422] | 91 | }
|
---|
[482] | 92 | accuracy.Data = nCorrect / (double)nSamples;
|
---|
[422] | 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|