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