Free cookie consent management tool by TermsFeed Policy Generator

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

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

Removed variable AllowedFeatures in all modeling algorithms. #709

File size: 1.8 KB
Line 
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 {
11    public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, int start, int end) {
12      int rowCount = end - start;
13      List<int> skippedFeatures = new List<int>();
14      for (int i = 0; i < dataset.Columns; i++) {
15        if (i != targetVariable) {
16          if (dataset.GetRange(i, start, end) == 0)
17            skippedFeatures.Add(i);
18        }
19      }
20
21      int maxColumns = dataset.Columns - skippedFeatures.Count();
22
23      double[] targetVector = new double[rowCount];
24      for (int i = 0; i < rowCount; i++) {
25        double value = dataset.GetValue(start + i, targetVariable);
26          targetVector[i] = value;
27      }
28      targetVector = targetVector.Where(x=> !double.IsNaN(x)).ToArray();
29
30      SVM.Node[][] nodes = new SVM.Node[targetVector.Length][];
31      List<SVM.Node> tempRow;
32      int addedRows = 0;
33      for (int row = 0; row < rowCount; row++) {
34        tempRow = new List<SVM.Node>();
35        for (int col = 0; col < dataset.Columns; col++) {
36          if (!skippedFeatures.Contains(col) && col!=targetVariable) {
37            double value = dataset.GetValue(start + row, col);
38            if (!double.IsNaN(value))
39              tempRow.Add(new SVM.Node(col, value));
40          }
41        }
42        if (!double.IsNaN(dataset.GetValue(start + row, targetVariable))) {
43          nodes[addedRows] = tempRow.ToArray();
44          addedRows++;
45        }
46      }
47
48      return new SVM.Problem(targetVector.Length, targetVector, nodes, maxColumns);
49    }
50  }
51}
Note: See TracBrowser for help on using the repository browser.