# Working One-class SVM in Julia?

**URL:** <https://discourse.julialang.org/t/working-one-class-svm-in-julia/43747>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 27, 2020, 8:39am UTC](https://discourse.julialang.org/t/working-one-class-svm-in-julia/43747 "2020-07-27T08:39:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [July 27, 2020, 8:39am UTC](https://discourse.julialang.org/t/working-one-class-svm-in-julia/43747/1 "2020-07-27T08:39:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [July 27, 2020, 9:03am UTC](https://discourse.julialang.org/t/working-one-class-svm-in-julia/43747/2 "2020-07-27T09:03:17Z")

</div>

> [@\[ANN\] KernelMachines.jl](https://discourse.julialang.org/t/ann-kernelmachines-jl/43043):
>
> We are happy to announce the release of [KernelMachines.jl](https://gitlab.com/VeosDigital/KernelMachines.jl). Framework Kernel machines are a special case of a more general framework, parametric machines—see [this article](https://arxiv.org/abs/2007.02777) for technical details on the framework. The key idea of parametric machines in general (and kernel machines in particular) is to build large spaces of “neural network-like” architectures and ensure that those spaces have good geometrical properties: completeness and, for a regularized problem on a finite training dataset, com…

perhaps?

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [July 27, 2020, 9:21am UTC](https://discourse.julialang.org/t/working-one-class-svm-in-julia/43747/3 "2020-07-27T09:21:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![avik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avik/32/17_2.png) [@avik](https://discourse.julialang.org/u/avik)\
**Post date:** [July 27, 2020, 10:17am UTC](https://discourse.julialang.org/t/working-one-class-svm-in-julia/43747/4 "2020-07-27T10:17:24Z")

</div>

xref: github issue: [https://github.com/mpastell/LIBSVM.jl/issues/56](https://github.com/mpastell/LIBSVM.jl/issues/56)

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [July 27, 2020, 11:43pm UTC](https://discourse.julialang.org/t/working-one-class-svm-in-julia/43747/5 "2020-07-27T23:43:35Z")

</div>

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`:

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/3/2/32ca676c773ee89e977b2b9dad8db9df8b3709ed.png)

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [July 28, 2020, 9:22am UTC](https://discourse.julialang.org/t/working-one-class-svm-in-julia/43747/6 "2020-07-28T09:22:31Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [July 28, 2020, 2:56pm UTC](https://discourse.julialang.org/t/working-one-class-svm-in-julia/43747/7 "2020-07-28T14:56:52Z")

</div>

Glad it helped!

I used the docstrings in LIBSVM.jl (for example `?svmtraing` in the REPL) and the [LIBSVM](https://www.csie.ntu.edu.tw/~cjlin/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.
