Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2278: Encapsulated the classification performance measures to the ClassificationPerformanceMeasuresResultCollection

File size: 10.1 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    private const string ClassificationPerformanceMeasuresResultName = "Classification Performance Measures";
37
38    public new IClassificationModel Model {
39      get { return (IClassificationModel)base.Model; }
40      protected set { base.Model = value; }
41    }
42
43    public new IClassificationProblemData ProblemData {
44      get { return (IClassificationProblemData)base.ProblemData; }
45      set { base.ProblemData = value; }
46    }
47
48    #region Results
49    public double TrainingAccuracy {
50      get { return ((DoubleValue)this[TrainingAccuracyResultName].Value).Value; }
51      private set { ((DoubleValue)this[TrainingAccuracyResultName].Value).Value = value; }
52    }
53    public double TestAccuracy {
54      get { return ((DoubleValue)this[TestAccuracyResultName].Value).Value; }
55      private set { ((DoubleValue)this[TestAccuracyResultName].Value).Value = value; }
56    }
57    public double TrainingNormalizedGiniCoefficient {
58      get { return ((DoubleValue)this[TrainingNormalizedGiniCoefficientResultName].Value).Value; }
59      protected set { ((DoubleValue)this[TrainingNormalizedGiniCoefficientResultName].Value).Value = value; }
60    }
61    public double TestNormalizedGiniCoefficient {
62      get { return ((DoubleValue)this[TestNormalizedGiniCoefficientResultName].Value).Value; }
63      protected set { ((DoubleValue)this[TestNormalizedGiniCoefficientResultName].Value).Value = value; }
64    }
65    public ClassificationPerformanceMeasuresResultCollection ClassificationPerformanceMeasures {
66      get { return ((ClassificationPerformanceMeasuresResultCollection)this[ClassificationPerformanceMeasuresResultName].Value); }
67      protected set { (this[ClassificationPerformanceMeasuresResultName].Value) = value; }
68    }
69    #endregion
70
71    [StorableConstructor]
72    protected ClassificationSolutionBase(bool deserializing) : base(deserializing) { }
73    protected ClassificationSolutionBase(ClassificationSolutionBase original, Cloner cloner)
74      : base(original, cloner) {
75    }
76    protected ClassificationSolutionBase(IClassificationModel model, IClassificationProblemData problemData)
77      : base(model, problemData) {
78      Add(new Result(TrainingAccuracyResultName, "Accuracy of the model on the training partition (percentage of correctly classified instances).", new PercentValue()));
79      Add(new Result(TestAccuracyResultName, "Accuracy of the model on the test partition (percentage of correctly classified instances).", new PercentValue()));
80      Add(new Result(TrainingNormalizedGiniCoefficientResultName, "Normalized Gini coefficient of the model on the training partition.", new DoubleValue()));
81      Add(new Result(TestNormalizedGiniCoefficientResultName, "Normalized Gini coefficient of the model on the test partition.", new DoubleValue()));
82      Add(new Result(ClassificationPerformanceMeasuresResultName, @"Classification performance measures.\n
83                              In a multiclass classification all misclassifications of the negative class will be treated as true negatives except on positive class estimations.",
84                            new ClassificationPerformanceMeasuresResultCollection()));
85    }
86
87    [StorableHook(HookType.AfterDeserialization)]
88    private void AfterDeserialization() {
89      if (!this.ContainsKey(TrainingNormalizedGiniCoefficientResultName))
90        Add(new Result(TrainingNormalizedGiniCoefficientResultName, "Normalized Gini coefficient of the model on the training partition.", new DoubleValue()));
91      if (!this.ContainsKey(TestNormalizedGiniCoefficientResultName))
92        Add(new Result(TestNormalizedGiniCoefficientResultName, "Normalized Gini coefficient of the model on the test partition.", new DoubleValue()));
93      if (!this.ContainsKey(ClassificationPerformanceMeasuresResultName)) {
94        Add(new Result(ClassificationPerformanceMeasuresResultName, @"Classification performance measures.\n
95                              In a multiclass classification all misclassifications of the negative class will be treated as true negatives except on positive class estimations.",
96                              new ClassificationPerformanceMeasuresResultCollection()));
97        RecalculateResults();
98      }
99    }
100
101    protected void CalculateClassificationResults() {
102      double[] estimatedTrainingClassValues = EstimatedTrainingClassValues.ToArray(); // cache values
103      double[] originalTrainingClassValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToArray();
104
105      double[] estimatedTestClassValues = EstimatedTestClassValues.ToArray(); // cache values
106      double[] originalTestClassValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices).ToArray();
107
108      var positiveClassName = ProblemData.PositiveClassName;
109      double positiveClassValue = ProblemData.GetClassValue(positiveClassName);
110      ClassificationPerformanceMeasures.ClassificationPositiveClassValue = positiveClassName;
111      ClassificationPerformanceMeasuresCalculator trainingPerformanceCalculator = new ClassificationPerformanceMeasuresCalculator(positiveClassValue);
112      ClassificationPerformanceMeasuresCalculator testPerformanceCalculator = new ClassificationPerformanceMeasuresCalculator(positiveClassValue);
113
114      OnlineCalculatorError errorState;
115      double trainingAccuracy = OnlineAccuracyCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues, out errorState);
116      if (errorState != OnlineCalculatorError.None) trainingAccuracy = double.NaN;
117      double testAccuracy = OnlineAccuracyCalculator.Calculate(originalTestClassValues, estimatedTestClassValues, out errorState);
118      if (errorState != OnlineCalculatorError.None) testAccuracy = double.NaN;
119
120      TrainingAccuracy = trainingAccuracy;
121      TestAccuracy = testAccuracy;
122
123      double trainingNormalizedGini = NormalizedGiniCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues, out errorState);
124      if (errorState != OnlineCalculatorError.None) trainingNormalizedGini = double.NaN;
125      double testNormalizedGini = NormalizedGiniCalculator.Calculate(originalTestClassValues, estimatedTestClassValues, out errorState);
126      if (errorState != OnlineCalculatorError.None) testNormalizedGini = double.NaN;
127
128      TrainingNormalizedGiniCoefficient = trainingNormalizedGini;
129      TestNormalizedGiniCoefficient = testNormalizedGini;
130
131      trainingPerformanceCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues, out errorState);
132      if (errorState == OnlineCalculatorError.None) {
133        ClassificationPerformanceMeasures.TrainingTruePositiveRate = trainingPerformanceCalculator.TruePositiveRate;
134        ClassificationPerformanceMeasures.TrainingTrueNegativeRate = trainingPerformanceCalculator.TrueNegativeRate;
135        ClassificationPerformanceMeasures.TrainingPositivePredictiveValue = trainingPerformanceCalculator.PositivePredictiveValue;
136        ClassificationPerformanceMeasures.TrainingNegativePredictiveValue = trainingPerformanceCalculator.NegativePredictiveValue;
137        ClassificationPerformanceMeasures.TrainingFalsePositiveRate = trainingPerformanceCalculator.FalsePositiveRate;
138        ClassificationPerformanceMeasures.TrainingFalseDiscoveryRate = trainingPerformanceCalculator.FalseDiscoveryRate;
139      }
140      testPerformanceCalculator.Calculate(originalTestClassValues, estimatedTestClassValues, out errorState);
141      if (errorState == OnlineCalculatorError.None) {
142        ClassificationPerformanceMeasures.TestTruePositiveRate = testPerformanceCalculator.TruePositiveRate;
143        ClassificationPerformanceMeasures.TestTrueNegativeRate = testPerformanceCalculator.TrueNegativeRate;
144        ClassificationPerformanceMeasures.TestPositivePredictiveValue = testPerformanceCalculator.PositivePredictiveValue;
145        ClassificationPerformanceMeasures.TestNegativePredictiveValue = testPerformanceCalculator.NegativePredictiveValue;
146        ClassificationPerformanceMeasures.TestFalsePositiveRate = testPerformanceCalculator.FalsePositiveRate;
147        ClassificationPerformanceMeasures.TestFalseDiscoveryRate = testPerformanceCalculator.FalseDiscoveryRate;
148      }
149    }
150
151    public abstract IEnumerable<double> EstimatedClassValues { get; }
152    public abstract IEnumerable<double> EstimatedTrainingClassValues { get; }
153    public abstract IEnumerable<double> EstimatedTestClassValues { get; }
154
155    public abstract IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows);
156
157    protected override void RecalculateResults() {
158      CalculateClassificationResults();
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.