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