Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2906 First concept of simple transformation (single target transformation)

File size: 5.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("Transformed Regression Model", "A model that was transformed back to match the original variables after the training was performed on transformed variables.")]
31  [StorableClass]
32  public class TransformedRegressionModel : RegressionModel, ITransformedRegressionModel {
33
34    [Storable]
35    public IRegressionModel OriginalModel { get; private set; }
36
37    [Storable]
38    public ItemList<IDataAnalysisTransformation> Transformations { get; private set; }
39
40    public override IEnumerable<string> VariablesUsedForPrediction {
41      get { return OriginalModel.VariablesUsedForPrediction; }
42    }
43
44    #region Constructor, Cloning & Persistence
45    public TransformedRegressionModel(IRegressionModel originalModel, IEnumerable<IDataAnalysisTransformation> transformations)
46      : base(GetOriginalTragetVariable(originalModel.TargetVariable, transformations)) {
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    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
67      var transformedDataset = TransformInputs(dataset, Transformations);
68
69      var estimates = OriginalModel.GetEstimatedValues(transformedDataset, rows);
70
71      return TransformEstimates(estimates, Transformations, OriginalModel.TargetVariable);
72    }
73
74    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
75      return new TransformedRegressionSolution(this, new RegressionProblemData(problemData) { TargetVariable = GetOriginalTragetVariable(TargetVariable, Transformations) });
76    }
77
78    private static IDataset TransformInputs(IDataset dataset, IEnumerable<IDataAnalysisTransformation> transformations) {
79      return dataset;
80
81      var modifiableDataset = ((Dataset)dataset).ToModifiable();
82
83      foreach (var transformation in transformations.Reverse()) {
84        var trans = (ITransformation<double>)transformation.Transformation;
85
86        // no transformations on target (but what if target is an input?)
87
88        var originalData = modifiableDataset.GetDoubleValues(transformation.OriginalVariable);
89        if (!trans.Check(originalData, out string errorMessage))
90          throw new InvalidOperationException($"Cannot estimate Values, Transformation is invalid: {errorMessage}");
91        var transformedData = trans.Apply(originalData).ToList();
92
93        if (modifiableDataset.VariableNames.Contains(transformation.TransformedVariable))
94          modifiableDataset.ReplaceVariable(transformation.TransformedVariable, transformedData);
95        else
96          modifiableDataset.AddVariable(transformation.TransformedVariable, transformedData);
97      }
98
99      return modifiableDataset;
100    }
101
102    private static IEnumerable<double> TransformEstimates(IEnumerable<double> data, IEnumerable<IDataAnalysisTransformation> transformations, string targetVariable) {
103      var targetTransformations = transformations.Where(x => x.TransformedVariable == targetVariable);
104
105      var transformedData = data.ToList();
106
107      foreach (var transformation in targetTransformations.Reverse()) {
108        var trans = (ITransformation<double>)transformation.Transformation;
109
110        if (!trans.Check(transformedData, out string errorMessage))
111          throw new InvalidOperationException($"Cannot estimate Values, Transformation is invalid: {errorMessage}");
112
113        transformedData = trans.InverseApply(transformedData).ToList();
114      }
115
116      return transformedData;
117    }
118
119    public static string GetOriginalTragetVariable(string transformedTarget, IEnumerable<IDataAnalysisTransformation> transformations) {
120      var target = transformedTarget;
121      foreach (var transformation in transformations.Reverse()) {
122        if (transformation.TransformedVariable == target)
123          target = transformation.OriginalVariable;
124      }
125      return target;
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.