source: trunk/sources/HeuristicLab.Tests/Test Resources/Script Sources/GridSearch_SVM_Classification_Script.cs @ 11514

Last change on this file since 11514 was 11514, checked in by jkarder, 8 years ago

#2211:

  • updated/added unit tests
    • added AssemblyInitialize method to load all plugins, create output directories for (script) samples and initialize the MainForm
    • script code is now stored in test resource files
    • refactored unit tests
  • updated (script) samples
  • added Test.cmd
File size: 5.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Linq.Expressions;
5
6using HeuristicLab.Algorithms.DataAnalysis;
7using HeuristicLab.Core;
8using HeuristicLab.Data;
9using HeuristicLab.Parameters;
10using HeuristicLab.Problems.DataAnalysis;
11using HeuristicLab.Problems.Instances.DataAnalysis;
12using HeuristicLab.Scripting;
13
14using LibSVM;
15
16public class SVMClassificationCrossValidationScript : HeuristicLab.Scripting.CSharpScriptBase {
17  /* Maximum degree of parallelism (specifies whether or not the grid search should be parallelized) */
18  const int maximumDegreeOfParallelism = 4;
19
20  /* Number of crossvalidation folds: */
21  const int numberOfFolds = 5;
22
23  /* Specify whether the folds should be shuffled */
24  const bool shuffleFolds = false;
25
26  /* The tunable SVM parameters:
27     - "C" (penalty factor) effects the trade-off between complexity and proportion of nonseparable samples and must be selected by the user. Can have any positive value.
28     - "nu" is an upper bound on the fraction of margin errors and a lower bound of the fraction of support vectors relative to the total number of training examples.
29     - "degree" represents the polynomial kernel degree
30     - "eps" (epsilon) determines the level of accuracy of the approximated function. It controls the width of the epsilon-insensitive zone used to fit the training data.
31       With optimal values of epsilon, the parameter C has negligible effect.
32     - "degree" represents the degree of the polynomial kernel
33     - "kernel_type" specifies the kernel to be used: linear, polynomial, radial basis or sigmoidal.
34       Valid values: svm_parameter.LINEAR, svm_parameter.POLY, svm_parameter.RBF, svm_parameter.SIGMOID
35     Comment or uncomment the parameter ranges below as needed.  */
36
37  static Dictionary<string, IEnumerable<double>> svmParameterRanges = new Dictionary<string, IEnumerable<double>> {
38        { "svm_type", new List<double> {svm_parameter.NU_SVC } },
39        { "kernel_type", new List<double> { svm_parameter.RBF }},
40        { "C", ValueGenerator.GenerateSteps(-1m, 10, 1).Select(x => Math.Pow(2,(double)x)) },
41        { "gamma", ValueGenerator.GenerateSteps(-4m, -1, 1).Select(x => Math.Pow(2,(double)x)) },
42//        { "eps", ValueGenerator.GenerateSteps(-8m, -1, 1).Select(x => Math.Pow(2, (double)x)) },
43//        { "nu" , ValueGenerator.GenerateSteps(0m, 1, 0.05m).Select(x => Math.Pow(2, (double)x)) },
44//        { "degree", ValueGenerator.GenerateSteps(1m, 4, 1).Select(x => (double)x) }
45  };
46
47  static Dictionary<int, string> svmTypes = new Dictionary<int, string> {
48    { svm_parameter.NU_SVC, "NU_SVC" },
49    { svm_parameter.C_SVC, "C_SVC" }
50  };
51
52  static Dictionary<int, string> kernelTypes = new Dictionary<int, string> {
53    { svm_parameter.LINEAR, "LINEAR" },
54    { svm_parameter.POLY, "POLY" },
55    { svm_parameter.RBF, "RBF" },
56    { svm_parameter.SIGMOID, "SIGMOID" }
57  };
58
59  private static SupportVectorClassificationSolution SvmGridSearch(IClassificationProblemData problemData, out svm_parameter bestParameters, out int nSv) {
60    bestParameters = SupportVectorMachineUtil.GridSearch(problemData, svmParameterRanges, numberOfFolds, shuffleFolds, maximumDegreeOfParallelism);
61    double trainingError, testError;
62    string svmType = svmTypes[bestParameters.svm_type];
63    string kernelType = kernelTypes[bestParameters.kernel_type];
64    var svm_solution = SupportVectorClassification.CreateSupportVectorClassificationSolution(problemData, problemData.AllowedInputVariables, svmType, kernelType,
65                       bestParameters.C, bestParameters.nu, bestParameters.gamma, bestParameters.degree, out trainingError, out testError, out nSv);
66    return svm_solution;
67  }
68
69  public override void Main() {
70    var variables = (Variables)vars;
71    var item = variables.SingleOrDefault(x => x.Value is IClassificationProblem || x.Value is IClassificationProblemData);
72    if (item.Equals(default(KeyValuePair<string, object>)))
73      throw new ArgumentException("Could not find a suitable problem or problem data.");
74
75    string name = item.Key;
76    IClassificationProblemData problemData;
77    if (item.Value is IClassificationProblem)
78      problemData = ((IClassificationProblem)item.Value).ProblemData;
79    else
80      problemData = (IClassificationProblemData)item.Value;
81
82    int nSv; // number of support vectors
83    svm_parameter bestParameters;
84    var bestSolution = SvmGridSearch(problemData, out bestParameters, out nSv);
85
86    vars["bestSolution"] = bestSolution;
87    Console.WriteLine(name + " parameters: C = {0}, g = {1:0.000}, eps = {2:0.000}, nu = {3:0.000}, degree = {4}", bestParameters.C, bestParameters.gamma, bestParameters.eps, bestParameters.nu, bestParameters.degree);
88    Console.WriteLine(name + " best solution accuracy (training): " + bestSolution.TrainingAccuracy + ", accuracy (test): " + bestSolution.TestAccuracy);
89
90    var bestParametersCollection = new ParameterCollection();
91    foreach (var p in svmParameterRanges.Keys) {
92      var getter = GenerateGetter(p);
93      bestParametersCollection.Add(new FixedValueParameter<DoubleValue>(p, new DoubleValue(getter(bestParameters))));
94    }
95    vars["bestParameters"] = bestParametersCollection;
96  }
97
98  private static Func<svm_parameter, double> GenerateGetter(string field) {
99    var paramExpr = Expression.Parameter(typeof(svm_parameter));
100    var getterExpr = Expression.Convert(Expression.Field(paramExpr, field), typeof(double)); // cast to double
101    Func<svm_parameter, double> f = Expression.Lambda<Func<svm_parameter, double>>(getterExpr, paramExpr).Compile();
102    return f;
103  }
104}
Note: See TracBrowser for help on using the repository browser.