5

I am currently using the kernels that come with sk-learn support vector machine library.

How do I extract the kernel matrix for a classifier created using sklearn.svm.SVC?

Omar Shehab
  • 1,004
  • 3
  • 14
  • 26

1 Answers1

0

Unfortunately, scikit did not provide the direct method to get kernel matrix from a well-trained svm.

But, scikit allows svm to take a custom kernel, what I did is,

  1. train a svm with specific kernel,
  2. manually calculate the kernel matrix from parameters given from the trained svm,
  3. define a new svm with the same type of kernel and the matrix, then check the new svm on the same train data to see if it is the same with previous one.

Here are codes, just taking rbf and poly as examples,

# rbf
K_train = np.exp(-clf.gamma * np.sum((X_train_C[..., None, :] - X_train_C) ** 2, axis=2))
# poly
# K_train = (clf.gamma * X_train_C.dot(X_train_C.T) + clf.coef0) ** clf.degree


clf_pre = SVC(kernel='precomputed')
clf_pre.fit(K_train, y_train_C)
pred_pre = clf_pre.predict(K_train)

There is one last thing I am not that sure, when I load the pre-computed kernel, I could not directlly use it. I need to re-fit it again, this is the same as given by scikit.

Here are examples provided by scikit.

  1. https://scikit-learn.org/stable/auto_examples/svm/plot_custom_kernel.html#sphx-glr-auto-examples-svm-plot-custom-kernel-py
  2. https://scikit-learn.org/stable/modules/svm.html?highlight=svc+custom+kernel (1.4.6.2 Custom Kernels)
  • Actually, in issue https://stackoverflow.com/questions/47271662/what-is-the-fastest-way-to-compute-an-rbf-kernel-in-python, it provides at least 4 methods to compute. – Zhengyang1995 Nov 14 '22 at 08:51