Working One-class SVM in Julia?

Hi. Does anyone know of a working One-Class SVM in Julia? There is a ‘OneClassSVM’ option in LIBSVM, but it doesn’t work properly (which I have documented on GItHub, but there doesn’t seem to be much activity on the LIBSVM.jl page in the past 2 years). Scikit-Learn (apparently) uses the LIBSVM one which has given me wrong answers on simple test problems.

Thanks for any suggestions!
PS I have seen SVM.jl which seems very out of date.

1 Like

perhaps?

1 Like

Great! Can’t wait to try this! Thanks!

1 Like

xref: github issue: https://github.com/mpastell/LIBSVM.jl/issues/56

1 Like

I think maybe the defaults for LIBSVM (inherited from the underlying library) are not appropriate for the suggested test. Here is the test, with a Linear kernel and a small value for nu:

using LIBSVM
using Random
using Plots

rng = MersenneTwister(42)
traindata = randn(rng, (2, 500))

posdata = traindata[:, traindata[1, :] + traindata[2, :] .> 1]

fig = scatter(posdata[1, :], posdata[2, :]; label="Train", marker=:o, color=:blue)

# Default for comparison
# mdl = svmtrain(posdata; svmtype=OneClassSVM, nu=0.01)
mdl = svmtrain(posdata; svmtype=OneClassSVM, kernel=Kernel.Linear, nu=0.01)

testdata = randn(rng, (2, 200))

labels, values = svmpredict(mdl, testdata)

pos_data = testdata[:, labels]
neg_data = testdata[:, .~labels]

scatter!(fig, pos_data[1, :], pos_data[2,:]; marker=:+, color=:green, label="Test positive")
scatter!(fig, neg_data[1, :], neg_data[2,:]; marker=:+, color=:red, label="Test negative")

savefig(fig, "OneClassSVM.png")
display(fig)

OneClassSVM

2 Likes

That’s great! Thank you for this. That is really very helpful.
It looks like the default value of nu is chosen with the other SVM types in mind, but gives very bad performance for One-Class (on examples I have tested).

I looked for examples (like this) using LIBSVM in Julia, or more documentation, for the Julia implementation, but I couldn’t find any. Do you know of somewhere? If so, that would be really helpful. If not, then I think potential users would find your example above extremely helpful (maybe on here is good enough).

Thank you again!

Glad it helped!

I used the docstrings in LIBSVM.jl (for example ?svmtraing in the REPL) and the LIBSVM documentation. I didn’t find a specific set of examples for LIBSVM.jl, but most of the examples in the LIBSVM documentation should translate well.