Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/SupportVectorMachine/SupportVectorMachineUtil.cs @ 3842

Last change on this file since 3842 was 3842, checked in by gkronber, 14 years ago

Added operators for support vector regression. #1009

File size: 2.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Common;
29
30namespace HeuristicLab.Problems.DataAnalysis.SupportVectorMachine {
31  public class SupportVectorMachineUtil {
32    /// <summary>
33    /// Transforms <paramref name="problemData"/> into a data structure as needed by libSVM.
34    /// </summary>
35    /// <param name="problemData">The problem data to transform</param>
36    /// <param name="start">The index of the first row of <paramref name="problemData"/> to copy to the output.</param>
37    /// <param name="end">The last of the first row of <paramref name="problemData"/> to copy to the output.</param>
38    /// <returns>A problem data type that can be used to train a support vector machine.</returns>
39    public static SVM.Problem CreateSvmProblem(DataAnalysisProblemData problemData, int start, int end) {
40      int rowCount = end - start;
41      var targetVector = problemData.Dataset.GetVariableValues(problemData.TargetVariable.Value, start, end);
42
43      SVM.Node[][] nodes = new SVM.Node[targetVector.Length][];
44      List<SVM.Node> tempRow;
45      int maxNodeIndex = 0;
46      for (int row = 0; row < rowCount; row++) {
47        tempRow = new List<SVM.Node>();
48        foreach (var inputVariable in problemData.InputVariables) {
49          int col = problemData.Dataset.GetVariableIndex(inputVariable.Value);
50          double value = problemData.Dataset[start + row, col];
51          if (!double.IsNaN(value)) {
52            int nodeIndex = col + 1; // make sure the smallest nodeIndex = 1
53            tempRow.Add(new SVM.Node(nodeIndex, value));
54            if (nodeIndex > maxNodeIndex) maxNodeIndex = nodeIndex;
55          }
56        }
57        nodes[row] = tempRow.OrderBy(x => x.Index).ToArray(); // make sure the values are sorted by node index
58      }
59
60      return new SVM.Problem(targetVector.Length, targetVector, nodes, maxNodeIndex);
61    }
62  }
63}
Note: See TracBrowser for help on using the repository browser.