Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/15/09 17:33:39 (15 years ago)
Author:
gkronber
Message:

Implemented #707: ProblemInjector creates a new dataset that contains only the target variable and all allowed input variables.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Modeling/3.2/ProblemInjector.cs

    r2161 r2162  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.DataAnalysis;
     29using System.Linq;
    2930
    3031namespace HeuristicLab.Modeling {
     
    8384
    8485    public override IOperation Apply(IScope scope) {
    85       AddVariableToScope("Dataset", scope);
    86       AddVariableToScope("TargetVariable", scope);
    87       AddVariableToScope("AllowedFeatures", scope);
    8886      AddVariableToScope("TrainingSamplesStart", scope);
    8987      AddVariableToScope("TrainingSamplesEnd", scope);
     
    9290      AddVariableToScope("TestSamplesStart", scope);
    9391      AddVariableToScope("TestSamplesEnd", scope);
     92
     93      Dataset operatorDataset = (Dataset)GetVariable("Dataset").Value;
     94      int targetVariable = ((IntData)GetVariable("TargetVariable").Value).Data;
     95      ItemList<IntData> operatorAllowedFeatures = (ItemList<IntData>)GetVariable("AllowedFeatures").Value;
     96
     97      Dataset scopeDataset = CreateNewDataset(operatorDataset, targetVariable, operatorAllowedFeatures);
     98
     99      ItemList<IntData> allowedFeatures = new ItemList<IntData>();
     100      allowedFeatures.AddRange(Enumerable.Range(1, scopeDataset.Columns -1 ).Select(x=>new IntData(x)));
     101
     102      scope.AddVariable(new Variable("Dataset", scopeDataset));
     103      scope.AddVariable(new Variable("AllowedFeatures", allowedFeatures));
     104      scope.AddVariable(new Variable("TargetVariable", new IntData(0)));
    94105
    95106      int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
     
    110121    }
    111122
     123    private Dataset CreateNewDataset(Dataset operatorDataset, int targetVariable, ItemList<IntData> operatorAllowedFeatures) {
     124      int columns = (operatorAllowedFeatures.Count() + 1);
     125      double[] values = new double[operatorDataset.Rows * columns];
     126
     127      for (int i = 0; i < values.Length; i++) {
     128        int row = i / columns;
     129        int column = i % columns;
     130        if (column == 0) {
     131          values[i] = operatorDataset.GetValue(row, targetVariable);
     132        } else {
     133          values[i] = operatorDataset.GetValue(row, operatorAllowedFeatures[column-1].Data);
     134        }
     135      }
     136
     137      Dataset ds = new Dataset();
     138      ds.Columns = columns;
     139      ds.Rows = operatorDataset.Rows;
     140      ds.Name = operatorDataset.Name;
     141      ds.Samples = values;
     142      double[] scalingFactor = new double[columns];
     143      double[] scalingOffset = new double[columns];
     144      ds.SetVariableName(0, operatorDataset.GetVariableName(targetVariable));
     145      scalingFactor[0] = operatorDataset.ScalingFactor[targetVariable];
     146      scalingOffset[0] = operatorDataset.ScalingOffset[targetVariable];
     147      for (int column = 1; column < columns; column++) {
     148        ds.SetVariableName(column, operatorDataset.GetVariableName(operatorAllowedFeatures[column - 1].Data));
     149        scalingFactor[column] = operatorDataset.ScalingFactor[operatorAllowedFeatures[column - 1].Data];
     150        scalingOffset[column] = operatorDataset.ScalingOffset[operatorAllowedFeatures[column - 1].Data];
     151      }
     152      ds.ScalingOffset = scalingOffset;
     153      ds.ScalingFactor = scalingFactor;
     154      return ds;
     155    }
     156
    112157    private void AddVariableToScope(string variableName, IScope scope) {
    113158      scope.AddVariable(new Variable(variableName, (IItem)GetVariable(variableName).Value.Clone()));     
Note: See TracChangeset for help on using the changeset viewer.