2011-04-01 17:50:07 +08:00
|
|
|
"""
|
|
|
|
|
==========
|
|
|
|
|
Kernel PCA
|
|
|
|
|
==========
|
|
|
|
|
|
2011-04-03 07:47:28 +08:00
|
|
|
This example shows that Kernel PCA is able to find a projection of the data
|
|
|
|
|
that makes data linearly separable.
|
2011-04-01 17:50:07 +08:00
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2011-04-01 17:50:07 +08:00
|
|
|
|
|
|
|
|
# Authors: Mathieu Blondel
|
2012-03-04 22:37:30 +08:00
|
|
|
# Andreas Mueller
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2011-04-01 17:50:07 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2011-04-01 17:50:07 +08:00
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.decomposition import PCA, KernelPCA
|
2012-03-04 22:37:30 +08:00
|
|
|
from sklearn.datasets import make_circles
|
2011-04-01 17:50:07 +08:00
|
|
|
|
|
|
|
|
np.random.seed(0)
|
|
|
|
|
|
2012-03-04 22:37:30 +08:00
|
|
|
X, y = make_circles(n_samples=400, factor=.3, noise=.05)
|
2011-04-03 07:47:28 +08:00
|
|
|
|
2012-05-02 10:02:04 +08:00
|
|
|
kpca = KernelPCA(kernel="rbf", fit_inverse_transform=True, gamma=10)
|
2011-04-01 17:50:07 +08:00
|
|
|
X_kpca = kpca.fit_transform(X)
|
|
|
|
|
X_back = kpca.inverse_transform(X_kpca)
|
|
|
|
|
pca = PCA()
|
|
|
|
|
X_pca = pca.fit_transform(X)
|
|
|
|
|
|
2011-04-01 20:47:31 +08:00
|
|
|
# Plot results
|
|
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure()
|
|
|
|
|
plt.subplot(2, 2, 1, aspect='equal')
|
|
|
|
|
plt.title("Original space")
|
2012-03-04 22:37:30 +08:00
|
|
|
reds = y == 0
|
|
|
|
|
blues = y == 1
|
|
|
|
|
|
2017-06-28 20:56:27 +08:00
|
|
|
plt.scatter(X[reds, 0], X[reds, 1], c="red",
|
|
|
|
|
s=20, edgecolor='k')
|
|
|
|
|
plt.scatter(X[blues, 0], X[blues, 1], c="blue",
|
|
|
|
|
s=20, edgecolor='k')
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xlabel("$x_1$")
|
|
|
|
|
plt.ylabel("$x_2$")
|
2011-04-01 17:50:07 +08:00
|
|
|
|
2012-03-04 22:37:30 +08:00
|
|
|
X1, X2 = np.meshgrid(np.linspace(-1.5, 1.5, 50), np.linspace(-1.5, 1.5, 50))
|
2011-04-01 17:50:07 +08:00
|
|
|
X_grid = np.array([np.ravel(X1), np.ravel(X2)]).T
|
|
|
|
|
# projection on the first principal component (in the phi space)
|
|
|
|
|
Z_grid = kpca.transform(X_grid)[:, 0].reshape(X1.shape)
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.contour(X1, X2, Z_grid, colors='grey', linewidths=1, origin='lower')
|
|
|
|
|
|
|
|
|
|
plt.subplot(2, 2, 2, aspect='equal')
|
2017-06-28 20:56:27 +08:00
|
|
|
plt.scatter(X_pca[reds, 0], X_pca[reds, 1], c="red",
|
|
|
|
|
s=20, edgecolor='k')
|
|
|
|
|
plt.scatter(X_pca[blues, 0], X_pca[blues, 1], c="blue",
|
|
|
|
|
s=20, edgecolor='k')
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.title("Projection by PCA")
|
|
|
|
|
plt.xlabel("1st principal component")
|
|
|
|
|
plt.ylabel("2nd component")
|
|
|
|
|
|
|
|
|
|
plt.subplot(2, 2, 3, aspect='equal')
|
2017-06-28 20:56:27 +08:00
|
|
|
plt.scatter(X_kpca[reds, 0], X_kpca[reds, 1], c="red",
|
|
|
|
|
s=20, edgecolor='k')
|
|
|
|
|
plt.scatter(X_kpca[blues, 0], X_kpca[blues, 1], c="blue",
|
|
|
|
|
s=20, edgecolor='k')
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.title("Projection by KPCA")
|
|
|
|
|
plt.xlabel("1st principal component in space induced by $\phi$")
|
|
|
|
|
plt.ylabel("2nd component")
|
|
|
|
|
|
|
|
|
|
plt.subplot(2, 2, 4, aspect='equal')
|
2017-06-28 20:56:27 +08:00
|
|
|
plt.scatter(X_back[reds, 0], X_back[reds, 1], c="red",
|
|
|
|
|
s=20, edgecolor='k')
|
|
|
|
|
plt.scatter(X_back[blues, 0], X_back[blues, 1], c="blue",
|
|
|
|
|
s=20, edgecolor='k')
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.title("Original space after inverse transform")
|
|
|
|
|
plt.xlabel("$x_1$")
|
|
|
|
|
plt.ylabel("$x_2$")
|
|
|
|
|
|
|
|
|
|
plt.subplots_adjust(0.02, 0.10, 0.98, 0.94, 0.04, 0.35)
|
|
|
|
|
|
|
|
|
|
plt.show()
|