Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kernel pca scikit plotting code extension for multiple gammas #122

Open
teamiceberg opened this issue Jan 11, 2020 · 0 comments
Open

kernel pca scikit plotting code extension for multiple gammas #122

teamiceberg opened this issue Jan 11, 2020 · 0 comments

Comments

@teamiceberg
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant