Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Exporter-715/sources/HeuristicLab.SupportVectorMachines/3.2/SVMHelper.cs @ 2227

Last change on this file since 2227 was 2148, checked in by mkommend, 15 years ago

ignored dataset rows where the target variable is nan during !SVM learning (ticket #693)

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, 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}
Note: See TracBrowser for help on using the repository browser.