Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6604 was 6589, checked in by mkommend, 13 years ago

#1600: Adapted classification solutions to the same design as used by regression solutions.

File size: 4.3 KB
RevLine 
[6589]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
35    public new IClassificationModel Model {
36      get { return (IClassificationModel)base.Model; }
37      protected set { base.Model = value; }
38    }
39
40    public new IClassificationProblemData ProblemData {
41      get { return (IClassificationProblemData)base.ProblemData; }
42      protected set { base.ProblemData = value; }
43    }
44
45    #region Results
46    public double TrainingAccuracy {
47      get { return ((DoubleValue)this[TrainingAccuracyResultName].Value).Value; }
48      private set { ((DoubleValue)this[TrainingAccuracyResultName].Value).Value = value; }
49    }
50    public double TestAccuracy {
51      get { return ((DoubleValue)this[TestAccuracyResultName].Value).Value; }
52      private set { ((DoubleValue)this[TestAccuracyResultName].Value).Value = value; }
53    }
54    #endregion
55
56    [StorableConstructor]
57    protected ClassificationSolutionBase(bool deserializing) : base(deserializing) { }
58    protected ClassificationSolutionBase(ClassificationSolutionBase original, Cloner cloner)
59      : base(original, cloner) {
60    }
61    protected ClassificationSolutionBase(IClassificationModel model, IClassificationProblemData problemData)
62      : base(model, problemData) {
63      Add(new Result(TrainingAccuracyResultName, "Accuracy of the model on the training partition (percentage of correctly classified instances).", new PercentValue()));
64      Add(new Result(TestAccuracyResultName, "Accuracy of the model on the test partition (percentage of correctly classified instances).", new PercentValue()));
65    }
66
67    protected void CalculateResults() {
68      double[] estimatedTrainingClassValues = EstimatedTrainingClassValues.ToArray(); // cache values
69      double[] originalTrainingClassValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToArray();
70      double[] estimatedTestClassValues = EstimatedTestClassValues.ToArray(); // cache values
71      double[] originalTestClassValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TestIndizes).ToArray();
72
73      OnlineCalculatorError errorState;
74      double trainingAccuracy = OnlineAccuracyCalculator.Calculate(estimatedTrainingClassValues, originalTrainingClassValues, out errorState);
75      if (errorState != OnlineCalculatorError.None) trainingAccuracy = double.NaN;
76      double testAccuracy = OnlineAccuracyCalculator.Calculate(estimatedTestClassValues, originalTestClassValues, out errorState);
77      if (errorState != OnlineCalculatorError.None) testAccuracy = double.NaN;
78
79      TrainingAccuracy = trainingAccuracy;
80      TestAccuracy = testAccuracy;
81    }
82
83    public abstract IEnumerable<double> EstimatedClassValues { get; }
84    public abstract IEnumerable<double> EstimatedTrainingClassValues { get; }
85    public abstract IEnumerable<double> EstimatedTestClassValues { get; }
86
87    public abstract IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows);
88  }
89}
Note: See TracBrowser for help on using the repository browser.