Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassification.cs @ 5690

Last change on this file since 5690 was 5690, checked in by gkronber, 13 years ago

#1418 removed SupportVectorIndizes field from libSVM models.

File size: 7.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
32using System.Collections.Generic;
33using HeuristicLab.Problems.DataAnalysis.Symbolic;
34
35namespace HeuristicLab.Algorithms.DataAnalysis {
36  /// <summary>
37  /// Support vector machine classification data analysis algorithm.
38  /// </summary>
39  [Item("Support Vector Classification", "Support vector machine classification data analysis algorithm.")]
40  [Creatable("Data Analysis")]
41  [StorableClass]
42  public sealed class SupportVectorClassification : FixedDataAnalysisAlgorithm<IClassificationProblem> {
43    private const string SvmTypeParameterName = "SvmType";
44    private const string KernelTypeParameterName = "KernelType";
45    private const string CostParameterName = "Cost";
46    private const string NuParameterName = "Nu";
47    private const string GammaParameterName = "Gamma";
48
49    #region parameter properties
50    public IValueParameter<StringValue> SvmTypeParameter {
51      get { return (IValueParameter<StringValue>)Parameters[SvmTypeParameterName]; }
52    }
53    public IValueParameter<StringValue> KernelTypeParameter {
54      get { return (IValueParameter<StringValue>)Parameters[KernelTypeParameterName]; }
55    }
56    public IValueParameter<DoubleValue> NuParameter {
57      get { return (IValueParameter<DoubleValue>)Parameters[NuParameterName]; }
58    }
59    public IValueParameter<DoubleValue> CostParameter {
60      get { return (IValueParameter<DoubleValue>)Parameters[CostParameterName]; }
61    }
62    public IValueParameter<DoubleValue> GammaParameter {
63      get { return (IValueParameter<DoubleValue>)Parameters[GammaParameterName]; }
64    }
65    #endregion
66    #region properties
67    public StringValue SvmType {
68      get { return SvmTypeParameter.Value; }
69    }
70    public StringValue KernelType {
71      get { return KernelTypeParameter.Value; }
72    }
73    public DoubleValue Nu {
74      get { return NuParameter.Value; }
75    }
76    public DoubleValue Cost {
77      get { return CostParameter.Value; }
78    }
79    public DoubleValue Gamma {
80      get { return GammaParameter.Value; }
81    }
82    #endregion
83    [StorableConstructor]
84    private SupportVectorClassification(bool deserializing) : base(deserializing) { }
85    private SupportVectorClassification(SupportVectorClassification original, Cloner cloner)
86      : base(original, cloner) {
87    }
88    public SupportVectorClassification()
89      : base() {
90      Problem = new ClassificationProblem();
91
92      List<StringValue> svrTypes = (from type in new List<string> { "NU_SVC", "EPSILON_SVC" }
93                                    select new StringValue(type).AsReadOnly())
94                                   .ToList();
95      ItemSet<StringValue> svrTypeSet = new ItemSet<StringValue>(svrTypes);
96      List<StringValue> kernelTypes = (from type in new List<string> { "LINEAR", "POLY", "SIGMOID", "RBF" }
97                                       select new StringValue(type).AsReadOnly())
98                                   .ToList();
99      ItemSet<StringValue> kernelTypeSet = new ItemSet<StringValue>(kernelTypes);
100      Parameters.Add(new ConstrainedValueParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", svrTypeSet, svrTypes[0]));
101      Parameters.Add(new ConstrainedValueParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", kernelTypeSet, kernelTypes[3]));
102      Parameters.Add(new ValueParameter<DoubleValue>(NuParameterName, "The value of the nu parameter nu-SVC.", new DoubleValue(0.5)));
103      Parameters.Add(new ValueParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of C-SVC.", new DoubleValue(1.0)));
104      Parameters.Add(new ValueParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0)));
105    }
106    [StorableHook(HookType.AfterDeserialization)]
107    private void AfterDeserialization() { }
108
109    public override IDeepCloneable Clone(Cloner cloner) {
110      return new SupportVectorClassification(this, cloner);
111    }
112
113    #region support vector classification
114    protected override void Run() {
115      IClassificationProblemData problemData = Problem.ProblemData;
116      IEnumerable<string> selectedInputVariables = problemData.AllowedInputVariables;
117      var solution = CreateSupportVectorClassificationSolution(problemData, selectedInputVariables, SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value);
118
119      Results.Add(new Result("Support vector classification solution", "The support vector classification solution.", solution));
120    }
121
122    public static SupportVectorClassificationSolution CreateSupportVectorClassificationSolution(IClassificationProblemData problemData, IEnumerable<string> allowedInputVariables,
123      string svmType, string kernelType, double cost, double nu, double gamma) {
124      Dataset dataset = problemData.Dataset;
125      string targetVariable = problemData.TargetVariable;
126      int start = problemData.TrainingPartitionStart.Value;
127      int end = problemData.TrainingPartitionEnd.Value;
128      IEnumerable<int> rows = Enumerable.Range(start, end - start);
129
130      //extract SVM parameters from scope and set them
131      SVM.Parameter parameter = new SVM.Parameter();
132      parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
133      parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);
134      parameter.C = cost;
135      parameter.Nu = nu;
136      parameter.Gamma = gamma;
137      parameter.CacheSize = 500;
138      parameter.Probability = false;
139
140
141      SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
142      SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
143      SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem);
144      var model = new SupportVectorMachineModel(SVM.Training.Train(scaledProblem, parameter), rangeTransform, targetVariable, allowedInputVariables, problemData.ClassValues);
145
146      return new SupportVectorClassificationSolution(model, problemData);
147    }
148    #endregion
149  }
150}
Note: See TracBrowser for help on using the repository browser.