Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Classification/3.4/ClassificationInstanceProvider.cs @ 7682

Last change on this file since 7682 was 7682, checked in by sforsten, 12 years ago

#1784:

  • added Problem.Instances.Classification project
  • added classification problem instances
  • added a class Transformer to Problem.Instances
File size: 3.6 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;
28
29namespace HeuristicLab.Problems.Instances.Regression {
30  public abstract class ClassificationInstanceProvider : IProblemInstanceProvider<ClassificationData> {
31    public ClassificationData LoadData(string path) {
32      TableFileParser csvFileParser = new TableFileParser();
33      csvFileParser.Parse(path);
34
35      ClassificationData claData = new ClassificationData();
36      int pos = path.LastIndexOf('\\');
37      if (pos < 0)
38        claData.Name = path;
39      else {
40        pos++;
41        claData.Name = path.Substring(pos, path.Length - pos);
42      }
43      claData.InputVariables = new List<string>(csvFileParser.VariableNames).ToArray();
44      claData.TargetVariable = csvFileParser.VariableNames.Last();
45      claData.AllowedInputVariables = claData.InputVariables.Where(x => !x.Equals(claData.TargetVariable)).ToArray();
46      //convert to multidimensional array
47      List<IList> values = csvFileParser.Values;
48      claData.Values = new double[values.First().Count, values.Count];
49      for (int i = 0; i < values.Count; i++) {
50        for (int j = 0; j < values.First().Count; j++) {
51          claData.Values[j, i] = (double)values[i][j];
52        }
53      }
54
55      int trainingPartEnd = csvFileParser.Rows * 2 / 3;
56      claData.TrainingPartitionStart = 0;
57      claData.TrainingPartitionEnd = trainingPartEnd;
58      claData.TestPartitionStart = trainingPartEnd;
59      claData.TestPartitionEnd = csvFileParser.Rows;
60      return claData;
61    }
62
63    public void SaveData(ClassificationData instance, string path) {
64      StringBuilder strBuilder = new StringBuilder();
65
66      foreach (var variable in instance.InputVariables) {
67        strBuilder.Append(variable + ";");
68      }
69      strBuilder.Remove(strBuilder.Length - 1, 1);
70      strBuilder.AppendLine();
71
72      double[,] values = instance.Values;
73
74      for (int i = 0; i < values.GetLength(0); i++) {
75        for (int j = 0; j < values.GetLength(1); j++) {
76          strBuilder.Append(values[i, j] + ";");
77        }
78        strBuilder.Remove(strBuilder.Length - 1, 1);
79        strBuilder.AppendLine();
80      }
81
82      using (StreamWriter writer = new StreamWriter(path)) {
83        writer.Write(strBuilder);
84      }
85    }
86
87    public abstract IEnumerable<IDataDescriptor> GetDataDescriptors();
88    public abstract ClassificationData LoadData(IDataDescriptor descriptor);
89
90    public abstract string Name { get; }
91    public abstract string Description { get; }
92    public abstract Uri WebLink { get; }
93    public abstract string ReferencePublication { get; }
94
95    public IProblemInstanceConsumer<ClassificationData> Consumer { get; set; }
96  }
97}
Note: See TracBrowser for help on using the repository browser.