Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassification.cs @ 5914

Last change on this file since 5914 was 5914, checked in by mkommend, 12 years ago

#1418: Changed DataAnalysisSolutions and -Models and updated GenerateRowsToEvaluate method in SymbolicDataAnalysisEvaluator.

File size: 7.1 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.")]
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    [StorableHook(HookType.AfterDeserialization)]
105    private void AfterDeserialization() { }
106
107    public override IDeepCloneable Clone(Cloner cloner) {
108      return new SupportVectorClassification(this, cloner);
109    }
110
111    #region support vector classification
112    protected override void Run() {
113      IClassificationProblemData problemData = Problem.ProblemData;
114      IEnumerable<string> selectedInputVariables = problemData.AllowedInputVariables;
115      var solution = CreateSupportVectorClassificationSolution(problemData, selectedInputVariables, SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value);
116
117      Results.Add(new Result("Support vector classification solution", "The support vector classification solution.", solution));
118    }
119
120    public static SupportVectorClassificationSolution CreateSupportVectorClassificationSolution(IClassificationProblemData problemData, IEnumerable<string> allowedInputVariables,
121      string svmType, string kernelType, double cost, double nu, double gamma) {
122      Dataset dataset = problemData.Dataset;
123      string targetVariable = problemData.TargetVariable;
124      int start = problemData.TrainingPartition.Start;
125      int end = problemData.TrainingPartition.End;
126      IEnumerable<int> rows = Enumerable.Range(start, end - start);
127
128      //extract SVM parameters from scope and set them
129      SVM.Parameter parameter = new SVM.Parameter();
130      parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
131      parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);
132      parameter.C = cost;
133      parameter.Nu = nu;
134      parameter.Gamma = gamma;
135      parameter.CacheSize = 500;
136      parameter.Probability = false;
137
138
139      SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
140      SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
141      SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem);
142      var model = new SupportVectorMachineModel(SVM.Training.Train(scaledProblem, parameter), rangeTransform, targetVariable, allowedInputVariables, problemData.ClassValues);
143
144      return new SupportVectorClassificationSolution(model, (IClassificationProblemData)problemData.Clone());
145    }
146    #endregion
147  }
148}
Note: See TracBrowser for help on using the repository browser.