Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Classification/CSV/ClassifiactionCSVInstanceProvider.cs @ 11316

Last change on this file since 11316 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

File size: 7.0 KB
RevLine 
[7860]1#region License Information
2/* HeuristicLab
[11170]3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7860]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;
[8598]23using System.Collections;
[7860]24using System.Collections.Generic;
[8180]25using System.IO;
[8192]26using System.Linq;
[8566]27using HeuristicLab.Common;
[7860]28using HeuristicLab.Problems.DataAnalysis;
[8192]29
[7860]30namespace HeuristicLab.Problems.Instances.DataAnalysis {
31  public class ClassificationCSVInstanceProvider : ClassificationInstanceProvider {
32    public override string Name {
[8211]33      get { return "CSV File"; }
[7860]34    }
35    public override string Description {
36      get {
37        return "";
38      }
39    }
40    public override Uri WebLink {
41      get { return new Uri("http://dev.heuristiclab.com/trac/hl/core/wiki/UsersFAQ#DataAnalysisImportFileFormat"); }
42    }
43    public override string ReferencePublication {
44      get { return ""; }
45    }
46
[8192]47    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
48      return new List<IDataDescriptor>();
49    }
50
51    public override IClassificationProblemData LoadData(IDataDescriptor descriptor) {
52      throw new NotImplementedException();
53    }
54
55    public override bool CanImportData {
[8180]56      get { return true; }
57    }
[8192]58    public override IClassificationProblemData ImportData(string path) {
59      TableFileParser csvFileParser = new TableFileParser();
[8180]60
[9651]61      csvFileParser.Parse(path, csvFileParser.AreColumnNamesInFirstLine(path));
[8192]62
63      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
[8566]64      string targetVar = dataset.DoubleVariables.Last();
[8192]65
[8566]66      // turn of input variables that are constant in the training partition
67      var allowedInputVars = new List<string>();
68      var trainingIndizes = Enumerable.Range(0, (csvFileParser.Rows * 2) / 3);
[8601]69      if (trainingIndizes.Count() >= 2) {
70        foreach (var variableName in dataset.DoubleVariables) {
71          if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
72            variableName != targetVar)
73            allowedInputVars.Add(variableName);
74        }
75      } else {
[8877]76        allowedInputVars.AddRange(dataset.DoubleVariables.Where(x => !x.Equals(targetVar)));
[8192]77      }
78
[8566]79      ClassificationProblemData classificationData = new ClassificationProblemData(dataset, allowedInputVars, targetVar);
80
81      int trainingPartEnd = trainingIndizes.Last();
82      classificationData.TrainingPartition.Start = trainingIndizes.First();
83      classificationData.TrainingPartition.End = trainingPartEnd;
84      classificationData.TestPartition.Start = trainingPartEnd;
85      classificationData.TestPartition.End = csvFileParser.Rows;
86
87      classificationData.Name = Path.GetFileName(path);
88
89      return classificationData;
[7860]90    }
91
[8877]92    protected override IClassificationProblemData ImportData(string path, ClassificationImportType type, TableFileParser csvFileParser) {
[9021]93      int trainingPartEnd = (csvFileParser.Rows * type.TrainingPercentage) / 100;
[8598]94      List<IList> values = csvFileParser.Values;
95      if (type.Shuffle) {
[8885]96        values = Shuffle(values);
97        if (type.UniformlyDistributeClasses) {
98          values = Shuffle(values, csvFileParser.VariableNames.ToList().FindIndex(x => x.Equals(type.TargetVariable)),
[9021]99                           type.TrainingPercentage, out trainingPartEnd);
[8885]100        }
[8598]101      }
102
103      Dataset dataset = new Dataset(csvFileParser.VariableNames, values);
104
105      // turn of input variables that are constant in the training partition
106      var allowedInputVars = new List<string>();
[8599]107      var trainingIndizes = Enumerable.Range(0, trainingPartEnd);
[8877]108      if (trainingIndizes.Count() >= 2) {
109        foreach (var variableName in dataset.DoubleVariables) {
110          if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 &&
111            variableName != type.TargetVariable)
112            allowedInputVars.Add(variableName);
113        }
114      } else {
115        allowedInputVars.AddRange(dataset.DoubleVariables.Where(x => !x.Equals(type.TargetVariable)));
[8598]116      }
117
[8877]118      ClassificationProblemData classificationData = new ClassificationProblemData(dataset, allowedInputVars, type.TargetVariable);
[8598]119
[8599]120      classificationData.TrainingPartition.Start = 0;
[8598]121      classificationData.TrainingPartition.End = trainingPartEnd;
122      classificationData.TestPartition.Start = trainingPartEnd;
123      classificationData.TestPartition.End = csvFileParser.Rows;
124
125      classificationData.Name = Path.GetFileName(path);
126
127      return classificationData;
128    }
129
[8877]130    protected List<IList> Shuffle(List<IList> values, int target, int trainingPercentage, out int trainingPartEnd) {
131      IList targetValues = values[target];
132      var group = targetValues.Cast<double>().GroupBy(x => x).Select(g => new { Key = g.Key, Count = g.Count() }).ToList();
133      Dictionary<double, double> taken = new Dictionary<double, double>();
134      foreach (var classCount in group) {
135        taken[classCount.Key] = (classCount.Count * trainingPercentage) / 100.0;
[8180]136      }
137
[8877]138      List<IList> training = GetListOfIListCopy(values);
139      List<IList> test = GetListOfIListCopy(values);
[8180]140
[8877]141      for (int i = 0; i < targetValues.Count; i++) {
142        if (taken[(double)targetValues[i]] > 0) {
143          AddRow(training, values, i);
144          taken[(double)targetValues[i]]--;
145        } else {
146          AddRow(test, values, i);
[8180]147        }
148      }
149
[8877]150      trainingPartEnd = training.First().Count;
151
152      for (int i = 0; i < training.Count; i++) {
153        for (int j = 0; j < test[i].Count; j++) {
154          training[i].Add(test[i][j]);
155        }
[8180]156      }
[8877]157
158      return training;
[8180]159    }
[8877]160
161    private void AddRow(List<IList> destination, List<IList> source, int index) {
162      for (int i = 0; i < source.Count; i++) {
163        destination[i].Add(source[i][index]);
164      }
165    }
166
167    private List<IList> GetListOfIListCopy(List<IList> values) {
168      List<IList> newList = new List<IList>(values.Count);
169      foreach (IList t in values) {
170        if (t is List<double>)
171          newList.Add(new List<double>());
172        else if (t is List<DateTime>)
173          newList.Add(new List<DateTime>());
174        else if (t is List<string>)
175          newList.Add(new List<string>());
176        else
177          throw new InvalidOperationException();
178      }
179      return newList;
180    }
[7860]181  }
182}
Note: See TracBrowser for help on using the repository browser.