1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HEAL.Attic;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis.OnlineCalculators;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
32 | [StorableType("60599497-EAF0-4DB0-B2E4-D58F34458D8F")]
|
---|
33 | public abstract class ClassificationSolutionBase : DataAnalysisSolution, IClassificationSolution {
|
---|
34 | private const string TrainingAccuracyResultName = "Accuracy (training)";
|
---|
35 | private const string TestAccuracyResultName = "Accuracy (test)";
|
---|
36 | private const string TrainingNormalizedGiniCoefficientResultName = "Norm. Gini coeff. (training)";
|
---|
37 | private const string TestNormalizedGiniCoefficientResultName = "Norm. Gini coeff. (test)";
|
---|
38 | private const string ClassificationPerformanceMeasuresResultName = "Classification Performance Measures";
|
---|
39 |
|
---|
40 | public new IClassificationModel Model {
|
---|
41 | get { return (IClassificationModel)base.Model; }
|
---|
42 | protected set { base.Model = value; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public new IClassificationProblemData ProblemData {
|
---|
46 | get { return (IClassificationProblemData)base.ProblemData; }
|
---|
47 | set {
|
---|
48 | if (value == null) throw new ArgumentNullException("The problemData must not be null.");
|
---|
49 | string errorMessage = string.Empty;
|
---|
50 | if (!Model.IsProblemDataCompatible(value, out errorMessage)) throw new ArgumentException(errorMessage);
|
---|
51 |
|
---|
52 | base.ProblemData = value;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | #region Results
|
---|
57 | public double TrainingAccuracy {
|
---|
58 | get { return ((DoubleValue)this[TrainingAccuracyResultName].Value).Value; }
|
---|
59 | private set { ((DoubleValue)this[TrainingAccuracyResultName].Value).Value = value; }
|
---|
60 | }
|
---|
61 | public double TestAccuracy {
|
---|
62 | get { return ((DoubleValue)this[TestAccuracyResultName].Value).Value; }
|
---|
63 | private set { ((DoubleValue)this[TestAccuracyResultName].Value).Value = value; }
|
---|
64 | }
|
---|
65 | public double TrainingNormalizedGiniCoefficient {
|
---|
66 | get { return ((DoubleValue)this[TrainingNormalizedGiniCoefficientResultName].Value).Value; }
|
---|
67 | protected set { ((DoubleValue)this[TrainingNormalizedGiniCoefficientResultName].Value).Value = value; }
|
---|
68 | }
|
---|
69 | public double TestNormalizedGiniCoefficient {
|
---|
70 | get { return ((DoubleValue)this[TestNormalizedGiniCoefficientResultName].Value).Value; }
|
---|
71 | protected set { ((DoubleValue)this[TestNormalizedGiniCoefficientResultName].Value).Value = value; }
|
---|
72 | }
|
---|
73 | public ClassificationPerformanceMeasuresResultCollection ClassificationPerformanceMeasures {
|
---|
74 | get { return ((ClassificationPerformanceMeasuresResultCollection)this[ClassificationPerformanceMeasuresResultName].Value); }
|
---|
75 | protected set { (this[ClassificationPerformanceMeasuresResultName].Value) = value; }
|
---|
76 | }
|
---|
77 | #endregion
|
---|
78 |
|
---|
79 | [StorableConstructor]
|
---|
80 | protected ClassificationSolutionBase(StorableConstructorFlag _) : base(_) { }
|
---|
81 | protected ClassificationSolutionBase(ClassificationSolutionBase original, Cloner cloner)
|
---|
82 | : base(original, cloner) {
|
---|
83 | }
|
---|
84 | protected ClassificationSolutionBase(IClassificationModel model, IClassificationProblemData problemData)
|
---|
85 | : base(model, problemData) {
|
---|
86 | Add(new Result(TrainingAccuracyResultName, "Accuracy of the model on the training partition (percentage of correctly classified instances).", new PercentValue()));
|
---|
87 | Add(new Result(TestAccuracyResultName, "Accuracy of the model on the test partition (percentage of correctly classified instances).", new PercentValue()));
|
---|
88 | Add(new Result(TrainingNormalizedGiniCoefficientResultName, "Normalized Gini coefficient of the model on the training partition.", new DoubleValue()));
|
---|
89 | Add(new Result(TestNormalizedGiniCoefficientResultName, "Normalized Gini coefficient of the model on the test partition.", new DoubleValue()));
|
---|
90 | Add(new Result(ClassificationPerformanceMeasuresResultName, @"Classification performance measures.\n
|
---|
91 | In a multiclass classification all misclassifications of the negative class will be treated as true negatives except on positive class estimations.",
|
---|
92 | new ClassificationPerformanceMeasuresResultCollection()));
|
---|
93 | }
|
---|
94 |
|
---|
95 | [StorableHook(HookType.AfterDeserialization)]
|
---|
96 | private void AfterDeserialization() {
|
---|
97 | if (string.IsNullOrEmpty(Model.TargetVariable))
|
---|
98 | Model.TargetVariable = this.ProblemData.TargetVariable;
|
---|
99 | var newResult = false;
|
---|
100 | if (!this.ContainsKey(TrainingNormalizedGiniCoefficientResultName)) {
|
---|
101 | Add(new Result(TrainingNormalizedGiniCoefficientResultName, "Normalized Gini coefficient of the model on the training partition.", new DoubleValue()));
|
---|
102 | newResult = true;
|
---|
103 | }
|
---|
104 | if (!this.ContainsKey(TestNormalizedGiniCoefficientResultName)) {
|
---|
105 | Add(new Result(TestNormalizedGiniCoefficientResultName, "Normalized Gini coefficient of the model on the test partition.", new DoubleValue()));
|
---|
106 | newResult = true;
|
---|
107 | }
|
---|
108 | if (!this.ContainsKey(ClassificationPerformanceMeasuresResultName)) {
|
---|
109 | Add(new Result(ClassificationPerformanceMeasuresResultName, @"Classification performance measures.\n
|
---|
110 | In a multiclass classification all misclassifications of the negative class will be treated as true negatives except on positive class estimations.",
|
---|
111 | new ClassificationPerformanceMeasuresResultCollection()));
|
---|
112 | newResult = true;
|
---|
113 | }
|
---|
114 | if (newResult) CalculateClassificationResults();
|
---|
115 | }
|
---|
116 |
|
---|
117 | protected void CalculateClassificationResults() {
|
---|
118 | double[] estimatedTrainingClassValues = EstimatedTrainingClassValues.ToArray(); // cache values
|
---|
119 | double[] originalTrainingClassValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToArray();
|
---|
120 |
|
---|
121 | double[] estimatedTestClassValues = EstimatedTestClassValues.ToArray(); // cache values
|
---|
122 | double[] originalTestClassValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndices).ToArray();
|
---|
123 |
|
---|
124 | var positiveClassName = ProblemData.PositiveClass;
|
---|
125 | double positiveClassValue = ProblemData.GetClassValue(positiveClassName);
|
---|
126 | ClassificationPerformanceMeasuresCalculator trainingPerformanceCalculator = new ClassificationPerformanceMeasuresCalculator(positiveClassName, positiveClassValue);
|
---|
127 | ClassificationPerformanceMeasuresCalculator testPerformanceCalculator = new ClassificationPerformanceMeasuresCalculator(positiveClassName, positiveClassValue);
|
---|
128 |
|
---|
129 | OnlineCalculatorError errorState;
|
---|
130 | double trainingAccuracy = OnlineAccuracyCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues, out errorState);
|
---|
131 | if (errorState != OnlineCalculatorError.None) trainingAccuracy = double.NaN;
|
---|
132 | double testAccuracy = OnlineAccuracyCalculator.Calculate(originalTestClassValues, estimatedTestClassValues, out errorState);
|
---|
133 | if (errorState != OnlineCalculatorError.None) testAccuracy = double.NaN;
|
---|
134 |
|
---|
135 | TrainingAccuracy = trainingAccuracy;
|
---|
136 | TestAccuracy = testAccuracy;
|
---|
137 |
|
---|
138 | double trainingNormalizedGini = NormalizedGiniCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues, out errorState);
|
---|
139 | if (errorState != OnlineCalculatorError.None) trainingNormalizedGini = double.NaN;
|
---|
140 | double testNormalizedGini = NormalizedGiniCalculator.Calculate(originalTestClassValues, estimatedTestClassValues, out errorState);
|
---|
141 | if (errorState != OnlineCalculatorError.None) testNormalizedGini = double.NaN;
|
---|
142 |
|
---|
143 | TrainingNormalizedGiniCoefficient = trainingNormalizedGini;
|
---|
144 | TestNormalizedGiniCoefficient = testNormalizedGini;
|
---|
145 |
|
---|
146 | ClassificationPerformanceMeasures.Reset();
|
---|
147 |
|
---|
148 | trainingPerformanceCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues);
|
---|
149 | if (trainingPerformanceCalculator.ErrorState == OnlineCalculatorError.None)
|
---|
150 | ClassificationPerformanceMeasures.SetTrainingResults(trainingPerformanceCalculator);
|
---|
151 |
|
---|
152 | testPerformanceCalculator.Calculate(originalTestClassValues, estimatedTestClassValues);
|
---|
153 | if (testPerformanceCalculator.ErrorState == OnlineCalculatorError.None)
|
---|
154 | ClassificationPerformanceMeasures.SetTestResults(testPerformanceCalculator);
|
---|
155 |
|
---|
156 | if (ProblemData.Classes == 2) {
|
---|
157 | var f1Training = FOneScoreCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues, out errorState);
|
---|
158 | if (errorState == OnlineCalculatorError.None) ClassificationPerformanceMeasures.TrainingF1Score = f1Training;
|
---|
159 | var f1Test = FOneScoreCalculator.Calculate(originalTestClassValues, estimatedTestClassValues, out errorState);
|
---|
160 | if (errorState == OnlineCalculatorError.None) ClassificationPerformanceMeasures.TestF1Score = f1Test;
|
---|
161 | }
|
---|
162 |
|
---|
163 | var mccTraining = MatthewsCorrelationCoefficientCalculator.Calculate(originalTrainingClassValues, estimatedTrainingClassValues, out errorState);
|
---|
164 | if (errorState == OnlineCalculatorError.None) ClassificationPerformanceMeasures.TrainingMatthewsCorrelation = mccTraining;
|
---|
165 | var mccTest = MatthewsCorrelationCoefficientCalculator.Calculate(originalTestClassValues, estimatedTestClassValues, out errorState);
|
---|
166 | if (errorState == OnlineCalculatorError.None) ClassificationPerformanceMeasures.TestMatthewsCorrelation = mccTest;
|
---|
167 | }
|
---|
168 |
|
---|
169 | public abstract IEnumerable<double> EstimatedClassValues { get; }
|
---|
170 | public abstract IEnumerable<double> EstimatedTrainingClassValues { get; }
|
---|
171 | public abstract IEnumerable<double> EstimatedTestClassValues { get; }
|
---|
172 |
|
---|
173 | public abstract IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows);
|
---|
174 |
|
---|
175 | protected override void RecalculateResults() {
|
---|
176 | CalculateClassificationResults();
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|