Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2906 Updated project references + small refactoring

File size: 3.8 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("Regression Transformation Model", "A regression model that was transformed back to match the original variables after the training was performed on transformed variables.")]
30  [StorableClass]
31  public class RegressionTransformationModel : DataAnalysisTransformationModel, IRegressionTransformationModel {
32
33    public new IRegressionModel OriginalModel {
34      get { return (IRegressionModel)base.OriginalModel; }
35    }
36
37    IEnumerable<IDataAnalysisTransformation> IRegressionTransformationModel.TargetTransformations {
38      get { return TargetTransformations; }
39    }
40
41    #region Constructor, Cloning & Persistence
42    public RegressionTransformationModel(IRegressionModel originalModel, IEnumerable<IDataAnalysisTransformation> transformations)
43      : base(originalModel, transformations) {
44      var learnedTarget = DataAnalysisTransformation.GetStrictTransitiveVariables(originalModel.TargetVariable, transformations, true).Last();
45      var transitiveTargets = DataAnalysisTransformation.GetStrictTransitiveVariables(learnedTarget, transformations, inverse: false).ToList();
46      TargetTransformations = new ItemList<IDataAnalysisTransformation>(transformations.Where(t => transitiveTargets.Contains(t.TransformedVariable))).AsReadOnly();
47
48      TargetVariable = DataAnalysisTransformation.GetStrictTransitiveVariables(originalModel.TargetVariable, TargetTransformations, true).Last();
49    }
50
51    protected RegressionTransformationModel(RegressionTransformationModel original, Cloner cloner)
52      : base(original, cloner) {
53    }
54
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new RegressionTransformationModel(this, cloner);
57    }
58
59    [StorableConstructor]
60    protected RegressionTransformationModel(bool deserializing)
61      : base(deserializing) { }
62    #endregion
63
64    public virtual IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
65      var transformedInput = DataAnalysisTransformation.Transform(dataset, InputTransformations);
66      var estimates = OriginalModel.GetEstimatedValues(transformedInput, rows);
67      return InverseTransform(estimates, TargetTransformations);
68    }
69
70    public virtual IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
71      return new RegressionSolution(this, new RegressionProblemData(problemData));
72    }
73
74    protected static IEnumerable<double> InverseTransform(IEnumerable<double> data, IEnumerable<IDataAnalysisTransformation> transformations) {
75      foreach (var transformation in transformations.Reverse()) { // TargetTransformations only contains only relevant transformations
76        var trans = (ITransformation<double>)transformation.Transformation;
77        data = trans.InverseApply(data).ToList();
78      }
79      return data;
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.