Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionTransformationModel.cs @ 15870

Last change on this file since 15870 was 15870, checked in by pfleck, 6 years ago

#2906

  • Implemented for classification, clustering, etc.
  • Simplified Transformation interfaces (read-only, ...).
  • Started moving transformation logic out of ProblemData.
File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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  [Item("Regression Transformation Model", "A regression model that was transformed back to match the original variables after the training was performed on transformed variables.")]
31  [StorableClass]
32  public class RegressionTransformationModel : DataAnalysisTransformationModel, IRegressionTransformationModel {
33
34    public new IRegressionModel OriginalModel {
35      get { return (IRegressionModel)base.OriginalModel; }
36    }
37
38    #region Constructor, Cloning & Persistence
39    public RegressionTransformationModel(IRegressionModel originalModel, IEnumerable<IDataAnalysisTransformation> transformations)
40      : base(originalModel, transformations) {
41      var transitiveTargets = CalculateTransitiveVariables(new[] { originalModel.TargetVariable }, transformations);
42      TargetTransformations = new ItemList<IDataAnalysisTransformation>(transformations.Where(t => transitiveTargets.Contains(t.OriginalVariable))).AsReadOnly();
43      TargetVariable = GetOriginalTragetVariable(originalModel.TargetVariable, TargetTransformations);
44    }
45
46    protected RegressionTransformationModel(RegressionTransformationModel original, Cloner cloner)
47      : base(original, cloner) {
48     
49    }
50
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new RegressionTransformationModel(this, cloner);
53    }
54
55    [StorableConstructor]
56    protected RegressionTransformationModel(bool deserializing)
57      : base(deserializing) { }
58    #endregion
59
60    public virtual IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
61      var estimates = OriginalModel.GetEstimatedValues(Transform(dataset, InputTransformations), rows);
62      return InverseTransform(estimates, TargetTransformations, OriginalModel.TargetVariable);
63    }
64
65    public virtual IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
66      return new RegressionSolution(this, new RegressionProblemData(problemData));
67    }
68
69    public static IEnumerable<double> InverseTransform(IEnumerable<double> data, IEnumerable<IDataAnalysisTransformation> transformations, string targetVariable) {
70      var estimates = data.ToList();
71
72      foreach (var transformation in transformations.Reverse()) {
73        if (transformation.TransformedVariable == targetVariable) {
74          var trans = (ITransformation<double>)transformation.Transformation;
75
76          estimates = trans.InverseApply(estimates).ToList();
77
78          // setup next iteration
79          targetVariable = transformation.OriginalVariable;
80        }
81      }
82
83      return estimates;
84    }
85
86    public static string GetTransformedTragetVariable(string originalTarget, IEnumerable<IDataAnalysisTransformation> transformations) {
87      var transformedTarget = originalTarget;
88      foreach (var transformation in transformations) {
89        if (transformation.OriginalVariable == transformedTarget)
90          transformedTarget = transformation.TransformedVariable;
91      }
92      return transformedTarget;
93    }
94
95    public static string GetOriginalTragetVariable(string transformedTarget, IEnumerable<IDataAnalysisTransformation> transformations) {
96      var originalTarget = transformedTarget;
97      foreach (var transformation in transformations.Reverse()) {
98        if (transformation.TransformedVariable == originalTarget)
99          originalTarget = transformation.OriginalVariable;
100      }
101      return originalTarget;
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.