NuSVC
NuSVC
A model type for constructing a ν-support vector classifier, based on LIBSVM.jl, and implementing the MLJ model interface.
From MLJ, the type can be imported using
NuSVC = @load NuSVC pkg=LIBSVM
Do model = NuSVC()
to construct an instance with default hyper-parameters. Provide keyword arguments to override hyper-parameter defaults, as in NuSVC(kernel=...)
.
This model is a re-parameterization of the SVC
classifier, where nu
replaces cost
, and is mathematically equivalent to it. The parameter nu
allows more direct control over the number of support vectors (see under "Hyper-parameters").
This model always predicts actual class labels. For probabilistic predictions, use instead ProbabilisticNuSVC
.
Reference for algorithm and core C-library: C.-C. Chang and C.-J. Lin (2011): "LIBSVM: a library for support vector machines." ACM Transactions on Intelligent Systems and Technology, 2(3):27:1–27:27. Updated at https://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf.
Training data
In MLJ or MLJBase, bind an instance model
to data with:
mach = machine(model, X, y)
where
X
: any table of input features (eg, aDataFrame
) whose columns each haveContinuous
element scitype; check column scitypes withschema(X)
y
: is the target, which can be anyAbstractVector
whose element scitype is<:OrderedFactor
or<:Multiclass
; check the scitype withscitype(y)
Train the machine using fit!(mach, rows=...)
.
Hyper-parameters
kernel=LIBSVM.Kernel.RadialBasis
: either an object that can be called, as inkernel(x1, x2)
, or one of the built-in kernels from the LIBSVM.jl package listed below. Herex1
andx2
are vectors whose lengths match the number of columns of the training dataX
(see "Examples" below).LIBSVM.Kernel.Linear
:(x1, x2) -> x1'*x2
LIBSVM.Kernel.Polynomial
:(x1, x2) -> gamma*x1'*x2 + coef0)^degree
LIBSVM.Kernel.RadialBasis
:(x1, x2) -> (exp(-gamma*norm(x1 - x2)^2))
LIBSVM.Kernel.Sigmoid
:(x1, x2) - > tanh(gamma*x1'*x2 + coef0)
Here
gamma
,coef0
,degree
are other hyper-parameters. Serialization of models with user-defined kernels comes with some restrictions. See LIVSVM.jl issue91gamma = 0.0
: kernel parameter (see above); ifgamma==-1.0
thengamma = 1/nfeatures
is used in training, wherenfeatures
is the number of features (columns ofX
). Ifgamma==0.0
thengamma = 1/(var(Tables.matrix(X))*nfeatures)
is used. Actual value used appears in the report (see below).coef0 = 0.0
: kernel parameter (see above)degree::Int32 = Int32(3)
: degree in polynomial kernel (see above)nu=0.5
(range (0, 1]): An upper bound on the fraction of margin errors and a lower bound of the fraction of support vectors. Denotedν
in the cited paper. Changingnu
changes the thickness of the margin (a neighborhood of the decision surface) and a margin error is said to have occurred if a training observation lies on the wrong side of the surface or within the margin.cachesize=200.0
cache memory size in MBtolerance=0.001
: tolerance for the stopping criterionshrinking=true
: whether to use shrinking heuristics
Operations
predict(mach, Xnew)
: return predictions of the target given featuresXnew
having the same scitype asX
above.
Fitted parameters
The fields of fitted_params(mach)
are:
libsvm_model
: the trained model object created by the LIBSVM.jl packageencoding
: class encoding used internally bylibsvm_model
- a dictionary of class labels keyed on the internal integer representation
Report
The fields of report(mach)
are:
gamma
: actual value of the kernel parametergamma
used in training
Examples
Using a built-in kernel
using MLJ
import LIBSVM
NuSVC = @load NuSVC pkg=LIBSVM ## model type
model = NuSVC(kernel=LIBSVM.Kernel.Polynomial) ## instance
X, y = @load_iris ## table, vector
mach = machine(model, X, y) |> fit!
Xnew = (sepal_length = [6.4, 7.2, 7.4],
sepal_width = [2.8, 3.0, 2.8],
petal_length = [5.6, 5.8, 6.1],
petal_width = [2.1, 1.6, 1.9],)
julia> yhat = predict(mach, Xnew)
3-element CategoricalArrays.CategoricalArray{String,1,UInt32}:
"virginica"
"virginica"
"virginica"
User-defined kernels
k(x1, x2) = x1'*x2 ## equivalent to `LIBSVM.Kernel.Linear`
model = NuSVC(kernel=k)
mach = machine(model, X, y) |> fit!
julia> yhat = predict(mach, Xnew)
3-element CategoricalArrays.CategoricalArray{String,1,UInt32}:
"virginica"
"virginica"
"virginica"
See also the classifiers SVC
and LinearSVC
, LIVSVM.jl and the original C implementation. documentation.