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