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, ItemList<IntData> allowedFeatures, int targetVariable, int start, int end) {
|
---|
12 | int rowCount = end - start;
|
---|
13 | double[] samples = dataset.Samples;
|
---|
14 |
|
---|
15 | List<int> skippedFeatures = new List<int>();
|
---|
16 | for (int i = 0; i < allowedFeatures.Count; i++) {
|
---|
17 | if (dataset.GetRange(allowedFeatures[i].Data, start, end) == 0)
|
---|
18 | skippedFeatures.Add(i);
|
---|
19 | }
|
---|
20 |
|
---|
21 | double[] targetVector = new double[rowCount];
|
---|
22 | for (int i = 0; i < rowCount; i++) {
|
---|
23 | double value = samples[(start + i) * dataset.Columns + targetVariable];
|
---|
24 | targetVector[i] = value;
|
---|
25 | }
|
---|
26 | targetVector = targetVector.Where(x=> !double.IsNaN(x)).ToArray();
|
---|
27 |
|
---|
28 | SVM.Node[][] nodes = new SVM.Node[targetVector.Length][];
|
---|
29 | List<SVM.Node> tempRow;
|
---|
30 | int addedRows = 0;
|
---|
31 | for (int row = 0; row < rowCount; row++) {
|
---|
32 | tempRow = new List<SVM.Node>();
|
---|
33 | for (int col = 0; col < allowedFeatures.Count; col++) {
|
---|
34 | if (!skippedFeatures.Contains(col)) {
|
---|
35 | double value = samples[(start + row) * dataset.Columns + allowedFeatures[col].Data];
|
---|
36 | if (!double.IsNaN(value))
|
---|
37 | tempRow.Add(new SVM.Node(allowedFeatures[col].Data, value));
|
---|
38 | }
|
---|
39 | }
|
---|
40 | if (!double.IsNaN(samples[(start + row) * dataset.Columns + targetVariable])) {
|
---|
41 | nodes[addedRows] = tempRow.ToArray();
|
---|
42 | addedRows++;
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | return new SVM.Problem(targetVector.Length, targetVector, nodes, allowedFeatures.Max(x => x.Data));
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|