Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassification.cs @ 9519

Last change on this file since 9519 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

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