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