I am trying to perform Kernel PCA using scikit-learn, using a kernel that is not in their implementation (and a custom input format that is recognized by this kernel). It would probably be easiest if I could just compute the kernel ahead of time, save it, and then use it in Kernel PCA.
The precomputed
argument to KernelPCA would imply that I am able to do what I want; however, it's not explained in the documentation, and I can't find any examples of it being used. Even in the unit test source code for KernelPCA in sklearn, the code doesn't ever seem to actually say what the precomputed kernel is.
Does anyone know how I would use my own precomputed kernel?
The precomputed kernel that you need to use at fit time is the gram matrix between the samples. I.e. if you have n_samples
samples denoted by x_i
, then you need to give to fit
as first parameter the matrix G
defined by G_ij = K(x_i, x_j)
for i, j
between 0
and n_samples - 1
.
E.g. for the linear kernel this is
def linear_kernel(X, Y):
return X.dot(Y.T)
X = np.random.randn(10, 20)
gram = linear_kernel(X, X)
For prediction on X_test
you need to pass
X_test = np.random.randn(5, 20)
gram_test = linear_kernel(X_test, X)
This is to be seen in the unit tests, e.g. here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With