Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SVMHelper.cs @ 2349

Last change on this file since 2349 was 2349, checked in by gkronber, 15 years ago

Created a subclass for time-series prognosis with SVM and created a simple version of TheilInequalityCoefficientEvaluator in HL.Modeling. (#705)

File size: 2.5 KB
RevLine 
[1808]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.DataAnalysis;
8
9namespace HeuristicLab.SupportVectorMachines {
10  public class SVMHelper {
[2290]11
[2347]12    public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, int start, int end, int minTimeOffset, int maxTimeOffset) {
13      return CreateSVMProblem(dataset, targetVariable, Enumerable.Range(0, dataset.Columns).ToDictionary<int, int>(x => x), start, end, minTimeOffset, maxTimeOffset);
[2290]14    }
15
[2347]16    public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, Dictionary<int, int> columnMapping, int start, int end, int minTimeOffset, int maxTimeOffset) {
[1808]17      int rowCount = end - start;
[2148]18      List<int> skippedFeatures = new List<int>();
[2165]19      for (int i = 0; i < dataset.Columns; i++) {
20        if (i != targetVariable) {
21          if (dataset.GetRange(i, start, end) == 0)
22            skippedFeatures.Add(i);
23        }
[2148]24      }
25
[2349]26      int maxColumns = (dataset.Columns - skippedFeatures.Count()) * (maxTimeOffset - minTimeOffset + 1);
[2165]27
[2148]28      double[] targetVector = new double[rowCount];
29      for (int i = 0; i < rowCount; i++) {
[2165]30        double value = dataset.GetValue(start + i, targetVariable);
[2290]31        targetVector[i] = value;
[2148]32      }
[2290]33      targetVector = targetVector.Where(x => !double.IsNaN(x)).ToArray();
[2148]34
35      SVM.Node[][] nodes = new SVM.Node[targetVector.Length][];
[1808]36      List<SVM.Node> tempRow;
[2148]37      int addedRows = 0;
[2347]38      int timeOffsetBase = columnMapping.Count;
[1808]39      for (int row = 0; row < rowCount; row++) {
40        tempRow = new List<SVM.Node>();
[2165]41        for (int col = 0; col < dataset.Columns; col++) {
[2290]42          if (!skippedFeatures.Contains(col) && col != targetVariable && columnMapping.ContainsKey(col)) {
[2347]43            for (int timeOffset = minTimeOffset; timeOffset <= maxTimeOffset; timeOffset++) {
[2349]44              int actualColumn = columnMapping[col] * (maxTimeOffset - minTimeOffset + 1) + (timeOffset - minTimeOffset);
[2347]45              double value = dataset.GetValue(start + row + timeOffset, col);
46              if (!double.IsNaN(value))
47                tempRow.Add(new SVM.Node(actualColumn, value));
48            }
[2148]49          }
[1808]50        }
[2165]51        if (!double.IsNaN(dataset.GetValue(start + row, targetVariable))) {
[2148]52          nodes[addedRows] = tempRow.ToArray();
53          addedRows++;
54        }
[1808]55      }
56
[2165]57      return new SVM.Problem(targetVector.Length, targetVector, nodes, maxColumns);
[1808]58    }
59  }
60}
Note: See TracBrowser for help on using the repository browser.