Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicDiscriminantFunctionClassificationModel.cs @ 17912

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

#2875: Merged r17180 from trunk to stable

File size: 7.1 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
[6233]22using System;
[5649]23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[17097]28using HEAL.Attic;
[5649]29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
31  /// <summary>
32  /// Represents a symbolic classification model
33  /// </summary>
[17097]34  [StorableType("99332204-4097-496A-AB05-4DB9478DB159")]
[5649]35  [Item(Name = "SymbolicDiscriminantFunctionClassificationModel", Description = "Represents a symbolic classification model unsing a discriminant function.")]
[8594]36  public class SymbolicDiscriminantFunctionClassificationModel : SymbolicClassificationModel, ISymbolicDiscriminantFunctionClassificationModel {
[5649]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    }
[8594]50
51    private IDiscriminantFunctionThresholdCalculator thresholdCalculator;
[5720]52    [Storable]
[8594]53    public IDiscriminantFunctionThresholdCalculator ThresholdCalculator {
54      get { return thresholdCalculator; }
55      private set { thresholdCalculator = value; }
56    }
[5720]57
[8594]58
[5649]59    [StorableConstructor]
[17097]60    protected SymbolicDiscriminantFunctionClassificationModel(StorableConstructorFlag _) : base(_) { }
[5649]61    protected SymbolicDiscriminantFunctionClassificationModel(SymbolicDiscriminantFunctionClassificationModel original, Cloner cloner)
62      : base(original, cloner) {
63      classValues = (double[])original.classValues.Clone();
64      thresholds = (double[])original.thresholds.Clone();
[8594]65      thresholdCalculator = cloner.Clone(original.thresholdCalculator);
[5649]66    }
[14027]67    public SymbolicDiscriminantFunctionClassificationModel(string targetVariable, ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDiscriminantFunctionThresholdCalculator thresholdCalculator,
[5720]68      double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue)
[14027]69      : base(targetVariable, tree, interpreter, lowerEstimationLimit, upperEstimationLimit) {
[8531]70      this.thresholds = new double[0];
71      this.classValues = new double[0];
[8594]72      this.ThresholdCalculator = thresholdCalculator;
[5649]73    }
74
[8594]75    [StorableHook(HookType.AfterDeserialization)]
76    private void AfterDeserialization() {
[8883]77      // BackwardsCompatibility3.4
78      #region Backwards compatible code, remove with 3.5
[8594]79      if (ThresholdCalculator == null) ThresholdCalculator = new AccuracyMaximizationThresholdCalculator();
[8883]80      #endregion
[8594]81    }
82
[5649]83    public override IDeepCloneable Clone(Cloner cloner) {
84      return new SymbolicDiscriminantFunctionClassificationModel(this, cloner);
85    }
86
[5736]87    public void SetThresholdsAndClassValues(IEnumerable<double> thresholds, IEnumerable<double> classValues) {
88      var classValuesArr = classValues.ToArray();
89      var thresholdsArr = thresholds.ToArray();
[12702]90      if (thresholdsArr.Length != classValuesArr.Length || thresholdsArr.Length < 1)
[8921]91        throw new ArgumentException();
[12702]92      if (!double.IsNegativeInfinity(thresholds.First()))
[8921]93        throw new ArgumentException();
[5736]94
95      this.classValues = classValuesArr;
96      this.thresholds = thresholdsArr;
97      OnThresholdsChanged(EventArgs.Empty);
98    }
99
[8594]100    public override void RecalculateModelParameters(IClassificationProblemData problemData, IEnumerable<int> rows) {
101      double[] classValues;
102      double[] thresholds;
103      var targetClassValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
104      var estimatedTrainingValues = GetEstimatedValues(problemData.Dataset, rows);
105      thresholdCalculator.Calculate(problemData, estimatedTrainingValues, targetClassValues, out classValues, out thresholds);
106      SetThresholdsAndClassValues(thresholds, classValues);
107    }
108
[12702]109    public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
[8594]110      return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows).LimitToRange(LowerEstimationLimit, UpperEstimationLimit);
[5649]111    }
112
[12702]113    public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
[17177]114      var estimatedValues = GetEstimatedValues(dataset, rows);
115      return GetEstimatedClassValues(estimatedValues);
116    }
117    public IEnumerable<double> GetEstimatedClassValues(IEnumerable<double> estimatedValues) {
[8531]118      if (!Thresholds.Any() && !ClassValues.Any()) throw new ArgumentException("No thresholds and class values were set for the current symbolic classification model.");
[17177]119      foreach (var x in estimatedValues) {
[5649]120        int classIndex = 0;
[5678]121        // find first threshold value which is larger than x => class index = threshold index + 1
[5649]122        for (int i = 0; i < thresholds.Length; i++) {
123          if (x > thresholds[i]) classIndex++;
124          else break;
125        }
[5657]126        yield return classValues.ElementAt(classIndex - 1);
[5649]127      }
128    }
129
[8594]130    public override ISymbolicClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
131      return CreateDiscriminantClassificationSolution(problemData);
132    }
133    public SymbolicDiscriminantFunctionClassificationSolution CreateDiscriminantClassificationSolution(IClassificationProblemData problemData) {
[8528]134      return new SymbolicDiscriminantFunctionClassificationSolution(this, new ClassificationProblemData(problemData));
[6604]135    }
136    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
[8594]137      return CreateDiscriminantClassificationSolution(problemData);
[6604]138    }
139    IDiscriminantFunctionClassificationSolution IDiscriminantFunctionClassificationModel.CreateDiscriminantFunctionClassificationSolution(IClassificationProblemData problemData) {
[8594]140      return CreateDiscriminantClassificationSolution(problemData);
[6604]141    }
142
[5649]143    #region events
144    public event EventHandler ThresholdsChanged;
145    protected virtual void OnThresholdsChanged(EventArgs e) {
146      var listener = ThresholdsChanged;
147      if (listener != null) listener(this, e);
148    }
[5720]149    #endregion
[5649]150  }
151}
Note: See TracBrowser for help on using the repository browser.