Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3027-NormalDistribution/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/DiscriminantFunctionClassificationSolution.cs @ 17399

Last change on this file since 17399 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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