Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/DiscriminantFunctionClassificationSolution.cs @ 17604

Last change on this file since 17604 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 5.2 KB
RevLine 
[5649]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) 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
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
[17097]26using HEAL.Attic;
[5649]27
28namespace HeuristicLab.Problems.DataAnalysis {
29  /// <summary>
30  /// Represents a classification solution that uses a discriminant function and classification thresholds.
31  /// </summary>
[17097]32  [StorableType("A3480DF9-49E7-4329-AD23-57B4441033C1")]
[5649]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]
[17097]39    protected DiscriminantFunctionClassificationSolution(StorableConstructorFlag _) : base(_) {
[6606]40      valueEvaluationCache = new Dictionary<int, double>();
41      classValueEvaluationCache = new Dictionary<int, double>();
42    }
[5649]43    protected DiscriminantFunctionClassificationSolution(DiscriminantFunctionClassificationSolution original, Cloner cloner)
44      : base(original, cloner) {
[6606]45      valueEvaluationCache = new Dictionary<int, double>(original.valueEvaluationCache);
46      classValueEvaluationCache = new Dictionary<int, double>(original.classValueEvaluationCache);
[5649]47    }
[8679]48    public DiscriminantFunctionClassificationSolution(IDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData)
[5649]49      : base(model, problemData) {
[6606]50      valueEvaluationCache = new Dictionary<int, double>();
51      classValueEvaluationCache = new Dictionary<int, double>();
[8723]52      CalculateRegressionResults();
53      CalculateClassificationResults();
[5649]54    }
55
[8683]56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new DiscriminantFunctionClassificationSolution(this, cloner);
58    }
59
[6589]60    public override IEnumerable<double> EstimatedClassValues {
61      get { return GetEstimatedClassValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
[5736]62    }
[6589]63    public override IEnumerable<double> EstimatedTrainingClassValues {
[8139]64      get { return GetEstimatedClassValues(ProblemData.TrainingIndices); }
[6411]65    }
[6589]66    public override IEnumerable<double> EstimatedTestClassValues {
[8139]67      get { return GetEstimatedClassValues(ProblemData.TestIndices); }
[6411]68    }
69
[6589]70    public override IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows) {
[6606]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]);
[5885]80    }
81
[5736]82
[6589]83    public override IEnumerable<double> EstimatedValues {
[5649]84      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
85    }
[6589]86    public override IEnumerable<double> EstimatedTrainingValues {
[8139]87      get { return GetEstimatedValues(ProblemData.TrainingIndices); }
[5649]88    }
[6589]89    public override IEnumerable<double> EstimatedTestValues {
[8139]90      get { return GetEstimatedValues(ProblemData.TestIndices); }
[5649]91    }
92
[6589]93    public override IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
[6606]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]);
[5649]103    }
[6606]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    }
[5649]119  }
120}
Note: See TracBrowser for help on using the repository browser.