Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicDiscriminantFunctionClassificationModel.cs @ 8531

Last change on this file since 8531 was 8531, checked in by mkommend, 12 years ago

#1919: Refactored calculation of thresholds for SymbolicDiscriminantFunctionClassficationModels and removed the automatic recalculation of thresholds during solution creation.

File size: 10.1 KB
RevLine 
[5649]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 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
[6233]22using System;
[5649]23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
31  /// <summary>
32  /// Represents a symbolic classification model
33  /// </summary>
34  [StorableClass]
35  [Item(Name = "SymbolicDiscriminantFunctionClassificationModel", Description = "Represents a symbolic classification model unsing a discriminant function.")]
36  public class SymbolicDiscriminantFunctionClassificationModel : SymbolicDataAnalysisModel, ISymbolicDiscriminantFunctionClassificationModel {
37
38    [Storable]
39    private double[] thresholds;
40    public IEnumerable<double> Thresholds {
41      get { return (IEnumerable<double>)thresholds.Clone(); }
[5736]42      private set { thresholds = value.ToArray(); }
[5649]43    }
[5678]44    [Storable]
45    private double[] classValues;
46    public IEnumerable<double> ClassValues {
47      get { return (IEnumerable<double>)classValues.Clone(); }
[5736]48      private set { classValues = value.ToArray(); }
[5678]49    }
[5720]50    [Storable]
51    private double lowerEstimationLimit;
52    public double LowerEstimationLimit { get { return lowerEstimationLimit; } }
53    [Storable]
54    private double upperEstimationLimit;
55    public double UpperEstimationLimit { get { return upperEstimationLimit; } }
56
[5649]57    [StorableConstructor]
58    protected SymbolicDiscriminantFunctionClassificationModel(bool deserializing) : base(deserializing) { }
59    protected SymbolicDiscriminantFunctionClassificationModel(SymbolicDiscriminantFunctionClassificationModel original, Cloner cloner)
60      : base(original, cloner) {
61      classValues = (double[])original.classValues.Clone();
62      thresholds = (double[])original.thresholds.Clone();
[5720]63      lowerEstimationLimit = original.lowerEstimationLimit;
64      upperEstimationLimit = original.upperEstimationLimit;
[5649]65    }
[5720]66    public SymbolicDiscriminantFunctionClassificationModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
67      double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue)
[5649]68      : base(tree, interpreter) {
[8531]69      this.thresholds = new double[0];
70      this.classValues = new double[0];
[5720]71      this.lowerEstimationLimit = lowerEstimationLimit;
72      this.upperEstimationLimit = upperEstimationLimit;
[5649]73    }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new SymbolicDiscriminantFunctionClassificationModel(this, cloner);
77    }
78
[5736]79    public void SetThresholdsAndClassValues(IEnumerable<double> thresholds, IEnumerable<double> classValues) {
80      var classValuesArr = classValues.ToArray();
81      var thresholdsArr = thresholds.ToArray();
82      if (thresholdsArr.Length != classValuesArr.Length) throw new ArgumentException();
83
84      this.classValues = classValuesArr;
85      this.thresholds = thresholdsArr;
86      OnThresholdsChanged(EventArgs.Empty);
87    }
88
[5649]89    public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
[8531]90      return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows).LimitToRange(lowerEstimationLimit, upperEstimationLimit);
[5649]91    }
92
93    public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
[8531]94      if (!Thresholds.Any() && !ClassValues.Any()) throw new ArgumentException("No thresholds and class values were set for the current symbolic classification model.");
[5649]95      foreach (var x in GetEstimatedValues(dataset, rows)) {
96        int classIndex = 0;
[5678]97        // find first threshold value which is larger than x => class index = threshold index + 1
[5649]98        for (int i = 0; i < thresholds.Length; i++) {
99          if (x > thresholds[i]) classIndex++;
100          else break;
101        }
[5657]102        yield return classValues.ElementAt(classIndex - 1);
[5649]103      }
104    }
105
[6604]106    public SymbolicDiscriminantFunctionClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
[8528]107      return new SymbolicDiscriminantFunctionClassificationSolution(this, new ClassificationProblemData(problemData));
[6604]108    }
109    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
110      return CreateClassificationSolution(problemData);
111    }
112    IDiscriminantFunctionClassificationSolution IDiscriminantFunctionClassificationModel.CreateDiscriminantFunctionClassificationSolution(IClassificationProblemData problemData) {
113      return CreateClassificationSolution(problemData);
114    }
115
[5649]116    #region events
117    public event EventHandler ThresholdsChanged;
118    protected virtual void OnThresholdsChanged(EventArgs e) {
119      var listener = ThresholdsChanged;
120      if (listener != null) listener(this, e);
121    }
[5720]122    #endregion
[5818]123
[8531]124    public static void SetAccuracyMaximizingThresholds(IDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData) {
125      double[] classValues;
126      double[] thresholds;
127      var targetClassValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
128      var estimatedTrainingValues = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices);
129      AccuracyMaximizationThresholdCalculator.CalculateThresholds(problemData, estimatedTrainingValues, targetClassValues, out classValues, out thresholds);
130
131      model.SetThresholdsAndClassValues(thresholds, classValues);
132    }
133
134    public static void SetClassDistibutionCutPointThresholds(IDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData) {
135      double[] classValues;
136      double[] thresholds;
137      var targetClassValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
138      var estimatedTrainingValues = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices);
139      NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(problemData, estimatedTrainingValues, targetClassValues, out classValues, out thresholds);
140
141      model.SetThresholdsAndClassValues(thresholds, classValues);
142    }
143
[5818]144    public static void Scale(SymbolicDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData) {
145      var dataset = problemData.Dataset;
146      var targetVariable = problemData.TargetVariable;
[8139]147      var rows = problemData.TrainingIndices;
[5818]148      var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
[6740]149      var targetValues = dataset.GetDoubleValues(targetVariable, rows);
[5818]150      double alpha;
151      double beta;
[5942]152      OnlineCalculatorError errorState;
[8030]153      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta, out errorState);
[5942]154      if (errorState != OnlineCalculatorError.None) return;
[5818]155
156      ConstantTreeNode alphaTreeNode = null;
157      ConstantTreeNode betaTreeNode = null;
158      // check if model has been scaled previously by analyzing the structure of the tree
159      var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
160      if (startNode.GetSubtree(0).Symbol is Addition) {
161        var addNode = startNode.GetSubtree(0);
[6803]162        if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
[5818]163          alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
164          var mulNode = addNode.GetSubtree(0);
[6803]165          if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
[5818]166            betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
167          }
168        }
169      }
170      // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
171      if (alphaTreeNode != null && betaTreeNode != null) {
172        betaTreeNode.Value *= beta;
173        alphaTreeNode.Value *= beta;
174        alphaTreeNode.Value += alpha;
175      } else {
176        var mainBranch = startNode.GetSubtree(0);
177        startNode.RemoveSubtree(0);
[6234]178        var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
[5818]179        startNode.AddSubtree(scaledMainBranch);
180      }
181    }
182
183    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
184      if (alpha.IsAlmost(0.0)) {
185        return treeNode;
186      } else {
[6234]187        var addition = new Addition();
[6233]188        var node = addition.CreateTreeNode();
[5818]189        var alphaConst = MakeConstant(alpha);
190        node.AddSubtree(treeNode);
191        node.AddSubtree(alphaConst);
192        return node;
193      }
194    }
195
[6233]196    private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
[5818]197      if (beta.IsAlmost(1.0)) {
198        return treeNode;
199      } else {
[6234]200        var multipliciation = new Multiplication();
[6233]201        var node = multipliciation.CreateTreeNode();
[5818]202        var betaConst = MakeConstant(beta);
203        node.AddSubtree(treeNode);
204        node.AddSubtree(betaConst);
205        return node;
206      }
207    }
208
209    private static ISymbolicExpressionTreeNode MakeConstant(double c) {
210      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
211      node.Value = c;
212      return node;
213    }
[5649]214  }
215}
Note: See TracBrowser for help on using the repository browser.