Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationSolutionBase.cs @ 12440

Last change on this file since 12440 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 8.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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        CalculateClassificationResults();
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.PositiveClass;
109      double positiveClassValue = ProblemData.GetClassValue(positiveClassName);
110      ClassificationPerformanceMeasuresCalculator trainingPerformanceCalculator = new ClassificationPerformanceMeasuresCalculator(positiveClassName, positiveClassValue);
111      ClassificationPerformanceMeasuresCalculator testPerformanceCalculator = new ClassificationPerformanceMeasuresCalculator(positiveClassName, positiveClassValue);
112
113      OnlineCalculatorError errorState;
114      double trainingAccuracy = OnlineAccuracyCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues, out errorState);
115      if (errorState != OnlineCalculatorError.None) trainingAccuracy = double.NaN;
116      double testAccuracy = OnlineAccuracyCalculator.Calculate(originalTestClassValues, estimatedTestClassValues, out errorState);
117      if (errorState != OnlineCalculatorError.None) testAccuracy = double.NaN;
118
119      TrainingAccuracy = trainingAccuracy;
120      TestAccuracy = testAccuracy;
121
122      double trainingNormalizedGini = NormalizedGiniCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues, out errorState);
123      if (errorState != OnlineCalculatorError.None) trainingNormalizedGini = double.NaN;
124      double testNormalizedGini = NormalizedGiniCalculator.Calculate(originalTestClassValues, estimatedTestClassValues, out errorState);
125      if (errorState != OnlineCalculatorError.None) testNormalizedGini = double.NaN;
126
127      TrainingNormalizedGiniCoefficient = trainingNormalizedGini;
128      TestNormalizedGiniCoefficient = testNormalizedGini;
129
130      trainingPerformanceCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues);
131      if (trainingPerformanceCalculator.ErrorState == OnlineCalculatorError.None)
132        ClassificationPerformanceMeasures.SetTrainingResults(trainingPerformanceCalculator);
133
134      testPerformanceCalculator.Calculate(originalTestClassValues, estimatedTestClassValues);
135      if (testPerformanceCalculator.ErrorState == OnlineCalculatorError.None)
136        ClassificationPerformanceMeasures.SetTestResults(testPerformanceCalculator);
137    }
138
139    public abstract IEnumerable<double> EstimatedClassValues { get; }
140    public abstract IEnumerable<double> EstimatedTrainingClassValues { get; }
141    public abstract IEnumerable<double> EstimatedTestClassValues { get; }
142
143    public abstract IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows);
144
145    protected override void RecalculateResults() {
146      CalculateClassificationResults();
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.