Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegression.cs @ 10878

Last change on this file since 10878 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 9.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.DataAnalysis;
32using LibSVM;
33
34namespace HeuristicLab.Algorithms.DataAnalysis {
35  /// <summary>
36  /// Support vector machine regression data analysis algorithm.
37  /// </summary>
38  [Item("Support Vector Regression", "Support vector machine regression data analysis algorithm (wrapper for libSVM).")]
39  [Creatable("Data Analysis")]
40  [StorableClass]
41  public sealed class SupportVectorRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
42    private const string SvmTypeParameterName = "SvmType";
43    private const string KernelTypeParameterName = "KernelType";
44    private const string CostParameterName = "Cost";
45    private const string NuParameterName = "Nu";
46    private const string GammaParameterName = "Gamma";
47    private const string EpsilonParameterName = "Epsilon";
48    private const string DegreeParameterName = "Degree";
49
50    #region parameter properties
51    public IConstrainedValueParameter<StringValue> SvmTypeParameter {
52      get { return (IConstrainedValueParameter<StringValue>)Parameters[SvmTypeParameterName]; }
53    }
54    public IConstrainedValueParameter<StringValue> KernelTypeParameter {
55      get { return (IConstrainedValueParameter<StringValue>)Parameters[KernelTypeParameterName]; }
56    }
57    public IValueParameter<DoubleValue> NuParameter {
58      get { return (IValueParameter<DoubleValue>)Parameters[NuParameterName]; }
59    }
60    public IValueParameter<DoubleValue> CostParameter {
61      get { return (IValueParameter<DoubleValue>)Parameters[CostParameterName]; }
62    }
63    public IValueParameter<DoubleValue> GammaParameter {
64      get { return (IValueParameter<DoubleValue>)Parameters[GammaParameterName]; }
65    }
66    public IValueParameter<DoubleValue> EpsilonParameter {
67      get { return (IValueParameter<DoubleValue>)Parameters[EpsilonParameterName]; }
68    }
69    public IValueParameter<IntValue> DegreeParameter {
70      get { return (IValueParameter<IntValue>)Parameters[DegreeParameterName]; }
71    }
72    #endregion
73    #region properties
74    public StringValue SvmType {
75      get { return SvmTypeParameter.Value; }
76      set { SvmTypeParameter.Value = value; }
77    }
78    public StringValue KernelType {
79      get { return KernelTypeParameter.Value; }
80      set { KernelTypeParameter.Value = value; }
81    }
82    public DoubleValue Nu {
83      get { return NuParameter.Value; }
84    }
85    public DoubleValue Cost {
86      get { return CostParameter.Value; }
87    }
88    public DoubleValue Gamma {
89      get { return GammaParameter.Value; }
90    }
91    public DoubleValue Epsilon {
92      get { return EpsilonParameter.Value; }
93    }
94    public IntValue Degree {
95      get { return DegreeParameter.Value; }
96    }
97    #endregion
98    [StorableConstructor]
99    private SupportVectorRegression(bool deserializing) : base(deserializing) { }
100    private SupportVectorRegression(SupportVectorRegression original, Cloner cloner)
101      : base(original, cloner) {
102    }
103    public SupportVectorRegression()
104      : base() {
105      Problem = new RegressionProblem();
106
107      List<StringValue> svrTypes = (from type in new List<string> { "NU_SVR", "EPSILON_SVR" }
108                                    select new StringValue(type).AsReadOnly())
109                                   .ToList();
110      ItemSet<StringValue> svrTypeSet = new ItemSet<StringValue>(svrTypes);
111      List<StringValue> kernelTypes = (from type in new List<string> { "LINEAR", "POLY", "SIGMOID", "RBF" }
112                                       select new StringValue(type).AsReadOnly())
113                                   .ToList();
114      ItemSet<StringValue> kernelTypeSet = new ItemSet<StringValue>(kernelTypes);
115      Parameters.Add(new ConstrainedValueParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", svrTypeSet, svrTypes[0]));
116      Parameters.Add(new ConstrainedValueParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", kernelTypeSet, kernelTypes[3]));
117      Parameters.Add(new ValueParameter<DoubleValue>(NuParameterName, "The value of the nu parameter of the nu-SVR.", new DoubleValue(0.5)));
118      Parameters.Add(new ValueParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of epsilon-SVR and nu-SVR.", new DoubleValue(1.0)));
119      Parameters.Add(new ValueParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0)));
120      Parameters.Add(new ValueParameter<DoubleValue>(EpsilonParameterName, "The value of the epsilon parameter for epsilon-SVR.", new DoubleValue(0.1)));
121      Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName, "The degree parameter for the polynomial kernel function.", new IntValue(3)));
122    }
123    [StorableHook(HookType.AfterDeserialization)]
124    private void AfterDeserialization() {
125      #region backwards compatibility (change with 3.4)
126      if (!Parameters.ContainsKey(DegreeParameterName))
127        Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName, "The degree parameter for the polynomial kernel function.", new IntValue(3)));
128      #endregion
129    }
130
131    public override IDeepCloneable Clone(Cloner cloner) {
132      return new SupportVectorRegression(this, cloner);
133    }
134
135    #region support vector regression
136    protected override void Run() {
137      IRegressionProblemData problemData = Problem.ProblemData;
138      IEnumerable<string> selectedInputVariables = problemData.AllowedInputVariables;
139      double trainR2, testR2;
140      int nSv;
141      var solution = CreateSupportVectorRegressionSolution(problemData, selectedInputVariables, SvmType.Value,
142        KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, Epsilon.Value, Degree.Value,
143        out trainR2, out testR2, out nSv);
144
145      Results.Add(new Result("Support vector regression solution", "The support vector regression solution.", solution));
146      Results.Add(new Result("Training R²", "The Pearson's R² of the SVR solution on the training partition.", new DoubleValue(trainR2)));
147      Results.Add(new Result("Test R²", "The Pearson's R² of the SVR solution on the test partition.", new DoubleValue(testR2)));
148      Results.Add(new Result("Number of support vectors", "The number of support vectors of the SVR solution.", new IntValue(nSv)));
149    }
150
151    public static SupportVectorRegressionSolution CreateSupportVectorRegressionSolution(IRegressionProblemData problemData, IEnumerable<string> allowedInputVariables,
152      string svmType, string kernelType, double cost, double nu, double gamma, double epsilon, int degree,
153      out double trainingR2, out double testR2, out int nSv) {
154      Dataset dataset = problemData.Dataset;
155      string targetVariable = problemData.TargetVariable;
156      IEnumerable<int> rows = problemData.TrainingIndices;
157
158      //extract SVM parameters from scope and set them
159      svm_parameter parameter = new svm_parameter();
160      parameter.svm_type = GetSvmType(svmType);
161      parameter.kernel_type = GetKernelType(kernelType);
162      parameter.C = cost;
163      parameter.nu = nu;
164      parameter.gamma = gamma;
165      parameter.p = epsilon;
166      parameter.cache_size = 500;
167      parameter.probability = 0;
168      parameter.eps = 0.001;
169      parameter.degree = degree;
170      parameter.shrinking = 1;
171      parameter.coef0 = 0;
172
173
174
175      svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
176      RangeTransform rangeTransform = RangeTransform.Compute(problem);
177      svm_problem scaledProblem = rangeTransform.Scale(problem);
178      var svmModel = svm.svm_train(scaledProblem, parameter);
179      nSv = svmModel.SV.Length;
180      var model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables);
181      var solution = new SupportVectorRegressionSolution(model, (IRegressionProblemData)problemData.Clone());
182      trainingR2 = solution.TrainingRSquared;
183      testR2 = solution.TestRSquared;
184      return solution;
185    }
186
187    private static int GetSvmType(string svmType) {
188      if (svmType == "NU_SVR") return svm_parameter.NU_SVR;
189      if (svmType == "EPSILON_SVR") return svm_parameter.EPSILON_SVR;
190      throw new ArgumentException("Unknown SVM type");
191    }
192
193    private static int GetKernelType(string kernelType) {
194      if (kernelType == "LINEAR") return svm_parameter.LINEAR;
195      if (kernelType == "POLY") return svm_parameter.POLY;
196      if (kernelType == "SIGMOID") return svm_parameter.SIGMOID;
197      if (kernelType == "RBF") return svm_parameter.RBF;
198      throw new ArgumentException("Unknown kernel type");
199    }
200    #endregion
201  }
202}
Note: See TracBrowser for help on using the repository browser.