source: trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/CSV/RegressionCSVInstanceProvider.cs @ 8566

Last change on this file since 8566 was 8566, checked in by gkronber, 7 years ago

#1927 implemented check to deactivate input variables that are constant in the training partition in the CSV problem instance providers for regression, classification and clustering.

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Globalization;
25using System.IO;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Problems.DataAnalysis;
30
31namespace HeuristicLab.Problems.Instances.DataAnalysis {
32  public class RegressionCSVInstanceProvider : RegressionInstanceProvider {
33    public override string Name {
34      get { return "CSV File"; }
35    }
36    public override string Description {
37      get {
38        return "";
39      }
40    }
41    public override Uri WebLink {
42      get { return new Uri("http://dev.heuristiclab.com/trac/hl/core/wiki/UsersFAQ#DataAnalysisImportFileFormat"); }
43    }
44    public override string ReferencePublication {
45      get { return ""; }
46    }
47
48    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
49      return new List<IDataDescriptor>();
50    }
51    public override IRegressionProblemData LoadData(IDataDescriptor descriptor) {
52      throw new NotImplementedException();
53    }
54
55    public override bool CanImportData {
56      get { return true; }
57    }
58    public override IRegressionProblemData ImportData(string path) {
59      TableFileParser csvFileParser = new TableFileParser();
60      csvFileParser.Parse(path);
61
62      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
63      string targetVar = dataset.DoubleVariables.Last();
64
65      // turn of input variables that are constant in the training partition
66      var allowedInputVars = new List<string>();
67      var trainingIndizes = Enumerable.Range(0, (csvFileParser.Rows * 2) / 3);
68      foreach (var variableName in dataset.DoubleVariables) {
69        if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
70          variableName != targetVar)
71          allowedInputVars.Add(variableName);
72      }
73
74      IRegressionProblemData regressionData = new RegressionProblemData(dataset, allowedInputVars, targetVar);
75
76      var trainingPartEnd = trainingIndizes.Last();
77      regressionData.TrainingPartition.Start = trainingIndizes.First();
78      regressionData.TrainingPartition.End = trainingPartEnd;
79      regressionData.TestPartition.Start = trainingPartEnd;
80      regressionData.TestPartition.End = csvFileParser.Rows;
81
82      regressionData.Name = Path.GetFileName(path);
83
84      return regressionData;
85    }
86
87    public override bool CanExportData {
88      get { return true; }
89    }
90    public override void ExportData(IRegressionProblemData instance, string path) {
91      var strBuilder = new StringBuilder();
92
93      foreach (var variable in instance.InputVariables) {
94        strBuilder.Append(variable + CultureInfo.CurrentCulture.TextInfo.ListSeparator);
95      }
96      strBuilder.Remove(strBuilder.Length - CultureInfo.CurrentCulture.TextInfo.ListSeparator.Length, CultureInfo.CurrentCulture.TextInfo.ListSeparator.Length);
97      strBuilder.AppendLine();
98
99      var dataset = instance.Dataset;
100
101      for (int i = 0; i < dataset.Rows; i++) {
102        for (int j = 0; j < dataset.Columns; j++) {
103          if (j > 0) strBuilder.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
104          strBuilder.Append(dataset.GetValue(i, j));
105        }
106        strBuilder.AppendLine();
107      }
108
109      using (var writer = new StreamWriter(path)) {
110        writer.Write(strBuilder);
111      }
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.