Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/DiscriminantFunctionClassificationModel.cs @ 14711

Last change on this file since 14711 was 14711, checked in by gkronber, 7 years ago

#2520

  • renamed StorableClass -> StorableType
  • changed persistence to use GUIDs instead of type names
File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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;
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 discriminant function classification data analysis models.
32  /// </summary>
33  [StorableType("4BF35837-8978-4620-A5F4-2F11473C5F75")]
34  [Item("DiscriminantFunctionClassificationModel", "Represents a classification model that uses a discriminant function and classification thresholds.")]
35  public class DiscriminantFunctionClassificationModel : NamedItem, IDiscriminantFunctionClassificationModel {
36    [Storable]
37    private IRegressionModel model;
38    public IRegressionModel Model {
39      get { return model; }
40      private set { model = value; }
41    }
42
43    [Storable]
44    private double[] classValues;
45    public IEnumerable<double> ClassValues {
46      get { return (double[])classValues.Clone(); }
47      private set { classValues = value.ToArray(); }
48    }
49
50    [Storable]
51    private double[] thresholds;
52    public IEnumerable<double> Thresholds {
53      get { return (IEnumerable<double>)thresholds.Clone(); }
54      private set { thresholds = value.ToArray(); }
55    }
56
57    private IDiscriminantFunctionThresholdCalculator thresholdCalculator;
58    [Storable]
59    public IDiscriminantFunctionThresholdCalculator ThresholdCalculator {
60      get { return thresholdCalculator; }
61      private set { thresholdCalculator = value; }
62    }
63
64
65    [StorableConstructor]
66    protected DiscriminantFunctionClassificationModel(bool deserializing) : base(deserializing) { }
67    protected DiscriminantFunctionClassificationModel(DiscriminantFunctionClassificationModel original, Cloner cloner)
68      : base(original, cloner) {
69      model = cloner.Clone(original.model);
70      classValues = (double[])original.classValues.Clone();
71      thresholds = (double[])original.thresholds.Clone();
72    }
73
74    public DiscriminantFunctionClassificationModel(IRegressionModel model, IDiscriminantFunctionThresholdCalculator thresholdCalculator)
75      : base() {
76      this.name = ItemName;
77      this.description = ItemDescription;
78      this.model = model;
79      this.classValues = new double[0];
80      this.thresholds = new double[0];
81      this.thresholdCalculator = thresholdCalculator;
82    }
83
84    [StorableHook(HookType.AfterDeserialization)]
85    private void AfterDeserialization() {
86      if (ThresholdCalculator == null) ThresholdCalculator = new AccuracyMaximizationThresholdCalculator();
87    }
88
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new DiscriminantFunctionClassificationModel(this, cloner);
91    }
92
93    public void SetThresholdsAndClassValues(IEnumerable<double> thresholds, IEnumerable<double> classValues) {
94      var classValuesArr = classValues.ToArray();
95      var thresholdsArr = thresholds.ToArray();
96      if (thresholdsArr.Length != classValuesArr.Length) throw new ArgumentException();
97
98      this.classValues = classValuesArr;
99      this.thresholds = thresholdsArr;
100      OnThresholdsChanged(EventArgs.Empty);
101    }
102
103    public virtual void RecalculateModelParameters(IClassificationProblemData problemData, IEnumerable<int> rows) {
104      double[] classValues;
105      double[] thresholds;
106      var targetClassValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
107      var estimatedTrainingValues = GetEstimatedValues(problemData.Dataset, rows);
108      thresholdCalculator.Calculate(problemData, estimatedTrainingValues, targetClassValues, out classValues, out thresholds);
109      SetThresholdsAndClassValues(thresholds, classValues);
110    }
111
112
113    public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
114      return model.GetEstimatedValues(dataset, rows);
115    }
116
117    public IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
118      if (!Thresholds.Any() && !ClassValues.Any()) throw new ArgumentException("No thresholds and class values were set for the current classification model.");
119      foreach (var x in GetEstimatedValues(dataset, rows)) {
120        int classIndex = 0;
121        // find first threshold value which is larger than x => class index = threshold index + 1
122        for (int i = 0; i < thresholds.Length; i++) {
123          if (x > thresholds[i]) classIndex++;
124          else break;
125        }
126        yield return classValues.ElementAt(classIndex - 1);
127      }
128    }
129    #region events
130    public event EventHandler ThresholdsChanged;
131    protected virtual void OnThresholdsChanged(EventArgs e) {
132      var listener = ThresholdsChanged;
133      if (listener != null) listener(this, e);
134    }
135    #endregion
136
137    public virtual IDiscriminantFunctionClassificationSolution CreateDiscriminantFunctionClassificationSolution(IClassificationProblemData problemData) {
138      return new DiscriminantFunctionClassificationSolution(this, new ClassificationProblemData(problemData));
139    }
140
141    public virtual IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
142      return CreateDiscriminantFunctionClassificationSolution(problemData);
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.