SKLearn Kernel PCA "Predictive" argument

I am trying to execute Kernel PCA with scikit-learn using a kernel that is not part of their implementation (and a custom input format that is recognized by that kernel). It would probably be easiest if I could just figure out the kernel ahead of time, save it, and then use it in the Kernel PCA.

The argument precomputed

for KernelPCA means that I can do what I want; however, this is not explained in the documentation and I cannot find examples of its use. Even in the source code of the unit test for KernelPCA in sklearn, the code never says that the kernel is precomputed.

Does anyone know how I will use my own precomputed kernel?

+3


source to share


1 answer


The precomputed kernel that you need to use at the right time is the grammatical matrix between patterns. That is, if you have n_samples

patterns denoted by a symbol x_i

, then you need to give fit

as the first parameter the matrix G

defined G_ij = K(x_i, x_j)

for i, j

between 0

and n_samples - 1

.

eg. for a linear kernel it is

def linear_kernel(X, Y):
    return X.dot(Y.T)

X = np.random.randn(10, 20)
gram = linear_kernel(X, X)

      



To predict on X_test

you need to pass

X_test = np.random.randn(5, 20)
gram_test = linear_kernel(X_test, X)

      

This can be seen in unit tests for example. here

+4


source







All Articles