Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/03/12 16:46:35 (12 years ago)
Author:
gkronber
Message:

#1847: merged r8084:8205 from trunk into GP move operators branch

Location:
branches/GP-MoveOperators
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/GP-MoveOperators

  • branches/GP-MoveOperators/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Classification/CSV/ClassifiactionCSVInstanceProvider.cs

    r7860 r8206  
    2020#endregion
    2121
    22 
    2322using System;
    2423using System.Collections.Generic;
     24using System.IO;
     25using System.Linq;
     26using System.Text;
    2527using HeuristicLab.Problems.DataAnalysis;
     28
    2629namespace HeuristicLab.Problems.Instances.DataAnalysis {
    2730  public class ClassificationCSVInstanceProvider : ClassificationInstanceProvider {
    2831    public override string Name {
    29       get { return "CSV Problem Provider"; }
     32      get { return "Comma-separated Values File"; }
    3033    }
    3134    public override string Description {
     
    4851      throw new NotImplementedException();
    4952    }
     53
     54    public override bool CanImportData {
     55      get { return true; }
     56    }
     57    public override IClassificationProblemData ImportData(string path) {
     58      TableFileParser csvFileParser = new TableFileParser();
     59
     60      csvFileParser.Parse(path);
     61
     62      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
     63      string targetVar = csvFileParser.VariableNames.Where(x => dataset.DoubleVariables.Contains(x)).Last();
     64      IEnumerable<string> allowedInputVars = dataset.DoubleVariables.Where(x => !x.Equals(targetVar));
     65
     66      ClassificationProblemData claData = new ClassificationProblemData(dataset, allowedInputVars, targetVar);
     67
     68      int trainingPartEnd = csvFileParser.Rows * 2 / 3;
     69      claData.TrainingPartition.Start = 0;
     70      claData.TrainingPartition.End = trainingPartEnd;
     71      claData.TestPartition.Start = trainingPartEnd;
     72      claData.TestPartition.End = csvFileParser.Rows;
     73      int pos = path.LastIndexOf('\\');
     74      if (pos < 0)
     75        claData.Name = path;
     76      else {
     77        pos++;
     78        claData.Name = path.Substring(pos, path.Length - pos);
     79      }
     80
     81      return claData;
     82    }
     83
     84    public override bool CanExportData {
     85      get { return true; }
     86    }
     87    public override void ExportData(IClassificationProblemData instance, string path) {
     88      StringBuilder strBuilder = new StringBuilder();
     89
     90      foreach (var variable in instance.InputVariables) {
     91        strBuilder.Append(variable + ";");
     92      }
     93      strBuilder.Remove(strBuilder.Length - 1, 1);
     94      strBuilder.AppendLine();
     95
     96      Dataset dataset = instance.Dataset;
     97
     98      for (int i = 0; i < dataset.Rows; i++) {
     99        for (int j = 0; j < dataset.Columns; j++) {
     100          strBuilder.Append(dataset.GetValue(i, j) + ";");
     101        }
     102        strBuilder.Remove(strBuilder.Length - 1, 1);
     103        strBuilder.AppendLine();
     104      }
     105
     106      using (StreamWriter writer = new StreamWriter(path)) {
     107        writer.Write(strBuilder);
     108      }
     109    }
    50110  }
    51111}
Note: See TracChangeset for help on using the changeset viewer.