Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataPreprocessing/3.4/ProblemDataCreator.cs @ 11378

Last change on this file since 11378 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 4.4 KB
RevLine 
[10310]1#region License Information
2/* HeuristicLab
[11171]3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10310]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
[10383]22using System;
[10536]23using System.Collections.Generic;
[10982]24using HeuristicLab.Common;
[10310]25using HeuristicLab.Problems.DataAnalysis;
26
27namespace HeuristicLab.DataPreprocessing {
[10908]28  public class ProblemDataCreator {
[10310]29
30    private readonly IPreprocessingContext context;
31
[10695]32    private Dataset ExportedDataset {
33      get { return exporteDataset ?? (exporteDataset = context.Data.ExportToDataset()); }
34    }
35    private Dataset exporteDataset;
36
[10922]37    private IList<ITransformation> Transformations { get { return context.Data.Transformations; } }
[10695]38
[10383]39    public ProblemDataCreator(IPreprocessingContext context) {
[10310]40      this.context = context;
41    }
42
[10383]43    public IDataAnalysisProblemData CreateProblemData() {
[11098]44      if (context.Data.Rows == 0 || context.Data.Columns == 0) return null;
45
[10990]46      var oldProblemData = context.ProblemData;
47      IDataAnalysisProblemData problemData;
[10310]48
[10536]49      if (oldProblemData is RegressionProblemData) {
[10695]50        problemData = CreateRegressionData((RegressionProblemData)oldProblemData);
[10536]51      } else if (oldProblemData is ClassificationProblemData) {
[10695]52        problemData = CreateClassificationData((ClassificationProblemData)oldProblemData);
[10536]53      } else if (oldProblemData is ClusteringProblemData) {
[10695]54        problemData = CreateClusteringData((ClusteringProblemData)oldProblemData);
[10536]55      } else {
56        throw new NotImplementedException("The type of the DataAnalysisProblemData is not supported.");
[10383]57      }
58
[10536]59      SetTrainingAndTestPartition(problemData);
60
[10383]61      return problemData;
62    }
63
[10695]64    private IDataAnalysisProblemData CreateRegressionData(RegressionProblemData oldProblemData) {
[10536]65      var targetVariable = oldProblemData.TargetVariable;
66      // target variable must be double and must exist in the new dataset
[10982]67      return new RegressionProblemData(ExportedDataset, GetDoubleInputVariables(targetVariable), targetVariable, Transformations);
[10536]68    }
[10310]69
[10695]70    private IDataAnalysisProblemData CreateClassificationData(ClassificationProblemData oldProblemData) {
[10536]71      var targetVariable = oldProblemData.TargetVariable;
72      // target variable must be double and must exist in the new dataset
[10982]73      return new ClassificationProblemData(ExportedDataset, GetDoubleInputVariables(targetVariable), targetVariable, Transformations);
[10536]74    }
[10383]75
[10695]76    private IDataAnalysisProblemData CreateClusteringData(ClusteringProblemData oldProblemData) {
[10982]77      return new ClusteringProblemData(ExportedDataset, GetDoubleInputVariables(String.Empty), Transformations);
[10383]78    }
79
80    private void SetTrainingAndTestPartition(IDataAnalysisProblemData problemData) {
81      var ppData = context.Data;
82
83      problemData.TrainingPartition.Start = ppData.TrainingPartition.Start;
84      problemData.TrainingPartition.End = ppData.TrainingPartition.End;
85      problemData.TestPartition.Start = ppData.TestPartition.Start;
86      problemData.TestPartition.End = ppData.TestPartition.End;
87    }
[10982]88
89    private IEnumerable<string> GetDoubleInputVariables(string targetVariable) {
90      var variableNames = new List<string>();
91      for (int i = 0; i < context.Data.Columns; ++i) {
92        var variableName = context.Data.GetVariableName(i);
[11156]93        if (context.Data.VariableHasType<double>(i)
[10982]94          && variableName != targetVariable
95          && IsNotConstantInputVariable(context.Data.GetValues<double>(i))) {
96
97          variableNames.Add(variableName);
98        }
99      }
100      return variableNames;
101    }
102
103    private bool IsNotConstantInputVariable(IList<double> list) {
104      return context.Data.TrainingPartition.End - context.Data.TrainingPartition.Start > 1 || list.Range() > 0;
105    }
[10310]106  }
107}
Note: See TracBrowser for help on using the repository browser.