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 isdef 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 passX_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 这篇关于SKLearn 内核 PCA“预计算"争论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 09:09
查看更多