[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 {
|
---|
[2290] | 11 |
|
---|
[2165] | 12 | public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, int start, int end) {
|
---|
[2290] | 13 | return CreateSVMProblem(dataset, targetVariable, Enumerable.Range(0, dataset.Columns).ToDictionary<int, int>(x => x), start, end);
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, Dictionary<int, int> columnMapping, int start, int end) {
|
---|
[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 |
|
---|
[2165] | 26 | int maxColumns = dataset.Columns - skippedFeatures.Count();
|
---|
| 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;
|
---|
[1808] | 38 | for (int row = 0; row < rowCount; row++) {
|
---|
| 39 | tempRow = new List<SVM.Node>();
|
---|
[2165] | 40 | for (int col = 0; col < dataset.Columns; col++) {
|
---|
[2290] | 41 | if (!skippedFeatures.Contains(col) && col != targetVariable && columnMapping.ContainsKey(col)) {
|
---|
[2165] | 42 | double value = dataset.GetValue(start + row, col);
|
---|
[2148] | 43 | if (!double.IsNaN(value))
|
---|
[2290] | 44 | tempRow.Add(new SVM.Node(columnMapping[col], value));
|
---|
[2148] | 45 | }
|
---|
[1808] | 46 | }
|
---|
[2165] | 47 | if (!double.IsNaN(dataset.GetValue(start + row, targetVariable))) {
|
---|
[2148] | 48 | nodes[addedRows] = tempRow.ToArray();
|
---|
| 49 | addedRows++;
|
---|
| 50 | }
|
---|
[1808] | 51 | }
|
---|
| 52 |
|
---|
[2165] | 53 | return new SVM.Problem(targetVector.Length, targetVector, nodes, maxColumns);
|
---|
[1808] | 54 | }
|
---|
| 55 | }
|
---|
| 56 | }
|
---|