Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/19/18 17:46:57 (7 years ago)
Author:
pfleck
Message:

#2906 Implemented chained transformations on target and input variables.

Location:
branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation
Files:
3 added
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs

    r15846 r15847  
    241241      }
    242242    }
     243
     244    public virtual IDataAnalysisProblemData InverseTransform() {
     245      var newDataset = InverseTransform(Dataset, Transformations);
     246
     247      // TODO: Cannot create concrete instance here (maybe derived Create-method?)
     248      var cloner = new Cloner();
     249      cloner.RegisterClonedObject(Dataset, newDataset);
     250      cloner.RegisterClonedObject(TransformationsParameter.Value, new ItemList<IDataAnalysisTransformation>().AsReadOnly());
     251
     252      return cloner.Clone(this);
     253    }
     254
     255    public static IDataset Transform(IDataset dataset, IEnumerable<IDataAnalysisTransformation> transformations) {
     256      var modifiableDataset = ((Dataset)dataset).ToModifiable();
     257
     258      foreach (var transformation in transformations) {
     259        var trans = (ITransformation<double>)transformation.Transformation;
     260
     261        var originalData = modifiableDataset.GetDoubleValues(transformation.OriginalVariable);
     262        if (!trans.Check(originalData, out string errorMessage))
     263          throw new InvalidOperationException($"Cannot estimate Values, Transformation is invalid: {errorMessage}");
     264
     265        var transformedData = trans.Apply(originalData).ToList();
     266        if (modifiableDataset.VariableNames.Contains(transformation.TransformedVariable))
     267          modifiableDataset.ReplaceVariable(transformation.TransformedVariable, transformedData);
     268        else
     269          modifiableDataset.AddVariable(transformation.TransformedVariable, transformedData);
     270      }
     271
     272      return modifiableDataset;
     273    }
     274
     275    public static IDataset InverseTransform(IDataset dataset, IEnumerable<IDataAnalysisTransformation> transformations) {
     276      var modifiableDataset = ((Dataset)dataset).ToModifiable();
     277
     278      foreach (var transformation in transformations.Reverse()) {
     279        var trans = (ITransformation<double>)transformation.Transformation;
     280
     281        var transformedData = modifiableDataset.GetDoubleValues(transformation.TransformedVariable);
     282
     283        var originalData = trans.InverseApply(transformedData).ToList();
     284        modifiableDataset.ReplaceVariable(transformation.OriginalVariable, originalData);
     285      }
     286
     287      return modifiableDataset;
     288    }
    243289  }
    244290}
  • branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblemData.cs

    r15846 r15847  
    180180      base.AdjustProblemDataProperties(problemData);
    181181    }
     182
     183    public override IDataAnalysisProblemData InverseTransform() {
     184      var transformedProblemData = (IRegressionProblemData)base.InverseTransform();
     185      var targetVariable = GetOriginalTragetVariable(TargetVariable, Transformations);
     186      transformedProblemData.TargetVariable = targetVariable;
     187      return transformedProblemData;
     188    }
     189
     190    public static string GetOriginalTragetVariable(string transformedTarget, IEnumerable<IDataAnalysisTransformation> transformations) {
     191      var originalTarget = transformedTarget;
     192      foreach (var transformation in transformations.Reverse()) {
     193        if (transformation.TransformedVariable == originalTarget)
     194          originalTarget = transformation.OriginalVariable;
     195      }
     196      return originalTarget;
     197    }
    182198  }
    183199}
  • branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/TransformedRegressionModel.cs

    r15846 r15847  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using System.Linq;
     
    4443    #region Constructor, Cloning & Persistence
    4544    public TransformedRegressionModel(IRegressionModel originalModel, IEnumerable<IDataAnalysisTransformation> transformations)
    46       : base(GetOriginalTragetVariable(originalModel.TargetVariable, transformations)) {
     45      : base(RegressionProblemData.GetOriginalTragetVariable(originalModel.TargetVariable, transformations)) {
     46      Name = "Transformed " + originalModel.Name;
    4747      OriginalModel = originalModel;
    4848      Transformations = new ItemList<IDataAnalysisTransformation>(transformations);
     
    6464    #endregion
    6565
     66    // dataset in original data range
    6667    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
    6768      var transformedDataset = TransformInputs(dataset, Transformations);
     
    6970      var estimates = OriginalModel.GetEstimatedValues(transformedDataset, rows);
    7071
    71       return TransformEstimates(estimates, Transformations, OriginalModel.TargetVariable);
     72      return InverseTransformEstimates(estimates, Transformations, OriginalModel.TargetVariable);
    7273    }
    7374
     75    // problemData in original data range
    7476    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
    75       return new TransformedRegressionSolution(this, new RegressionProblemData(problemData) { TargetVariable = GetOriginalTragetVariable(TargetVariable, Transformations) });
     77      return new TransformedRegressionSolution(this, new RegressionProblemData(problemData));
    7678    }
    7779
    7880    private static IDataset TransformInputs(IDataset dataset, IEnumerable<IDataAnalysisTransformation> transformations) {
    79       return dataset;
     81      return DataAnalysisProblemData.Transform(dataset, transformations);
     82    }
    8083
    81       var modifiableDataset = ((Dataset)dataset).ToModifiable();
     84    private static IEnumerable<double> InverseTransformEstimates(IEnumerable<double> data, IEnumerable<IDataAnalysisTransformation> transformations, string targetVariable) {
     85      var estimates = data.ToList();
    8286
    8387      foreach (var transformation in transformations.Reverse()) {
    84         var trans = (ITransformation<double>)transformation.Transformation;
     88        if (transformation.TransformedVariable == targetVariable) {
     89          var trans = (ITransformation<double>)transformation.Transformation;
    8590
    86         // no transformations on target (but what if target is an input?)
     91          estimates = trans.InverseApply(estimates).ToList();
    8792
    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);
     93          // setup next iteration
     94          targetVariable = transformation.OriginalVariable;
     95        }
    9796      }
    9897
    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;
     98      return estimates;
    12699    }
    127100  }
Note: See TracChangeset for help on using the changeset viewer.