X, T = datasets.make_circles(n_samples, noise=sigma, factor=0.5) n_components = 2 plt.figure(figsize=(8, 6)) n_rows, n_cols = 2, 2 marker = 'x' color = T cmap = plt.cm.Paired plt.subplot(n_rows, n_cols, 1) plt.scatter(X[:, 0], X[:, 1], marker=marker, color=T, cmap=cmap) plt.title('Origin') pca = PCA(n_components) Y_pca = pca.fit(X) plt.subplot(n_rows, n_cols, 2) plt.scatter(Y_pca[:, 0], Y_pca[:, 1], marker=marker, color=T, cmap=cmap) plt.title('PCA') sigma_gaussian = 0.2 kpca = KernelPCA(n_components, kernel='gaussian', sigma=sigma_gaussian) Y_kpca = kpca.fit(X) plt.subplot(n_rows, n_cols, 3) plt.scatter(Y_kpca[:, 0], Y_kpca[:, 1], marker=marker, color=T, cmap=cmap) plt.title('KPCA(Gaussian): $\sigma$={0}'.format(sigma_gaussian)) degree = 9 coef0 = 1 kpca = KernelPCA(n_components, kernel='polynomial', degree=degree, coef0=coef0) Y_kpca = kpca.fit(X)
from mlpy.model import PCA from mlpy.util import loadMNISTImages, loadMNISTLabels X = loadMNISTImages('../../mnist/train-images-idx3-ubyte') T = loadMNISTLabels('../../mnist/train-labels-idx1-ubyte') digit3_idx = sp.nonzero(T == 3)[0] digit3_T = T[digit3_idx] digit3_X = X[digit3_idx] n_digit3, n_features = digit3_X.shape n_sqrt_features = int(sp.sqrt(n_features)) n_components = 4 pca = PCA(M=n_components) Y = pca.fit(digit3_X) cov = sp.cov(digit3_X, rowvar=0) eigvals, eigvecs = pca.eigvals, pca.eigvecs digit3_mean = digit3_X.mean(axis=0) # Show the Figures 12.3 on Page 566 of the book n_figrows, n_figcols = 2, n_components + 1 plt.figure(figsize=(10, 4)) plt.subplot(n_figrows, n_figcols, 1) plt.imshow( digit3_mean.reshape((n_sqrt_features, n_sqrt_features)), cmap=plt.cm.Greys_r, interpolation='none') plt.title('Mean')