Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/TransformedRegressionModel.cs @ 15847

Last change on this file since 15847 was 15847, checked in by pfleck, 7 years ago

#2906 Implemented chained transformations on target and input variables.

File size: 4.1 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Problems.DataAnalysis {
29  [Item("Transformed Regression Model", "A model that was transformed back to match the original variables after the training was performed on transformed variables.")]
30  [StorableClass]
31  public class TransformedRegressionModel : RegressionModel, ITransformedRegressionModel {
32
33    [Storable]
34    public IRegressionModel OriginalModel { get; private set; }
35
36    [Storable]
37    public ItemList<IDataAnalysisTransformation> Transformations { get; private set; }
38
39    public override IEnumerable<string> VariablesUsedForPrediction {
40      get { return OriginalModel.VariablesUsedForPrediction; }
41    }
42
43    #region Constructor, Cloning & Persistence
44    public TransformedRegressionModel(IRegressionModel originalModel, IEnumerable<IDataAnalysisTransformation> transformations)
45      : base(RegressionProblemData.GetOriginalTragetVariable(originalModel.TargetVariable, transformations)) {
46      Name = "Transformed " + originalModel.Name;
47      OriginalModel = originalModel;
48      Transformations = new ItemList<IDataAnalysisTransformation>(transformations);
49    }
50
51    protected TransformedRegressionModel(TransformedRegressionModel original, Cloner cloner)
52      : base(original, cloner) {
53      OriginalModel = cloner.Clone(original.OriginalModel);
54      Transformations = cloner.Clone(original.Transformations);
55    }
56
57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new TransformedRegressionModel(this, cloner);
59    }
60
61    [StorableConstructor]
62    protected TransformedRegressionModel(bool deserializing)
63      : base(deserializing) { }
64    #endregion
65
66    // dataset in original data range
67    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
68      var transformedDataset = TransformInputs(dataset, Transformations);
69
70      var estimates = OriginalModel.GetEstimatedValues(transformedDataset, rows);
71
72      return InverseTransformEstimates(estimates, Transformations, OriginalModel.TargetVariable);
73    }
74
75    // problemData in original data range
76    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
77      return new TransformedRegressionSolution(this, new RegressionProblemData(problemData));
78    }
79
80    private static IDataset TransformInputs(IDataset dataset, IEnumerable<IDataAnalysisTransformation> transformations) {
81      return DataAnalysisProblemData.Transform(dataset, transformations);
82    }
83
84    private static IEnumerable<double> InverseTransformEstimates(IEnumerable<double> data, IEnumerable<IDataAnalysisTransformation> transformations, string targetVariable) {
85      var estimates = data.ToList();
86
87      foreach (var transformation in transformations.Reverse()) {
88        if (transformation.TransformedVariable == targetVariable) {
89          var trans = (ITransformation<double>)transformation.Transformation;
90
91          estimates = trans.InverseApply(estimates).ToList();
92
93          // setup next iteration
94          targetVariable = transformation.OriginalVariable;
95        }
96      }
97
98      return estimates;
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.