Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegression.cs @ 7460

Last change on this file since 7460 was 7460, checked in by gkronber, 12 years ago

#1081: merged r7266:7459 from the trunk into the time series prognosis branch.

File size: 8.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
32
33namespace HeuristicLab.Algorithms.DataAnalysis {
34  /// <summary>
35  /// Support vector machine regression data analysis algorithm.
36  /// </summary>
37  [Item("Support Vector Regression", "Support vector machine regression data analysis algorithm (wrapper for libSVM).")]
38  [Creatable("Data Analysis")]
39  [StorableClass]
40  public sealed class SupportVectorRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
41    private const string SvmTypeParameterName = "SvmType";
42    private const string KernelTypeParameterName = "KernelType";
43    private const string CostParameterName = "Cost";
44    private const string NuParameterName = "Nu";
45    private const string GammaParameterName = "Gamma";
46    private const string EpsilonParameterName = "Epsilon";
47
48    #region parameter properties
49    public IValueParameter<StringValue> SvmTypeParameter {
50      get { return (IValueParameter<StringValue>)Parameters[SvmTypeParameterName]; }
51    }
52    public IValueParameter<StringValue> KernelTypeParameter {
53      get { return (IValueParameter<StringValue>)Parameters[KernelTypeParameterName]; }
54    }
55    public IValueParameter<DoubleValue> NuParameter {
56      get { return (IValueParameter<DoubleValue>)Parameters[NuParameterName]; }
57    }
58    public IValueParameter<DoubleValue> CostParameter {
59      get { return (IValueParameter<DoubleValue>)Parameters[CostParameterName]; }
60    }
61    public IValueParameter<DoubleValue> GammaParameter {
62      get { return (IValueParameter<DoubleValue>)Parameters[GammaParameterName]; }
63    }
64    public IValueParameter<DoubleValue> EpsilonParameter {
65      get { return (IValueParameter<DoubleValue>)Parameters[EpsilonParameterName]; }
66    }
67    #endregion
68    #region properties
69    public StringValue SvmType {
70      get { return SvmTypeParameter.Value; }
71    }
72    public StringValue KernelType {
73      get { return KernelTypeParameter.Value; }
74    }
75    public DoubleValue Nu {
76      get { return NuParameter.Value; }
77    }
78    public DoubleValue Cost {
79      get { return CostParameter.Value; }
80    }
81    public DoubleValue Gamma {
82      get { return GammaParameter.Value; }
83    }
84    public DoubleValue Epsilon {
85      get { return EpsilonParameter.Value; }
86    }
87    #endregion
88    [StorableConstructor]
89    private SupportVectorRegression(bool deserializing) : base(deserializing) { }
90    private SupportVectorRegression(SupportVectorRegression original, Cloner cloner)
91      : base(original, cloner) {
92    }
93    public SupportVectorRegression()
94      : base() {
95      Problem = new RegressionProblem();
96
97      List<StringValue> svrTypes = (from type in new List<string> { "NU_SVR", "EPSILON_SVR" }
98                                    select new StringValue(type).AsReadOnly())
99                                   .ToList();
100      ItemSet<StringValue> svrTypeSet = new ItemSet<StringValue>(svrTypes);
101      List<StringValue> kernelTypes = (from type in new List<string> { "LINEAR", "POLY", "SIGMOID", "RBF" }
102                                       select new StringValue(type).AsReadOnly())
103                                   .ToList();
104      ItemSet<StringValue> kernelTypeSet = new ItemSet<StringValue>(kernelTypes);
105      Parameters.Add(new ConstrainedValueParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", svrTypeSet, svrTypes[0]));
106      Parameters.Add(new ConstrainedValueParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", kernelTypeSet, kernelTypes[3]));
107      Parameters.Add(new ValueParameter<DoubleValue>(NuParameterName, "The value of the nu parameter of the nu-SVR.", new DoubleValue(0.5)));
108      Parameters.Add(new ValueParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of epsilon-SVR and nu-SVR.", new DoubleValue(1.0)));
109      Parameters.Add(new ValueParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0)));
110      Parameters.Add(new ValueParameter<DoubleValue>(EpsilonParameterName, "The value of the epsilon parameter for epsilon-SVR.", new DoubleValue(0.1)));
111    }
112    [StorableHook(HookType.AfterDeserialization)]
113    private void AfterDeserialization() { }
114
115    public override IDeepCloneable Clone(Cloner cloner) {
116      return new SupportVectorRegression(this, cloner);
117    }
118
119    #region support vector regression
120    protected override void Run() {
121      IRegressionProblemData problemData = Problem.ProblemData;
122      IEnumerable<string> selectedInputVariables = problemData.AllowedInputVariables;
123      double trainR2, testR2;
124      int nSv;
125      var solution = CreateSupportVectorRegressionSolution(problemData, selectedInputVariables, SvmType.Value,
126        KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, Epsilon.Value,
127        out trainR2, out testR2, out nSv);
128
129      Results.Add(new Result("Support vector regression solution", "The support vector regression solution.", solution));
130      Results.Add(new Result("Training R²", "The Pearson's R² of the SVR solution on the training partition.", new DoubleValue(trainR2)));
131      Results.Add(new Result("Test R²", "The Pearson's R² of the SVR solution on the test partition.", new DoubleValue(testR2)));
132      Results.Add(new Result("Number of support vectors", "The number of support vectors of the SVR solution.", new IntValue(nSv)));
133    }
134
135    public static SupportVectorRegressionSolution CreateSupportVectorRegressionSolution(IRegressionProblemData problemData, IEnumerable<string> allowedInputVariables,
136      string svmType, string kernelType, double cost, double nu, double gamma, double epsilon,
137      out double trainingR2, out double testR2, out int nSv) {
138      Dataset dataset = problemData.Dataset;
139      string targetVariable = problemData.TargetVariable;
140      IEnumerable<int> rows = problemData.TrainingIndizes;
141
142      //extract SVM parameters from scope and set them
143      SVM.Parameter parameter = new SVM.Parameter();
144      parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
145      parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);
146      parameter.C = cost;
147      parameter.Nu = nu;
148      parameter.Gamma = gamma;
149      parameter.P = epsilon;
150      parameter.CacheSize = 500;
151      parameter.Probability = false;
152
153      SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
154      SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
155      SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem);
156      var svmModel = SVM.Training.Train(scaledProblem, parameter);
157      nSv = svmModel.SupportVectorCount;
158      var model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables);
159      var solution = new SupportVectorRegressionSolution(model, (IRegressionProblemData)problemData.Clone());
160      trainingR2 = solution.TrainingRSquared;
161      testR2 = solution.TestRSquared;
162      return solution;
163    }
164    #endregion
165  }
166}
Note: See TracBrowser for help on using the repository browser.