source: trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/RegressionInstanceProvider.cs @ 7965

Last change on this file since 7965 was 7965, checked in by sforsten, 9 years ago

#1784: CSV files that contain columns with non-numeric data can be imported again.

File size: 3.3 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;
24using System.Collections.Generic;
25using System.IO;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Problems.Instances.DataAnalysis {
31  public abstract class RegressionInstanceProvider : IProblemInstanceProvider<IRegressionProblemData> {
32
33    public IRegressionProblemData LoadData(string path) {
34      TableFileParser csvFileParser = new TableFileParser();
35      csvFileParser.Parse(path);
36
37      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
38      string targetVar = csvFileParser.VariableNames.Where(x => dataset.DoubleVariables.Contains(x)).Last();
39
40      IEnumerable<string> allowedInputVars = dataset.DoubleVariables.Where(x => !x.Equals(targetVar));
41
42      IRegressionProblemData regData = new RegressionProblemData(dataset, allowedInputVars, targetVar);
43
44      int trainingPartEnd = csvFileParser.Rows * 2 / 3;
45      regData.TrainingPartition.Start = 0;
46      regData.TrainingPartition.End = trainingPartEnd;
47      regData.TestPartition.Start = trainingPartEnd;
48      regData.TestPartition.End = csvFileParser.Rows;
49
50      int pos = path.LastIndexOf('\\');
51      if (pos < 0)
52        regData.Name = path;
53      else {
54        pos++;
55        regData.Name = path.Substring(pos, path.Length - pos);
56      }
57      return regData;
58    }
59
60    public void SaveData(IRegressionProblemData instance, string path) {
61      StringBuilder strBuilder = new StringBuilder();
62
63      foreach (var variable in instance.InputVariables) {
64        strBuilder.Append(variable + ";");
65      }
66      strBuilder.Remove(strBuilder.Length - 1, 1);
67      strBuilder.AppendLine();
68
69      Dataset dataset = instance.Dataset;
70
71      for (int i = 0; i < dataset.Rows; i++) {
72        for (int j = 0; j < dataset.Columns; j++) {
73          strBuilder.Append(dataset.GetValue(i, j) + ";");
74        }
75        strBuilder.Remove(strBuilder.Length - 1, 1);
76        strBuilder.AppendLine();
77      }
78
79      using (StreamWriter writer = new StreamWriter(path)) {
80        writer.Write(strBuilder);
81      }
82    }
83
84    public abstract IEnumerable<IDataDescriptor> GetDataDescriptors();
85    public abstract IRegressionProblemData LoadData(IDataDescriptor descriptor);
86
87    public abstract string Name { get; }
88    public abstract string Description { get; }
89    public abstract Uri WebLink { get; }
90    public abstract string ReferencePublication { get; }
91  }
92}
Note: See TracBrowser for help on using the repository browser.