Skip to content

kernel pca scikit plotting code extension for multiple gammas #122

Description

@teamiceberg

@rasbt

I extended the sci-kit kernel PCA for transforming the half moon example to produce plots for multiple gammas. This sequence of plots can then demonstrate how a gamma value of 15 visually makes sense. Thought your readers might find it useful.

Here's the small extension code.

`#Kernel principal component analysis in scikit-learn analyzed for a stream of gammas
gamma = [1, 3, 5, 7, 10, 12, 15, 20]
X, y = make_moons(n_samples=100, random_state=123)
ncols = 2
nrows_tup=divmod(len(gamma),ncols)
fig, ax = plt.subplots(nrows=nrows_tup[0] + nrows_tup[1], ncols=ncols)
textpos = (0.0, 0.3)
for g in enumerate(gamma):
scikit_kpca = KernelPCA(n_components=2, kernel='rbf', gamma=g[1])
X_skernpca = scikit_kpca.fit_transform(X)
idx = divmod(g[0] , ncols)
ax[idx].scatter(X_skernpca[y == 0, 0], X_skernpca[y == 0, 1],
color='red', marker='^', alpha=0.5)
ax[idx].scatter(X_skernpca[y == 1, 0], X_skernpca[y == 1, 1],
color='blue', marker='o', alpha=0.5)

ax[idx].set_xlabel('PC1', fontsize = 8)
ax[idx].set_ylabel('PC2', fontsize = 8)
ax[idx].text(textpos[0], textpos[1], "gamma:"+str(g[1]), fontsize = 6 )

plt.suptitle('Sci-kit RBF kernel PCA: with gamma = ' + str(gamma),
va = "bottom", fontsize=8)

plt.tight_layout()
plt.show()`

Here's the matplot lib output:
multiple_gammas_rbfkernel_halfmoonssyntheticdata

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions