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
|
|
|
"""
|
|
|
|
|
print __doc__
|
|
|
|
|
|
|
|
|
|
# Authors: Mathieu Blondel
|
|
|
|
|
# License: BSD
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
import pylab as pl
|
|
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.decomposition import PCA, KernelPCA
|
2011-04-01 17:50:07 +08:00
|
|
|
|
|
|
|
|
np.random.seed(0)
|
|
|
|
|
|
2011-04-03 07:47:28 +08:00
|
|
|
|
2011-04-01 17:50:07 +08:00
|
|
|
def genenerate_rings(n_samples=200):
|
|
|
|
|
x_red = np.random.random((n_samples,)) * 2 - 1
|
|
|
|
|
signs_red = np.sign(np.random.random(x_red.shape) - 0.5)
|
|
|
|
|
y_red = np.sqrt(np.abs(x_red ** 2 - 1)) * signs_red
|
|
|
|
|
|
|
|
|
|
x_blue = np.random.random((n_samples,)) * 6 - 3
|
|
|
|
|
signs_blue = np.sign(np.random.random(x_blue.shape) - 0.5)
|
|
|
|
|
y_blue = np.sqrt(np.abs(x_blue ** 2 - 9)) * signs_blue
|
|
|
|
|
|
|
|
|
|
return np.hstack(([x_red, y_red], [x_blue, y_blue])).T
|
|
|
|
|
|
2011-04-03 07:47:28 +08:00
|
|
|
|
2011-04-01 17:50:07 +08:00
|
|
|
def generate_clusters(n_samples=200):
|
|
|
|
|
mean1 = np.array([0, 2])
|
|
|
|
|
mean2 = np.array([2, 0])
|
|
|
|
|
cov = np.array([[2.0, 1.0], [1.0, 2.0]])
|
|
|
|
|
X_red = np.random.multivariate_normal(mean1, cov, n_samples)
|
|
|
|
|
X_blue = np.random.multivariate_normal(mean2, cov, n_samples)
|
|
|
|
|
return np.vstack((X_red, X_blue))
|
|
|
|
|
|
|
|
|
|
X = genenerate_rings()
|
|
|
|
|
#X = generate_clusters()
|
|
|
|
|
|
2011-06-03 16:14:45 +08:00
|
|
|
kpca = KernelPCA(kernel="rbf", fit_inverse_transform=True, gamma=0.5)
|
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
|
|
|
|
|
|
|
|
|
|
pl.figure()
|
|
|
|
|
pl.subplot(2, 2, 1, aspect='equal')
|
2011-04-01 17:50:07 +08:00
|
|
|
pl.title("Original space")
|
|
|
|
|
pl.plot(X[:200, 0], X[:200, 1], "ro")
|
|
|
|
|
pl.plot(X[200:, 0], X[200:, 1], "bo")
|
|
|
|
|
pl.xlabel("$x_1$")
|
|
|
|
|
pl.ylabel("$x_2$")
|
|
|
|
|
|
2011-04-03 07:47:28 +08:00
|
|
|
X1, X2 = np.meshgrid(np.linspace(-6, 6, 50), np.linspace(-6, 6, 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)
|
|
|
|
|
pl.contour(X1, X2, Z_grid, colors='grey', linewidths=1, origin='lower')
|
|
|
|
|
|
2011-04-01 20:47:31 +08:00
|
|
|
pl.subplot(2, 2, 2, aspect='equal')
|
2011-04-01 17:50:07 +08:00
|
|
|
pl.plot(X_kpca[:200, 0], X_pca[:200, 1], "ro")
|
|
|
|
|
pl.plot(X_pca[200:, 0], X_pca[200:, 1], "bo")
|
|
|
|
|
pl.title("Projection by PCA")
|
|
|
|
|
pl.xlabel("1st principal component")
|
|
|
|
|
pl.ylabel("2nd component")
|
|
|
|
|
|
2011-04-01 20:47:31 +08:00
|
|
|
pl.subplot(2, 2, 3, aspect='equal')
|
2011-04-01 17:50:07 +08:00
|
|
|
pl.plot(X_kpca[:200, 0], X_kpca[:200, 1], "ro")
|
|
|
|
|
pl.plot(X_kpca[200:, 0], X_kpca[200:, 1], "bo")
|
|
|
|
|
pl.title("Projection by KPCA")
|
|
|
|
|
pl.xlabel("1st principal component in space induced by $\phi$")
|
|
|
|
|
pl.ylabel("2nd component")
|
|
|
|
|
|
2011-04-01 20:47:31 +08:00
|
|
|
pl.subplot(2, 2, 4, aspect='equal')
|
2011-04-01 17:50:07 +08:00
|
|
|
pl.plot(X_back[:200, 0], X_back[:200, 1], "ro")
|
|
|
|
|
pl.plot(X_back[200:, 0], X_back[200:, 1], "bo")
|
|
|
|
|
pl.title("Original space after inverse transform")
|
|
|
|
|
pl.xlabel("$x_1$")
|
|
|
|
|
pl.ylabel("$x_2$")
|
|
|
|
|
|
2011-04-03 07:47:28 +08:00
|
|
|
pl.subplots_adjust(0.02, 0.10, 0.98, 0.94, 0.04, 0.35)
|
2011-04-01 17:50:07 +08:00
|
|
|
|
|
|
|
|
pl.show()
|