Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/ClassificationPerformanceMeasuresCalculator.cs @ 18189

Last change on this file since 18189 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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