# How do I specify a precomputed kernel in SVM using LIBSVM or MLJ?

**URL:** https://discourse.julialang.org/t/how-do-i-specify-a-precomputed-kernel-in-svm-using-libsvm-or-mlj/61962
**Category:** Machine Learning
**Created:** [May 27, 2021, 9:33pm UTC](https://discourse.julialang.org/t/how-do-i-specify-a-precomputed-kernel-in-svm-using-libsvm-or-mlj/61962 "2021-05-27T21:33:21Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)
#### Post date: [May 27, 2021, 9:33pm UTC](https://discourse.julialang.org/t/how-do-i-specify-a-precomputed-kernel-in-svm-using-libsvm-or-mlj/61962/1 "2021-05-27T21:33:21Z")

</div>

Question arising in an MLJ slack post:

maybe this is a naive question, but how do you actually train an SVM using a precomputed kernel using LIBSVM or MLJ? I can’t seem to find a way to “give” the kernel matrix to any sort of fit function or model as a parameter

---

<div class="post-metadata">

### Author: ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)
#### Post date: [May 27, 2021, 9:45pm UTC](https://discourse.julialang.org/t/how-do-i-specify-a-precomputed-kernel-in-svm-using-libsvm-or-mlj/61962/2 "2021-05-27T21:45:36Z")

</div>

This is not clear, even in the LIBSVM (C-code) docs, as far as I can tell. However, I found this [document](https://github.com/cjlin1/libsvm/blob/master/README#L138) which explains the kernel should be provided in a file, which is specified as a command-line argument in the C-case.

Looking at the LIBSVM.jl code, I can’t find an interface point, which means there won’t be one in the MLJ wrapper either, as this is based on the former.

I’ve opened [an issue](https://github.com/JuliaML/LIBSVM.jl/issues/71) at LIBSVM.jl querying this.

Happy to add the interface point to the MLJ wrapper when this is clarified.

@mpastell

---

<div class="post-metadata">

### Author: ![math4mad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math4mad/32/49486_2.png) [@math4mad](https://discourse.julialang.org/u/math4mad)
#### Post date: [May 16, 2023, 6:19am UTC](https://discourse.julialang.org/t/how-do-i-specify-a-precomputed-kernel-in-svm-using-libsvm-or-mlj/61962/3 "2023-05-16T06:19:54Z")

</div>

It’s long time, find this way

snippets based on [MLJ SVM tutorial](https://juliaai.github.io/DataScienceTutorials.jl/isl/lab-9/index.html)

```julia
import MLJ: fit!,predict

using MLJ
using Plots
using PrettyPrinting
using Random
using KernelFunctions

#define kernelmethods
k1=PolynomialKernel(; degree=2, c=1)
k2 = SqExponentialKernel() ∘ ScaleTransform(1.5)

# make data
n1=n2=10
Random.seed!(3203)
X = randn(20, 2)
y=vcat(fill(-1, n1), fill(1, n2))
xs,ys=X[:,1],X[:,2]
#scatter(X[:,1],X[:,2],group=y,label=false)

X = MLJ.table(X)
y = categorical(y);

# work flow 
@time SVC = @load SVC pkg=LIBSVM

svc_mdl = SVC(kernel=k2) #<=== kernel is here

svc = machine(svc_mdl, X, y)

fit!(svc);

ypred =predict(svc, X)

misclassification_rate(ypred, y)

```

---

<div class="post-metadata">

### Author: ![math4mad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math4mad/32/49486_2.png) [@math4mad](https://discourse.julialang.org/u/math4mad)
#### Post date: [May 16, 2023, 6:49am UTC](https://discourse.julialang.org/t/how-do-i-specify-a-precomputed-kernel-in-svm-using-libsvm-or-mlj/61962/4 "2023-05-16T06:49:26Z")

</div>

and Use this way to find more detail docs

```julia
using MLJ # or MLJModels 
doc("NuSVC", pkg="LIBSVM")

```

```julia
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)

```

---

<div class="post-metadata">

### Author: ![math4mad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math4mad/32/49486_2.png) [@math4mad](https://discourse.julialang.org/u/math4mad)
#### Post date: [May 17, 2023, 8:01pm UTC](https://discourse.julialang.org/t/how-do-i-specify-a-precomputed-kernel-in-svm-using-libsvm-or-mlj/61962/5 "2023-05-17T20:01:25Z")

</div>

As for **kernel matrix**

please look at this

[support-vector-machine](https://juliagaussianprocesses.github.io/KernelFunctions.jl/stable/examples/support-vector-machine/)

```julia
using KernelFunctions
using LIBSVM

# custom kernel function
k = SqExponentialKernel() ∘ ScaleTransform(1.5)

# configure model
model = svmtrain(kernelmatrix(k, x_train), y_train; kernel=LIBSVM.Kernel.Precomputed)

# predict with model

y_pred, _ = svmpredict(model, kernelmatrix(k, x_train, x_test));

```
