Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/14/12 18:58:15 (12 years ago)
Author:
gkronber
Message:

#1847 merged r8205:8635 from trunk into branch

Location:
branches/GP-MoveOperators
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/GP-MoveOperators

  • branches/GP-MoveOperators/HeuristicLab.Algorithms.DataAnalysis/3.4

    • Property svn:ignore
      •  

        old new  
        55*.vs10x
        66Plugin.cs
         7*.user
  • branches/GP-MoveOperators/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassification.cs

    r8206 r8660  
    3030using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3131using HeuristicLab.Problems.DataAnalysis;
     32using LibSVM;
    3233
    3334namespace HeuristicLab.Algorithms.DataAnalysis {
     
    4445    private const string NuParameterName = "Nu";
    4546    private const string GammaParameterName = "Gamma";
     47    private const string DegreeParameterName = "Degree";
    4648
    4749    #region parameter properties
     
    6062    public IValueParameter<DoubleValue> GammaParameter {
    6163      get { return (IValueParameter<DoubleValue>)Parameters[GammaParameterName]; }
     64    }
     65    public IValueParameter<IntValue> DegreeParameter {
     66      get { return (IValueParameter<IntValue>)Parameters[DegreeParameterName]; }
    6267    }
    6368    #endregion
     
    7984    public DoubleValue Gamma {
    8085      get { return GammaParameter.Value; }
     86    }
     87    public IntValue Degree {
     88      get { return DegreeParameter.Value; }
    8189    }
    8290    #endregion
     
    103111      Parameters.Add(new ValueParameter<DoubleValue>(CostParameterName, "The value of the C (cost) parameter of C-SVC.", new DoubleValue(1.0)));
    104112      Parameters.Add(new ValueParameter<DoubleValue>(GammaParameterName, "The value of the gamma parameter in the kernel function.", new DoubleValue(1.0)));
     113      Parameters.Add(new ValueParameter<IntValue>(DegreeParameterName, "The degree parameter for the polynomial kernel function.", new IntValue(3)));
    105114    }
    106115    [StorableHook(HookType.AfterDeserialization)]
    107     private void AfterDeserialization() { }
     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    }
    108122
    109123    public override IDeepCloneable Clone(Cloner cloner) {
     
    118132      int nSv;
    119133      var solution = CreateSupportVectorClassificationSolution(problemData, selectedInputVariables,
    120         SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value,
     134        SvmType.Value, KernelType.Value, Cost.Value, Nu.Value, Gamma.Value, Degree.Value,
    121135        out trainingAccuracy, out testAccuracy, out nSv);
    122136
    123137      Results.Add(new Result("Support vector classification solution", "The support vector classification solution.", solution));
    124138      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 ", "The accuracy of the SVR solution on the test partition.", new DoubleValue(testAccuracy)));
     139      Results.Add(new Result("Test accuracy", "The accuracy of the SVR solution on the test partition.", new DoubleValue(testAccuracy)));
    126140      Results.Add(new Result("Number of support vectors", "The number of support vectors of the SVR solution.", new IntValue(nSv)));
    127141    }
    128142
    129143    public static SupportVectorClassificationSolution CreateSupportVectorClassificationSolution(IClassificationProblemData problemData, IEnumerable<string> allowedInputVariables,
    130       string svmType, string kernelType, double cost, double nu, double gamma,
     144      string svmType, string kernelType, double cost, double nu, double gamma, int degree,
    131145      out double trainingAccuracy, out double testAccuracy, out int nSv) {
    132146      Dataset dataset = problemData.Dataset;
     
    135149
    136150      //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);
     151      svm_parameter parameter = new svm_parameter();
     152      parameter.svm_type = GetSvmType(svmType);
     153      parameter.kernel_type = GetKernelType(kernelType);
    140154      parameter.C = cost;
    141       parameter.Nu = nu;
    142       parameter.Gamma = gamma;
    143       parameter.CacheSize = 500;
    144       parameter.Probability = false;
    145 
     155      parameter.nu = nu;
     156      parameter.gamma = gamma;
     157      parameter.cache_size = 500;
     158      parameter.probability = 0;
     159      parameter.eps = 0.001;
     160      parameter.degree = degree;
     161      parameter.shrinking = 1;
     162      parameter.coef0 = 0;
     163
     164
     165      var weightLabels = new List<int>();
     166      var weights = new List<double>();
    146167      foreach (double c in problemData.ClassValues) {
    147168        double wSum = 0.0;
     
    151172          }
    152173        }
    153         parameter.Weights.Add((int)c, wSum);
     174        weightLabels.Add((int)c);
     175        weights.Add(wSum);
    154176      }
    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);
     177      parameter.weight_label = weightLabels.ToArray();
     178      parameter.weight = weights.ToArray();
     179
     180
     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);
    161185      var model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables, problemData.ClassValues);
    162186      var solution = new SupportVectorClassificationSolution(model, (IClassificationProblemData)problemData.Clone());
    163187
    164       nSv = svmModel.SupportVectorCount;
     188      nSv = svmModel.SV.Length;
    165189      trainingAccuracy = solution.TrainingAccuracy;
    166190      testAccuracy = solution.TestAccuracy;
    167191
    168192      return solution;
     193    }
     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");
    169207    }
    170208    #endregion
Note: See TracChangeset for help on using the changeset viewer.