Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/DiscriminantFunctionClassificationSolution.cs @ 5815

Last change on this file since 5815 was 5809, checked in by mkommend, 14 years ago

#1418: Reintegrated branch into trunk.

File size: 5.0 KB
RevLine 
[5649]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
[5777]22using System;
[5649]23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis {
30  /// <summary>
31  /// Represents a classification solution that uses a discriminant function and classification thresholds.
32  /// </summary>
33  [StorableClass]
34  [Item("DiscriminantFunctionClassificationSolution", "Represents a classification solution that uses a discriminant function and classification thresholds.")]
35  public class DiscriminantFunctionClassificationSolution : ClassificationSolution, IDiscriminantFunctionClassificationSolution {
[5717]36    public new IDiscriminantFunctionClassificationModel Model {
37      get { return (IDiscriminantFunctionClassificationModel)base.Model; }
[5736]38      protected set {
39        if (value != null && value != Model) {
40          if (Model != null) {
41            Model.ThresholdsChanged -= new EventHandler(Model_ThresholdsChanged);
42          }
43          value.ThresholdsChanged += new EventHandler(Model_ThresholdsChanged);
44          base.Model = value;
45        }
46      }
[5717]47    }
48
[5649]49    [StorableConstructor]
50    protected DiscriminantFunctionClassificationSolution(bool deserializing) : base(deserializing) { }
51    protected DiscriminantFunctionClassificationSolution(DiscriminantFunctionClassificationSolution original, Cloner cloner)
52      : base(original, cloner) {
[5736]53      RegisterEventHandler();
[5649]54    }
[5736]55    public DiscriminantFunctionClassificationSolution(IRegressionModel model, IClassificationProblemData problemData)
56      : this(new DiscriminantFunctionClassificationModel(model), problemData) {
[5649]57    }
58    public DiscriminantFunctionClassificationSolution(IDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData)
59      : base(model, problemData) {
[5736]60      RegisterEventHandler();
61      SetAccuracyMaximizingThresholds();
[5649]62    }
63
[5736]64    [StorableHook(HookType.AfterDeserialization)]
65    private void AfterDeserialization() {
66      RegisterEventHandler();
67    }
68
69    private void RegisterEventHandler() {
70      Model.ThresholdsChanged += new EventHandler(Model_ThresholdsChanged);
71    }
72    private void Model_ThresholdsChanged(object sender, EventArgs e) {
73      OnModelThresholdsChanged(e);
74    }
75
76    public void SetAccuracyMaximizingThresholds() {
77      double[] classValues;
78      double[] thresholds;
79      var targetClassValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes);
80      AccuracyMaximizationThresholdCalculator.CalculateThresholds(ProblemData, EstimatedTrainingValues, targetClassValues, out classValues, out thresholds);
81
82      Model.SetThresholdsAndClassValues(thresholds, classValues);
83    }
84
85    public void SetClassDistibutionCutPointThresholds() {
86      double[] classValues;
87      double[] thresholds;
88      var targetClassValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes);
89      NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(ProblemData, EstimatedTrainingValues, targetClassValues, out classValues, out thresholds);
90
91      Model.SetThresholdsAndClassValues(thresholds, classValues);
92    }
93
94    protected override void OnModelChanged(EventArgs e) {
[5777]95      base.OnModelChanged(e);
[5736]96      SetAccuracyMaximizingThresholds();
97    }
98
99    protected override void OnProblemDataChanged(EventArgs e) {
100      base.OnProblemDataChanged(e);
101      SetAccuracyMaximizingThresholds();
102    }
103    protected virtual void OnModelThresholdsChanged(EventArgs e) {
104      RecalculateResults();
105    }
106
[5649]107    public IEnumerable<double> EstimatedValues {
108      get { return GetEstimatedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); }
109    }
110
111    public IEnumerable<double> EstimatedTrainingValues {
112      get { return GetEstimatedValues(ProblemData.TrainingIndizes); }
113    }
114
115    public IEnumerable<double> EstimatedTestValues {
116      get { return GetEstimatedValues(ProblemData.TestIndizes); }
117    }
118
119    public IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
120      return Model.GetEstimatedValues(ProblemData.Dataset, rows);
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.