SKLearn Kernel PCA “Precomputed” argument

泄露秘密 提交于 2021-02-18 12:49:05

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/25355620/sklearn-kernel-pca-precomputed-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!