1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.DataAnalysis;
|
---|
8 |
|
---|
9 | namespace 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 | }
|
---|