Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/DiscriminantFunctionClassificationSolution.cs @ 5736

Last change on this file since 5736 was 5736, checked in by gkronber, 13 years ago

#1418 implemented linear scaling for classification solutions, fixed bugs interactive simplifier view for classification solutions.

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