digit3_mean.reshape((n_sqrt_features, n_sqrt_features)), cmap=plt.cm.Greys_r, interpolation='none') plt.imshow(eigvecs[:, i].reshape((n_sqrt_features, n_sqrt_features)), cmap=plt.cm.Greys_r, interpolation='none') plt.title('$\lambda$ = {0:3.1e}'.format(eigvals[i])) # PCA reconstruction digit3 = digit3_X[0] plt.subplot(n_figrows, n_figcols, n_figcols + 1) plt.imshow( digit3.reshape((n_sqrt_features, n_sqrt_features)), cmap=plt.cm.Greys_r, interpolation='none') plt.title('Origin') M = [1, 10, 50, 250] for i, n_components in enumerate(M): plt.subplot(n_figrows, n_figcols, n_figcols + 1 + i + 1) pca = PCA(M=n_components) pca.fit(digit3_X) reconstructed = pca.reconstruct(digit3) plt.imshow( reconstructed.reshape((n_sqrt_features, n_sqrt_features)), cmap=plt.cm.Greys_r, interpolation='none') plt.title('M = {0}'.format(n_components)) plt.show()