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