[5649] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5649] | 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;
|
---|
[6411] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[5649] | 27 |
|
---|
| 28 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 29 | /// <summary>
|
---|
| 30 | /// Represents a classification solution that uses a discriminant function and classification thresholds.
|
---|
| 31 | /// </summary>
|
---|
| 32 | [StorableClass]
|
---|
| 33 | [Item("DiscriminantFunctionClassificationSolution", "Represents a classification solution that uses a discriminant function and classification thresholds.")]
|
---|
[8679] | 34 | public class DiscriminantFunctionClassificationSolution : DiscriminantFunctionClassificationSolutionBase {
|
---|
[6606] | 35 | protected readonly Dictionary<int, double> valueEvaluationCache;
|
---|
| 36 | protected readonly Dictionary<int, double> classValueEvaluationCache;
|
---|
[5885] | 37 |
|
---|
[5649] | 38 | [StorableConstructor]
|
---|
[6606] | 39 | protected DiscriminantFunctionClassificationSolution(bool deserializing)
|
---|
| 40 | : base(deserializing) {
|
---|
| 41 | valueEvaluationCache = new Dictionary<int, double>();
|
---|
| 42 | classValueEvaluationCache = new Dictionary<int, double>();
|
---|
| 43 | }
|
---|
[5649] | 44 | protected DiscriminantFunctionClassificationSolution(DiscriminantFunctionClassificationSolution original, Cloner cloner)
|
---|
| 45 | : base(original, cloner) {
|
---|
[6606] | 46 | valueEvaluationCache = new Dictionary<int, double>(original.valueEvaluationCache);
|
---|
| 47 | classValueEvaluationCache = new Dictionary<int, double>(original.classValueEvaluationCache);
|
---|
[5649] | 48 | }
|
---|
[8679] | 49 | public DiscriminantFunctionClassificationSolution(IDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData)
|
---|
[5649] | 50 | : base(model, problemData) {
|
---|
[6606] | 51 | valueEvaluationCache = new Dictionary<int, double>();
|
---|
| 52 | classValueEvaluationCache = new Dictionary<int, double>();
|
---|
[8723] | 53 | CalculateRegressionResults();
|
---|
| 54 | CalculateClassificationResults();
|
---|
[5649] | 55 | }
|
---|
| 56 |
|
---|
[8683] | 57 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 58 | return new DiscriminantFunctionClassificationSolution(this, cloner);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[6589] | 61 | public override IEnumerable<double> EstimatedClassValues {
|
---|
| 62 | get { return GetEstimatedClassValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
|
---|
[5736] | 63 | }
|
---|
[6589] | 64 | public override IEnumerable<double> EstimatedTrainingClassValues {
|
---|
[8139] | 65 | get { return GetEstimatedClassValues(ProblemData.TrainingIndices); }
|
---|
[6411] | 66 | }
|
---|
[6589] | 67 | public override IEnumerable<double> EstimatedTestClassValues {
|
---|
[8139] | 68 | get { return GetEstimatedClassValues(ProblemData.TestIndices); }
|
---|
[6411] | 69 | }
|
---|
| 70 |
|
---|
[6589] | 71 | public override IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows) {
|
---|
[6606] | 72 | var rowsToEvaluate = rows.Except(classValueEvaluationCache.Keys);
|
---|
| 73 | var rowsEnumerator = rowsToEvaluate.GetEnumerator();
|
---|
| 74 | var valuesEnumerator = Model.GetEstimatedClassValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator();
|
---|
| 75 |
|
---|
| 76 | while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
|
---|
| 77 | classValueEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | return rows.Select(row => classValueEvaluationCache[row]);
|
---|
[5885] | 81 | }
|
---|
| 82 |
|
---|
[5736] | 83 |
|
---|
[6589] | 84 | public override IEnumerable<double> EstimatedValues {
|
---|
[5649] | 85 | get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
|
---|
| 86 | }
|
---|
[6589] | 87 | public override IEnumerable<double> EstimatedTrainingValues {
|
---|
[8139] | 88 | get { return GetEstimatedValues(ProblemData.TrainingIndices); }
|
---|
[5649] | 89 | }
|
---|
[6589] | 90 | public override IEnumerable<double> EstimatedTestValues {
|
---|
[8139] | 91 | get { return GetEstimatedValues(ProblemData.TestIndices); }
|
---|
[5649] | 92 | }
|
---|
| 93 |
|
---|
[6589] | 94 | public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
[6606] | 95 | var rowsToEvaluate = rows.Except(valueEvaluationCache.Keys);
|
---|
| 96 | var rowsEnumerator = rowsToEvaluate.GetEnumerator();
|
---|
| 97 | var valuesEnumerator = Model.GetEstimatedValues(ProblemData.Dataset, rowsToEvaluate).GetEnumerator();
|
---|
| 98 |
|
---|
| 99 | while (rowsEnumerator.MoveNext() & valuesEnumerator.MoveNext()) {
|
---|
| 100 | valueEvaluationCache.Add(rowsEnumerator.Current, valuesEnumerator.Current);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | return rows.Select(row => valueEvaluationCache[row]);
|
---|
[5649] | 104 | }
|
---|
[6606] | 105 |
|
---|
| 106 | protected override void OnModelChanged() {
|
---|
| 107 | valueEvaluationCache.Clear();
|
---|
| 108 | classValueEvaluationCache.Clear();
|
---|
| 109 | base.OnModelChanged();
|
---|
| 110 | }
|
---|
| 111 | protected override void OnModelThresholdsChanged(System.EventArgs e) {
|
---|
| 112 | classValueEvaluationCache.Clear();
|
---|
| 113 | base.OnModelThresholdsChanged(e);
|
---|
| 114 | }
|
---|
| 115 | protected override void OnProblemDataChanged() {
|
---|
| 116 | valueEvaluationCache.Clear();
|
---|
| 117 | classValueEvaluationCache.Clear();
|
---|
| 118 | base.OnProblemDataChanged();
|
---|
| 119 | }
|
---|
[5649] | 120 | }
|
---|
| 121 | }
|
---|