Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1722: Renamed indizes to indices in the whole trunk solution.

File size: 8.3 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 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 IConstrainedValueParameter<StringValue> SvmTypeParameter {
49      get { return (IConstrainedValueParameter<StringValue>)Parameters[SvmTypeParameterName]; }
50    }
51    public IConstrainedValueParameter<StringValue> KernelTypeParameter {
52      get { return (IConstrainedValueParameter<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      set { SvmTypeParameter.Value = value; }
68    }
69    public StringValue KernelType {
70      get { return KernelTypeParameter.Value; }
71      set { KernelTypeParameter.Value = 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", "C_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      double trainingAccuracy, testAccuracy;
118      int nSv;
119      var solution = CreateSupportVectorClassificationSolution(problemData, selectedInputVariables,
120        SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value,
121        out trainingAccuracy, out testAccuracy, out nSv);
122
123      Results.Add(new Result("Support vector classification solution", "The support vector classification solution.", solution));
124      Results.Add(new Result("Training accuracy", "The accuracy of the SVR solution on the training partition.", new DoubleValue(trainingAccuracy)));
125      Results.Add(new Result("Test R²", "The accuracy of the SVR solution on the test partition.", new DoubleValue(testAccuracy)));
126      Results.Add(new Result("Number of support vectors", "The number of support vectors of the SVR solution.", new IntValue(nSv)));
127    }
128
129    public static SupportVectorClassificationSolution CreateSupportVectorClassificationSolution(IClassificationProblemData problemData, IEnumerable<string> allowedInputVariables,
130      string svmType, string kernelType, double cost, double nu, double gamma,
131      out double trainingAccuracy, out double testAccuracy, out int nSv) {
132      Dataset dataset = problemData.Dataset;
133      string targetVariable = problemData.TargetVariable;
134      IEnumerable<int> rows = problemData.TrainingIndices;
135
136      //extract SVM parameters from scope and set them
137      SVM.Parameter parameter = new SVM.Parameter();
138      parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
139      parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);
140      parameter.C = cost;
141      parameter.Nu = nu;
142      parameter.Gamma = gamma;
143      parameter.CacheSize = 500;
144      parameter.Probability = false;
145
146      foreach (double c in problemData.ClassValues) {
147        double wSum = 0.0;
148        foreach (double otherClass in problemData.ClassValues) {
149          if (!c.IsAlmost(otherClass)) {
150            wSum += problemData.GetClassificationPenalty(c, otherClass);
151          }
152        }
153        parameter.Weights.Add((int)c, wSum);
154      }
155
156
157      SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
158      SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
159      SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem);
160      var svmModel = SVM.Training.Train(scaledProblem, parameter);
161      var model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables, problemData.ClassValues);
162      var solution = new SupportVectorClassificationSolution(model, (IClassificationProblemData)problemData.Clone());
163
164      nSv = svmModel.SupportVectorCount;
165      trainingAccuracy = solution.TrainingAccuracy;
166      testAccuracy = solution.TestAccuracy;
167
168      return solution;
169    }
170    #endregion
171  }
172}
Note: See TracBrowser for help on using the repository browser.