You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
@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)
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:
The text was updated successfully, but these errors were encountered: