Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2278

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