Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Classification-Extensions/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/ClassificationPerformanceMeasuresCalculator.cs @ 11685

Last change on this file since 11685 was 11685, checked in by ehopf, 9 years ago

#2278: improvements on the classification performance measures encapsulation

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