[5626] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[10556] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5626] | 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 |
|
---|
| 22 | using System;
|
---|
[5759] | 23 | using System.Collections.Generic;
|
---|
[5626] | 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[8609] | 32 | using LibSVM;
|
---|
[5626] | 33 |
|
---|
| 34 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 35 | /// <summary>
|
---|
| 36 | /// Support vector machine classification data analysis algorithm.
|
---|
| 37 | /// </summary>
|
---|
[6240] | 38 | [Item("Support Vector Classification", "Support vector machine classification data analysis algorithm (wrapper for libSVM).")]
|
---|
[5626] | 39 | [Creatable("Data Analysis")]
|
---|
| 40 | [StorableClass]
|
---|
| 41 | public sealed class SupportVectorClassification : FixedDataAnalysisAlgorithm<IClassificationProblem> {
|
---|
| 42 | private const string SvmTypeParameterName = "SvmType";
|
---|
| 43 | private const string KernelTypeParameterName = "KernelType";
|
---|
| 44 | private const string CostParameterName = "Cost";
|
---|
| 45 | private const string NuParameterName = "Nu";
|
---|
| 46 | private const string GammaParameterName = "Gamma";
|
---|
[8613] | 47 | private const string DegreeParameterName = "Degree";
|
---|
[5626] | 48 |
|
---|
| 49 | #region parameter properties
|
---|
[8121] | 50 | public IConstrainedValueParameter<StringValue> SvmTypeParameter {
|
---|
| 51 | get { return (IConstrainedValueParameter<StringValue>)Parameters[SvmTypeParameterName]; }
|
---|
[5626] | 52 | }
|
---|
[8121] | 53 | public IConstrainedValueParameter<StringValue> KernelTypeParameter {
|
---|
| 54 | get { return (IConstrainedValueParameter<StringValue>)Parameters[KernelTypeParameterName]; }
|
---|
[5626] | 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 | }
|
---|
[8613] | 65 | public IValueParameter<IntValue> DegreeParameter {
|
---|
| 66 | get { return (IValueParameter<IntValue>)Parameters[DegreeParameterName]; }
|
---|
| 67 | }
|
---|
[5626] | 68 | #endregion
|
---|
| 69 | #region properties
|
---|
| 70 | public StringValue SvmType {
|
---|
| 71 | get { return SvmTypeParameter.Value; }
|
---|
[8121] | 72 | set { SvmTypeParameter.Value = value; }
|
---|
[5626] | 73 | }
|
---|
| 74 | public StringValue KernelType {
|
---|
| 75 | get { return KernelTypeParameter.Value; }
|
---|
[8121] | 76 | set { KernelTypeParameter.Value = value; }
|
---|
[5626] | 77 | }
|
---|
| 78 | public DoubleValue Nu {
|
---|
| 79 | get { return NuParameter.Value; }
|
---|
| 80 | }
|
---|
| 81 | public DoubleValue Cost {
|
---|
| 82 | get { return CostParameter.Value; }
|
---|
| 83 | }
|
---|
| 84 | public DoubleValue Gamma {
|
---|
| 85 | get { return GammaParameter.Value; }
|
---|
| 86 | }
|
---|
[8613] | 87 | public IntValue Degree {
|
---|
| 88 | get { return DegreeParameter.Value; }
|
---|
| 89 | }
|
---|
[5626] | 90 | #endregion
|
---|
| 91 | [StorableConstructor]
|
---|
| 92 | private SupportVectorClassification(bool deserializing) : base(deserializing) { }
|
---|
| 93 | private SupportVectorClassification(SupportVectorClassification original, Cloner cloner)
|
---|
| 94 | : base(original, cloner) {
|
---|
| 95 | }
|
---|
| 96 | public SupportVectorClassification()
|
---|
| 97 | : base() {
|
---|
[5649] | 98 | Problem = new ClassificationProblem();
|
---|
| 99 |
|
---|
[6812] | 100 | List<StringValue> svrTypes = (from type in new List<string> { "NU_SVC", "C_SVC" }
|
---|
[5626] | 101 | select new StringValue(type).AsReadOnly())
|
---|
| 102 | .ToList();
|
---|
| 103 | ItemSet<StringValue> svrTypeSet = new ItemSet<StringValue>(svrTypes);
|
---|
| 104 | List<StringValue> kernelTypes = (from type in new List<string> { "LINEAR", "POLY", "SIGMOID", "RBF" }
|
---|
| 105 | select new StringValue(type).AsReadOnly())
|
---|
| 106 | .ToList();
|
---|
[5649] | 107 | ItemSet<StringValue> kernelTypeSet = new ItemSet<StringValue>(kernelTypes);
|
---|
[5626] | 108 | Parameters.Add(new ConstrainedValueParameter<StringValue>(SvmTypeParameterName, "The type of SVM to use.", svrTypeSet, svrTypes[0]));
|
---|
| 109 | Parameters.Add(new ConstrainedValueParameter<StringValue>(KernelTypeParameterName, "The kernel type to use for the SVM.", kernelTypeSet, kernelTypes[3]));
|
---|
| 110 | Parameters.Add(new ValueParameter<DoubleValue>(NuParameterName, "The value of the nu parameter nu-SVC.", new DoubleValue(0.5)));
|
---|
| 111 | Parameters.Add(new ValueParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of C-SVC.", new DoubleValue(1.0)));
|
---|
| 112 | Parameters.Add(new ValueParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0)));
|
---|
[8613] | 113 | Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName, "The degree parameter for the polynomial kernel function.", new IntValue(3)));
|
---|
[5626] | 114 | }
|
---|
| 115 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[8613] | 116 | private void AfterDeserialization() {
|
---|
| 117 | #region backwards compatibility (change with 3.4)
|
---|
| 118 | if (!Parameters.ContainsKey(DegreeParameterName))
|
---|
| 119 | Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName, "The degree parameter for the polynomial kernel function.", new IntValue(3)));
|
---|
| 120 | #endregion
|
---|
| 121 | }
|
---|
[5626] | 122 |
|
---|
| 123 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 124 | return new SupportVectorClassification(this, cloner);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | #region support vector classification
|
---|
| 128 | protected override void Run() {
|
---|
| 129 | IClassificationProblemData problemData = Problem.ProblemData;
|
---|
[5649] | 130 | IEnumerable<string> selectedInputVariables = problemData.AllowedInputVariables;
|
---|
[7430] | 131 | double trainingAccuracy, testAccuracy;
|
---|
| 132 | int nSv;
|
---|
| 133 | var solution = CreateSupportVectorClassificationSolution(problemData, selectedInputVariables,
|
---|
[8613] | 134 | SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, Degree.Value,
|
---|
[7430] | 135 | out trainingAccuracy, out testAccuracy, out nSv);
|
---|
[5626] | 136 |
|
---|
| 137 | Results.Add(new Result("Support vector classification solution", "The support vector classification solution.", solution));
|
---|
[7430] | 138 | Results.Add(new Result("Training accuracy", "The accuracy of the SVR solution on the training partition.", new DoubleValue(trainingAccuracy)));
|
---|
[8609] | 139 | Results.Add(new Result("Test accuracy", "The accuracy of the SVR solution on the test partition.", new DoubleValue(testAccuracy)));
|
---|
[7430] | 140 | Results.Add(new Result("Number of support vectors", "The number of support vectors of the SVR solution.", new IntValue(nSv)));
|
---|
[5626] | 141 | }
|
---|
| 142 |
|
---|
| 143 | public static SupportVectorClassificationSolution CreateSupportVectorClassificationSolution(IClassificationProblemData problemData, IEnumerable<string> allowedInputVariables,
|
---|
[8613] | 144 | string svmType, string kernelType, double cost, double nu, double gamma, int degree,
|
---|
[7430] | 145 | out double trainingAccuracy, out double testAccuracy, out int nSv) {
|
---|
[5626] | 146 | Dataset dataset = problemData.Dataset;
|
---|
| 147 | string targetVariable = problemData.TargetVariable;
|
---|
[8139] | 148 | IEnumerable<int> rows = problemData.TrainingIndices;
|
---|
[5626] | 149 |
|
---|
| 150 | //extract SVM parameters from scope and set them
|
---|
[8609] | 151 | svm_parameter parameter = new svm_parameter();
|
---|
| 152 | parameter.svm_type = GetSvmType(svmType);
|
---|
| 153 | parameter.kernel_type = GetKernelType(kernelType);
|
---|
[5626] | 154 | parameter.C = cost;
|
---|
[8609] | 155 | parameter.nu = nu;
|
---|
| 156 | parameter.gamma = gamma;
|
---|
| 157 | parameter.cache_size = 500;
|
---|
| 158 | parameter.probability = 0;
|
---|
| 159 | parameter.eps = 0.001;
|
---|
[8613] | 160 | parameter.degree = degree;
|
---|
[8609] | 161 | parameter.shrinking = 1;
|
---|
| 162 | parameter.coef0 = 0;
|
---|
[5626] | 163 |
|
---|
[8609] | 164 |
|
---|
| 165 | var weightLabels = new List<int>();
|
---|
| 166 | var weights = new List<double>();
|
---|
[6812] | 167 | foreach (double c in problemData.ClassValues) {
|
---|
| 168 | double wSum = 0.0;
|
---|
| 169 | foreach (double otherClass in problemData.ClassValues) {
|
---|
| 170 | if (!c.IsAlmost(otherClass)) {
|
---|
| 171 | wSum += problemData.GetClassificationPenalty(c, otherClass);
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
[8609] | 174 | weightLabels.Add((int)c);
|
---|
| 175 | weights.Add(wSum);
|
---|
[6812] | 176 | }
|
---|
[8609] | 177 | parameter.weight_label = weightLabels.ToArray();
|
---|
| 178 | parameter.weight = weights.ToArray();
|
---|
[5626] | 179 |
|
---|
[6812] | 180 |
|
---|
[8609] | 181 | svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
|
---|
| 182 | RangeTransform rangeTransform = RangeTransform.Compute(problem);
|
---|
| 183 | svm_problem scaledProblem = rangeTransform.Scale(problem);
|
---|
| 184 | var svmModel = svm.svm_train(scaledProblem, parameter);
|
---|
[7430] | 185 | var model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables, problemData.ClassValues);
|
---|
| 186 | var solution = new SupportVectorClassificationSolution(model, (IClassificationProblemData)problemData.Clone());
|
---|
[5626] | 187 |
|
---|
[8609] | 188 | nSv = svmModel.SV.Length;
|
---|
[7430] | 189 | trainingAccuracy = solution.TrainingAccuracy;
|
---|
| 190 | testAccuracy = solution.TestAccuracy;
|
---|
| 191 |
|
---|
| 192 | return solution;
|
---|
[5626] | 193 | }
|
---|
[8609] | 194 |
|
---|
| 195 | private static int GetSvmType(string svmType) {
|
---|
| 196 | if (svmType == "NU_SVC") return svm_parameter.NU_SVC;
|
---|
| 197 | if (svmType == "C_SVC") return svm_parameter.C_SVC;
|
---|
| 198 | throw new ArgumentException("Unknown SVM type");
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | private static int GetKernelType(string kernelType) {
|
---|
| 202 | if (kernelType == "LINEAR") return svm_parameter.LINEAR;
|
---|
| 203 | if (kernelType == "POLY") return svm_parameter.POLY;
|
---|
| 204 | if (kernelType == "SIGMOID") return svm_parameter.SIGMOID;
|
---|
| 205 | if (kernelType == "RBF") return svm_parameter.RBF;
|
---|
| 206 | throw new ArgumentException("Unknown kernel type");
|
---|
| 207 | }
|
---|
[5626] | 208 | #endregion
|
---|
| 209 | }
|
---|
| 210 | }
|
---|