Free cookie consent management tool by TermsFeed Policy Generator

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, 12 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
RevLine 
[7860]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;
[8530]24using System.Globalization;
[8180]25using System.IO;
[8192]26using System.Linq;
[8180]27using System.Text;
[8566]28using HeuristicLab.Common;
[7860]29using HeuristicLab.Problems.DataAnalysis;
[8192]30
[7860]31namespace HeuristicLab.Problems.Instances.DataAnalysis {
32  public class RegressionCSVInstanceProvider : RegressionInstanceProvider {
33    public override string Name {
[8211]34      get { return "CSV File"; }
[7860]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
[8192]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 {
[8180]56      get { return true; }
57    }
[8192]58    public override IRegressionProblemData ImportData(string path) {
59      TableFileParser csvFileParser = new TableFileParser();
60      csvFileParser.Parse(path);
[8180]61
[8192]62      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
[8566]63      string targetVar = dataset.DoubleVariables.Last();
[8192]64
[8566]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      }
[8192]73
[8566]74      IRegressionProblemData regressionData = new RegressionProblemData(dataset, allowedInputVars, targetVar);
[8192]75
[8566]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;
[8192]81
[8566]82      regressionData.Name = Path.GetFileName(path);
83
84      return regressionData;
[7860]85    }
86
[8192]87    public override bool CanExportData {
88      get { return true; }
89    }
90    public override void ExportData(IRegressionProblemData instance, string path) {
[8530]91      var strBuilder = new StringBuilder();
[8180]92
93      foreach (var variable in instance.InputVariables) {
[8530]94        strBuilder.Append(variable + CultureInfo.CurrentCulture.TextInfo.ListSeparator);
[8180]95      }
[8530]96      strBuilder.Remove(strBuilder.Length - CultureInfo.CurrentCulture.TextInfo.ListSeparator.Length, CultureInfo.CurrentCulture.TextInfo.ListSeparator.Length);
[8180]97      strBuilder.AppendLine();
98
[8530]99      var dataset = instance.Dataset;
[8180]100
101      for (int i = 0; i < dataset.Rows; i++) {
102        for (int j = 0; j < dataset.Columns; j++) {
[8530]103          if (j > 0) strBuilder.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator);
104          strBuilder.Append(dataset.GetValue(i, j));
[8180]105        }
106        strBuilder.AppendLine();
107      }
108
[8530]109      using (var writer = new StreamWriter(path)) {
[8180]110        writer.Write(strBuilder);
111      }
112    }
[7860]113  }
114}
Note: See TracBrowser for help on using the repository browser.