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
?
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
?
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,
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.