Kernel principal component analysis in scikit-learn

来源:互联网 发布:梦里花落知多少句子 编辑:程序博客网 时间:2024/04/30 20:57

For our convenience, scikit-learn implements a kernel PCA class in the sklearn.decompositionsubmodule. The usage is similar to the standard PCA class, and we can specify the kernel via thekernel parameter .

from sklearn.decomposition import KernelPCAX, y = make_moons(n_samples=100, random_state=123)scikit_kpca = KernelPCA(n_components=2, kernel='rbf', gamma=15)X_skernpca = scikit_kpca.fit_transform(X)

Plot the transformed half-moon shape data onto the frst two principal components

plt.scatter(X_skernpca[y == 0, 0], X_skernpca[y == 0, 1],            color='red', marker='^', alpha=0.5)plt.scatter(X_skernpca[y == 1, 0], X_skernpca[y == 1, 1],            color='blue', marker='o', alpha=0.5)plt.xlabel('PC1')plt.ylabel('PC2')plt.show()

Reference:《Python Machine Learning》


0 0