Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Classification-Extensions/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationSolutionBase.cs @ 11622

Last change on this file since 11622 was 11622, checked in by ehopf, 8 years ago

#2278 implemented classification quality measures
-true positive rate
-true negative rate
-positive predictive value
-negative predictive value
-false positive rate
-false discovery rate

File size: 16.3 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis {
30  [StorableClass]
31  public abstract class ClassificationSolutionBase : DataAnalysisSolution, IClassificationSolution {
32    private const string TrainingAccuracyResultName = "Accuracy (training)";
33    private const string TestAccuracyResultName = "Accuracy (test)";
34    private const string TrainingNormalizedGiniCoefficientResultName = "Normalized Gini Coefficient (training)";
35    private const string TestNormalizedGiniCoefficientResultName = "Normalized Gini Coefficient (test)";
36
37    private const string TrainingTruePositiveRateResultName = "True positive rate (training)";
38    private const string TrainingTrueNegativeRateResultName = "True negative rate (training)";
39    private const string TrainingPositivePredictiveValueResultName = "Positive predictive value (training)";
40    private const string TrainingNegativePredictiveValueResultName = "Negative predictive value (training)";
41    private const string TrainingFalsePositiveRateResultName = "False positive rate (training)";
42    private const string TrainingFalseDiscoveryRateResultName = "False discovery rate (training)";
43    private const string TestTruePositiveRateResultName = "True positive rate (test)";
44    private const string TestTrueNegativeRateResultName = "True negative rate (test)";
45    private const string TestPositivePredictiveValueResultName = "Positive predictive value (test)";
46    private const string TestNegativePredictiveValueResultName = "Negative predictive value (test)";
47    private const string TestFalsePositiveRateResultName = "False positive rate (test)";
48    private const string TestFalseDiscoveryRateResultName = "False discovery rate (test)";
49    private const string QualityMeasuresResultName = "Classification Quality Measures";
50
51    public new IClassificationModel Model {
52      get { return (IClassificationModel)base.Model; }
53      protected set { base.Model = value; }
54    }
55
56    public new IClassificationProblemData ProblemData {
57      get { return (IClassificationProblemData)base.ProblemData; }
58      set { base.ProblemData = value; }
59    }
60
61    #region Results
62    public double TrainingAccuracy {
63      get { return ((DoubleValue)this[TrainingAccuracyResultName].Value).Value; }
64      private set { ((DoubleValue)this[TrainingAccuracyResultName].Value).Value = value; }
65    }
66    public double TestAccuracy {
67      get { return ((DoubleValue)this[TestAccuracyResultName].Value).Value; }
68      private set { ((DoubleValue)this[TestAccuracyResultName].Value).Value = value; }
69    }
70    public double TrainingNormalizedGiniCoefficient {
71      get { return ((DoubleValue)this[TrainingNormalizedGiniCoefficientResultName].Value).Value; }
72      protected set { ((DoubleValue)this[TrainingNormalizedGiniCoefficientResultName].Value).Value = value; }
73    }
74    public double TestNormalizedGiniCoefficient {
75      get { return ((DoubleValue)this[TestNormalizedGiniCoefficientResultName].Value).Value; }
76      protected set { ((DoubleValue)this[TestNormalizedGiniCoefficientResultName].Value).Value = value; }
77    }
78
79    #region Quality Measures
80    public ResultCollection QualityMeasures {
81      get { return ((ResultCollection)this[QualityMeasuresResultName].Value); }
82      protected set { (this[QualityMeasuresResultName].Value) = value; }
83    }
84
85    public double TrainingTruePositiveRate {
86      get { return ((DoubleValue)QualityMeasures[TrainingTruePositiveRateResultName].Value).Value; }
87      protected set { ((DoubleValue)QualityMeasures[TrainingTruePositiveRateResultName].Value).Value = value; }
88    }
89    public double TrainingTrueNegativeRate {
90      get { return ((DoubleValue)QualityMeasures[TrainingTrueNegativeRateResultName].Value).Value; }
91      protected set { ((DoubleValue)QualityMeasures[TrainingTrueNegativeRateResultName].Value).Value = value; }
92    }
93    public double TrainingPositivePredictiveValue {
94      get { return ((DoubleValue)QualityMeasures[TrainingPositivePredictiveValueResultName].Value).Value; }
95      protected set { ((DoubleValue)QualityMeasures[TrainingPositivePredictiveValueResultName].Value).Value = value; }
96    }
97    public double TrainingNegativePredictiveValue {
98      get { return ((DoubleValue)QualityMeasures[TrainingNegativePredictiveValueResultName].Value).Value; }
99      protected set { ((DoubleValue)QualityMeasures[TrainingNegativePredictiveValueResultName].Value).Value = value; }
100    }
101    public double TrainingFalsePositiveRate {
102      get { return ((DoubleValue)QualityMeasures[TrainingFalsePositiveRateResultName].Value).Value; }
103      protected set { ((DoubleValue)QualityMeasures[TrainingFalsePositiveRateResultName].Value).Value = value; }
104    }
105    public double TrainingFalseDiscoveryRate {
106      get { return ((DoubleValue)QualityMeasures[TrainingFalseDiscoveryRateResultName].Value).Value; }
107      protected set { ((DoubleValue)QualityMeasures[TrainingFalseDiscoveryRateResultName].Value).Value = value; }
108    }
109
110    public double TestTruePositiveRate {
111      get { return ((DoubleValue)QualityMeasures[TestTruePositiveRateResultName].Value).Value; }
112      protected set { ((DoubleValue)QualityMeasures[TestTruePositiveRateResultName].Value).Value = value; }
113    }
114    public double TestTrueNegativeRate {
115      get { return ((DoubleValue)QualityMeasures[TestTrueNegativeRateResultName].Value).Value; }
116      protected set { ((DoubleValue)QualityMeasures[TestTrueNegativeRateResultName].Value).Value = value; }
117    }
118    public double TestPositivePredictiveValue {
119      get { return ((DoubleValue)QualityMeasures[TestPositivePredictiveValueResultName].Value).Value; }
120      protected set { ((DoubleValue)QualityMeasures[TestPositivePredictiveValueResultName].Value).Value = value; }
121    }
122    public double TestNegativePredictiveValue {
123      get { return ((DoubleValue)QualityMeasures[TestNegativePredictiveValueResultName].Value).Value; }
124      protected set { ((DoubleValue)QualityMeasures[TestNegativePredictiveValueResultName].Value).Value = value; }
125    }
126    public double TestFalsePositiveRate {
127      get { return ((DoubleValue)QualityMeasures[TestFalsePositiveRateResultName].Value).Value; }
128      protected set { ((DoubleValue)QualityMeasures[TestFalsePositiveRateResultName].Value).Value = value; }
129    }
130    public double TestFalseDiscoveryRate {
131      get { return ((DoubleValue)QualityMeasures[TestFalseDiscoveryRateResultName].Value).Value; }
132      protected set { ((DoubleValue)QualityMeasures[TestFalseDiscoveryRateResultName].Value).Value = value; }
133    }
134    #endregion
135    #endregion
136
137    [StorableConstructor]
138    protected ClassificationSolutionBase(bool deserializing) : base(deserializing) { }
139    protected ClassificationSolutionBase(ClassificationSolutionBase original, Cloner cloner)
140      : base(original, cloner) {
141    }
142    protected ClassificationSolutionBase(IClassificationModel model, IClassificationProblemData problemData)
143      : base(model, problemData) {
144      Add(new Result(TrainingAccuracyResultName, "Accuracy of the model on the training partition (percentage of correctly classified instances).", new PercentValue()));
145      Add(new Result(TestAccuracyResultName, "Accuracy of the model on the test partition (percentage of correctly classified instances).", new PercentValue()));
146      Add(new Result(TrainingNormalizedGiniCoefficientResultName, "Normalized Gini coefficient of the model on the training partition.", new DoubleValue()));
147      Add(new Result(TestNormalizedGiniCoefficientResultName, "Normalized Gini coefficient of the model on the test partition.", new DoubleValue()));
148      AddQualityMeasuresResultCollection();
149    }
150
151    [StorableHook(HookType.AfterDeserialization)]
152    private void AfterDeserialization() {
153      if (!this.ContainsKey(TrainingNormalizedGiniCoefficientResultName))
154        Add(new Result(TrainingNormalizedGiniCoefficientResultName, "Normalized Gini coefficient of the model on the training partition.", new DoubleValue()));
155      if (!this.ContainsKey(TestNormalizedGiniCoefficientResultName))
156        Add(new Result(TestNormalizedGiniCoefficientResultName, "Normalized Gini coefficient of the model on the test partition.", new DoubleValue()));
157      if (!this.ContainsKey(QualityMeasuresResultName))
158        AddQualityMeasuresResultCollection();
159    }
160
161    protected void AddQualityMeasuresResultCollection() {
162      ResultCollection qualityMeasuresResult = new ResultCollection();
163      qualityMeasuresResult.Add(new Result(TrainingTruePositiveRateResultName, "Sensitivity/True positive rate of the model on the training partition\n(TP/(TP+FN)).", new PercentValue()));
164      qualityMeasuresResult.Add(new Result(TrainingTrueNegativeRateResultName, "Specificity/True negative rate of the model on the training partition\n(TN/(FP+TN)).", new PercentValue()));
165      qualityMeasuresResult.Add(new Result(TrainingPositivePredictiveValueResultName, "Precision/Positive predictive value of the model on the training partition\n(TP/(TP+FP)).", new PercentValue()));
166      qualityMeasuresResult.Add(new Result(TrainingNegativePredictiveValueResultName, "Negative predictive value of the model on the training partition\n(TN/(TN+FN)).", new PercentValue()));
167      qualityMeasuresResult.Add(new Result(TrainingFalsePositiveRateResultName, "The false positive rate is the complement of the true negative rate of the model on the training partition.", new PercentValue()));
168      qualityMeasuresResult.Add(new Result(TrainingFalseDiscoveryRateResultName, "The false discovery rate is the complement of the positive predictive value of the model on the training partition.", new PercentValue()));
169      qualityMeasuresResult.Add(new Result(TestTruePositiveRateResultName, "Sensitivity/True positive rate of the model on the test partition\n(TP/(TP+FN)).", new PercentValue()));
170      qualityMeasuresResult.Add(new Result(TestTrueNegativeRateResultName, "Specificity/True negative rate of the model on the test partition\n(TN/(FP+TN)).", new PercentValue()));
171      qualityMeasuresResult.Add(new Result(TestPositivePredictiveValueResultName, "Precision/Positive predictive value of the model on the test partition\n(TP/(TP+FP)).", new PercentValue()));
172      qualityMeasuresResult.Add(new Result(TestNegativePredictiveValueResultName, "Negative predictive value of the model on the test partition\n(TN/(TN+FN)).", new PercentValue()));
173      qualityMeasuresResult.Add(new Result(TestFalsePositiveRateResultName, "The false positive rate is the complement of the true negative rate of the model on the test partition.", new PercentValue()));
174      qualityMeasuresResult.Add(new Result(TestFalseDiscoveryRateResultName, "The false discovery rate is the complement of the positive predictive value of the model on the test partition.", new PercentValue()));
175      Add(new Result(QualityMeasuresResultName, "Classification quality measures.\nIn Multiclass Classification all misclassifications of the negative class will be treated as true negatives.", qualityMeasuresResult));
176    }
177
178    protected void CalculateClassificationResults() {
179      double[] estimatedTrainingClassValues = EstimatedTrainingClassValues.ToArray(); // cache values
180      double[] originalTrainingClassValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToArray();
181
182      double[] estimatedTestClassValues = EstimatedTestClassValues.ToArray(); // cache values
183      double[] originalTestClassValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices).ToArray();
184
185      var positiveClassName = ProblemData.PositiveClassName;
186      double positiveClassValue = ProblemData.GetClassValue(positiveClassName);
187      QualityCalculator trainingQualityCalculator = new QualityCalculator(positiveClassValue);
188      QualityCalculator testQualityCalculator = new QualityCalculator(positiveClassValue);
189
190      OnlineCalculatorError errorState;
191      double trainingAccuracy = OnlineAccuracyCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues, out errorState);
192      if (errorState != OnlineCalculatorError.None) trainingAccuracy = double.NaN;
193      double testAccuracy = OnlineAccuracyCalculator.Calculate(originalTestClassValues, estimatedTestClassValues, out errorState);
194      if (errorState != OnlineCalculatorError.None) testAccuracy = double.NaN;
195
196      TrainingAccuracy = trainingAccuracy;
197      TestAccuracy = testAccuracy;
198
199      double trainingNormalizedGini = NormalizedGiniCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues, out errorState);
200      if (errorState != OnlineCalculatorError.None) trainingNormalizedGini = double.NaN;
201      double testNormalizedGini = NormalizedGiniCalculator.Calculate(originalTestClassValues, estimatedTestClassValues, out errorState);
202      if (errorState != OnlineCalculatorError.None) testNormalizedGini = double.NaN;
203
204      TrainingNormalizedGiniCoefficient = trainingNormalizedGini;
205      TestNormalizedGiniCoefficient = testNormalizedGini;
206
207      //quality measures training partition
208      trainingQualityCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues, out errorState);
209      if (errorState != OnlineCalculatorError.None) {
210        TrainingTruePositiveRate = double.NaN;
211        TrainingTrueNegativeRate = double.NaN;
212        TrainingPositivePredictiveValue = double.NaN;
213        TrainingNegativePredictiveValue = double.NaN;
214        TrainingFalsePositiveRate = double.NaN;
215        TrainingFalseDiscoveryRate = double.NaN;
216      } else {
217        TrainingTruePositiveRate = trainingQualityCalculator.TruePositiveRate;
218        TrainingTrueNegativeRate = trainingQualityCalculator.TrueNegativeRate;
219        TrainingPositivePredictiveValue = trainingQualityCalculator.PositivePredictiveValue;
220        TrainingNegativePredictiveValue = trainingQualityCalculator.NegativePredictiveValue;
221        TrainingFalsePositiveRate = trainingQualityCalculator.FalsePositiveRate;
222        TrainingFalseDiscoveryRate = trainingQualityCalculator.FalseDiscoveryRate;
223      }
224      //quality measures test partition
225      testQualityCalculator.Calculate(originalTestClassValues, estimatedTestClassValues, out errorState);
226      if (errorState != OnlineCalculatorError.None) {
227        TestTruePositiveRate = double.NaN;
228        TestTrueNegativeRate = double.NaN;
229        TestPositivePredictiveValue = double.NaN;
230        TestNegativePredictiveValue = double.NaN;
231        TestFalsePositiveRate = double.NaN;
232        TestFalseDiscoveryRate = double.NaN;
233      } else {
234        TestTruePositiveRate = testQualityCalculator.TruePositiveRate;
235        TestTrueNegativeRate = testQualityCalculator.TrueNegativeRate;
236        TestPositivePredictiveValue = testQualityCalculator.PositivePredictiveValue;
237        TestNegativePredictiveValue = testQualityCalculator.NegativePredictiveValue;
238        TestFalsePositiveRate = testQualityCalculator.FalsePositiveRate;
239        TestFalseDiscoveryRate = testQualityCalculator.FalseDiscoveryRate;
240      }
241    }
242
243    public abstract IEnumerable<double> EstimatedClassValues { get; }
244    public abstract IEnumerable<double> EstimatedTrainingClassValues { get; }
245    public abstract IEnumerable<double> EstimatedTestClassValues { get; }
246
247    public abstract IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows);
248
249    protected override void RecalculateResults() {
250      CalculateClassificationResults();
251    }
252  }
253}
Note: See TracBrowser for help on using the repository browser.