Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/ClassificationPerformanceMeasuresCalculator.cs @ 14149

Last change on this file since 14149 was 13156, checked in by gkronber, 9 years ago

#1998: merged changes from trunk to stable branch

File size: 5.5 KB
RevLine 
[11683]1#region License Information
2/* HeuristicLab
[12009]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11683]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 HeuristicLab.Common;
[13156]25using HeuristicLab.Problems.DataAnalysis.OnlineCalculators;
[11683]26
27namespace HeuristicLab.Problems.DataAnalysis {
28  public class ClassificationPerformanceMeasuresCalculator {
29
[11685]30    public ClassificationPerformanceMeasuresCalculator(string positiveClassName, double positiveClassValue) {
31      this.positiveClassName = positiveClassName;
[11683]32      this.positiveClassValue = positiveClassValue;
[11761]33      Reset();
[11683]34    }
35
36    #region Properties
37    private int truePositiveCount, falsePositiveCount, trueNegativeCount, falseNegativeCount;
38
[11761]39    private readonly string positiveClassName;
[11685]40    public string PositiveClassName {
[11761]41      get { return positiveClassName; }
[11685]42    }
[11761]43
44    private readonly double positiveClassValue;
[11685]45    public double PositiveClassValue {
[11761]46      get { return positiveClassValue; }
[11685]47    }
[11683]48    public double TruePositiveRate {
49      get {
50        double divisor = truePositiveCount + falseNegativeCount;
51        return divisor.IsAlmost(0) ? double.NaN : truePositiveCount / divisor;
52      }
53    }
54    public double TrueNegativeRate {
55      get {
56        double divisor = falsePositiveCount + trueNegativeCount;
57        return divisor.IsAlmost(0) ? double.NaN : trueNegativeCount / divisor;
58      }
59    }
60    public double PositivePredictiveValue {
61      get {
62        double divisor = truePositiveCount + falsePositiveCount;
63        return divisor.IsAlmost(0) ? double.NaN : truePositiveCount / divisor;
64      }
65    }
66    public double NegativePredictiveValue {
67      get {
68        double divisor = trueNegativeCount + falseNegativeCount;
69        return divisor.IsAlmost(0) ? double.NaN : trueNegativeCount / divisor;
70      }
71    }
72    public double FalsePositiveRate {
73      get {
74        double divisor = falsePositiveCount + trueNegativeCount;
75        return divisor.IsAlmost(0) ? double.NaN : falsePositiveCount / divisor;
76      }
77    }
78    public double FalseDiscoveryRate {
79      get {
80        double divisor = falsePositiveCount + truePositiveCount;
81        return divisor.IsAlmost(0) ? double.NaN : falsePositiveCount / divisor;
82      }
83    }
84
85    private OnlineCalculatorError errorState;
86    public OnlineCalculatorError ErrorState {
87      get { return errorState; }
88    }
89    #endregion
[11761]90
[11683]91    public void Reset() {
92      truePositiveCount = 0;
93      falseNegativeCount = 0;
94      trueNegativeCount = 0;
95      falseNegativeCount = 0;
96      errorState = OnlineCalculatorError.InsufficientElementsAdded;
97    }
98
99    public void Add(double originalClassValue, double estimatedClassValue) {
100      // ignore cases where original is NaN completely
[11761]101      if (double.IsNaN(originalClassValue)) return;
102
103      if (originalClassValue.IsAlmost(positiveClassValue)
104            || estimatedClassValue.IsAlmost(positiveClassValue)) { //positive class/positive class estimation
105        if (estimatedClassValue.IsAlmost(originalClassValue)) {
106          truePositiveCount++;
107        } else {
108          if (estimatedClassValue.IsAlmost(positiveClassValue)) //misclassification of the negative class
109            falsePositiveCount++;
110          else //misclassification of the positive class
111            falseNegativeCount++;
[11683]112        }
[11761]113      } else { //negative class/negative class estimation
114        //In a multiclass classification all misclassifications of the negative class
115        //will be treated as true negatives except on positive class estimations
116        trueNegativeCount++;
[11683]117      }
[11761]118
119      errorState = OnlineCalculatorError.None; // number of (non-NaN) samples >= 1
[11683]120    }
121
[11761]122    public void Calculate(IEnumerable<double> originalClassValues, IEnumerable<double> estimatedClassValues) {
[11683]123      IEnumerator<double> originalEnumerator = originalClassValues.GetEnumerator();
124      IEnumerator<double> estimatedEnumerator = estimatedClassValues.GetEnumerator();
125
126      // always move forward both enumerators (do not use short-circuit evaluation!)
127      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
128        double original = originalEnumerator.Current;
129        double estimated = estimatedEnumerator.Current;
130        Add(original, estimated);
131        if (ErrorState != OnlineCalculatorError.None) break;
132      }
133
134      // check if both enumerators are at the end to make sure both enumerations have the same length
[11761]135      if (ErrorState == OnlineCalculatorError.None && (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
[11683]136        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
137      }
[11761]138      errorState = ErrorState;
[11683]139    }
140  }
141}
Note: See TracBrowser for help on using the repository browser.