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 |
|
---|
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;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
30 | /// <summary>
|
---|
31 | /// Represents a classification data analysis solution
|
---|
32 | /// </summary>
|
---|
33 | [StorableClass]
|
---|
34 | public class ClassificationSolution : DataAnalysisSolution, IClassificationSolution {
|
---|
35 | private const string TrainingAccuracyResultName = "Accuracy (training)";
|
---|
36 | private const string TestAccuracyResultName = "Accuracy (test)";
|
---|
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 | protected set { base.ProblemData = value; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public double TrainingAccuracy {
|
---|
49 | get { return ((DoubleValue)this[TrainingAccuracyResultName].Value).Value; }
|
---|
50 | private set { ((DoubleValue)this[TrainingAccuracyResultName].Value).Value = value; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public double TestAccuracy {
|
---|
54 | get { return ((DoubleValue)this[TestAccuracyResultName].Value).Value; }
|
---|
55 | private set { ((DoubleValue)this[TestAccuracyResultName].Value).Value = value; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | [StorableConstructor]
|
---|
59 | protected ClassificationSolution(bool deserializing) : base(deserializing) { }
|
---|
60 | protected ClassificationSolution(ClassificationSolution original, Cloner cloner)
|
---|
61 | : base(original, cloner) {
|
---|
62 | }
|
---|
63 | public ClassificationSolution(IClassificationModel model, IClassificationProblemData problemData)
|
---|
64 | : base(model, problemData) {
|
---|
65 | Add(new Result(TrainingAccuracyResultName, "Accuracy of the model on the training partition (percentage of correctly classified instances).", new PercentValue()));
|
---|
66 | Add(new Result(TestAccuracyResultName, "Accuracy of the model on the test partition (percentage of correctly classified instances).", new PercentValue()));
|
---|
67 | CalculateResults();
|
---|
68 | }
|
---|
69 |
|
---|
70 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
71 | return new ClassificationSolution(this, cloner);
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected override void RecalculateResults() {
|
---|
75 | CalculateResults();
|
---|
76 | }
|
---|
77 |
|
---|
78 | private void CalculateResults() {
|
---|
79 | double[] estimatedTrainingClassValues = EstimatedTrainingClassValues.ToArray(); // cache values
|
---|
80 | IEnumerable<double> originalTrainingClassValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes);
|
---|
81 | double[] estimatedTestClassValues = EstimatedTestClassValues.ToArray(); // cache values
|
---|
82 | IEnumerable<double> originalTestClassValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TestIndizes);
|
---|
83 |
|
---|
84 | OnlineCalculatorError errorState;
|
---|
85 | double trainingAccuracy = OnlineAccuracyCalculator.Calculate(estimatedTrainingClassValues, originalTrainingClassValues, out errorState);
|
---|
86 | if (errorState != OnlineCalculatorError.None) trainingAccuracy = double.NaN;
|
---|
87 | double testAccuracy = OnlineAccuracyCalculator.Calculate(estimatedTestClassValues, originalTestClassValues, out errorState);
|
---|
88 | if (errorState != OnlineCalculatorError.None) testAccuracy = double.NaN;
|
---|
89 |
|
---|
90 | TrainingAccuracy = trainingAccuracy;
|
---|
91 | TestAccuracy = testAccuracy;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public virtual IEnumerable<double> EstimatedClassValues {
|
---|
95 | get {
|
---|
96 | return GetEstimatedClassValues(Enumerable.Range(0, ProblemData.Dataset.Rows));
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | public virtual IEnumerable<double> EstimatedTrainingClassValues {
|
---|
101 | get {
|
---|
102 | return GetEstimatedClassValues(ProblemData.TrainingIndizes);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | public virtual IEnumerable<double> EstimatedTestClassValues {
|
---|
107 | get {
|
---|
108 | return GetEstimatedClassValues(ProblemData.TestIndizes);
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | public virtual IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows) {
|
---|
113 | return Model.GetEstimatedClassValues(ProblemData.Dataset, rows);
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|