9
$\begingroup$

I have a dataset that I want to classify and I do the following:

clf = Classify[xtrain -> ytrain, Method -> "SupportVectorMachine"]

How can I use my own values for the parameters of the SupportVectorMachine ?

$\endgroup$

2 Answers 2

10
$\begingroup$

You can try setting them as suboptions

Classify[xtrain -> ytrain, 
 Method -> {"SupportVectorMachine", "KernelType" -> "Linear", 
   "SoftMarginParameter" -> 2}]
$\endgroup$
3
  • $\begingroup$ Could you please let me know how to find what sub options are available? $\endgroup$
    – chris
    Commented May 9, 2015 at 17:05
  • $\begingroup$ @chris I don't recall exactly, sorry. Hopefully, they will get documented $\endgroup$
    – Rojo
    Commented May 9, 2015 at 22:18
  • $\begingroup$ Seems some suboptions changed on 13.2. For example MulticlassStrategy $\endgroup$ Commented Dec 21, 2022 at 0:28
0
$\begingroup$

It is easy to get access to the parameters of a SVM. Let c be something like

c = Classify[
  Table[data1[[i, 1]] -> data1[[i, 2]], {i, 1, Length[data1]}], 
  Method -> {"SupportVectorMachine", 
    "KernelType" -> "RadialBasisFunction"}]

then we get the support vectors, support vector coefficients, rho and gamma scaling parameters as follows:

 GammaScalingParameter = 
      c[[1]]["Model"]["SVMParameters"]["GammaScalingParameter"];

    supportvectors = 
      c[[1]]["Model"]["TrainedModel"][[1]]["supportVectors"];

supportVectorCoefficients = 
 c[[1]]["Model"]["TrainedModel"][[1]]["supportVectorCoefficients"]

rho = c[[1]]["Model"]["TrainedModel"][[1]]["rho"]

and finally we can compute the decision function f

f[z_] :=Sum[supportVectorCoefficients[[i]] K[supportvectors[[i]], z], {i, 1, 
    Length[supportvectors]}] - rho

whereby K is the Radial basis kernel

K[x1_, x2_] := Exp[-GammaScalingParameter Norm[(x1 - x2)]^2]

I think the whole procedure works also for other kernels.

Here is an illustration. Here we have 2 groups with data (Fig.1) Illustration of data

Here we illustrate the decision areas of the SVM

Illustration of decision areas

and here we illustrate the values of the decision function of each data point:

Illustration of decision function for each data point

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.