In [ ]:
from sklearn.datasets import load_digits
digits = load_digits()
import matplotlib.pyplot as plt
%matplotlib inline

Dimensionality reduction and manifold learning

In [ ]:
from sklearn.decomposition import PCA
In [ ]:
pca = PCA(n_components=2)
In [ ]:
pca.fit(digits.data)
In [ ]:
digits_pca = pca.transform(digits.data)
In [ ]:
digits_pca.shape
In [ ]:
plt.scatter(digits_pca[:, 0], digits_pca[:, 1], c=digits.target)
In [ ]:
plt.matshow(pca.mean_.reshape(8, 8))
plt.matshow(pca.components_[0].reshape(8, 8))
plt.matshow(pca.components_[1].reshape(8, 8))

Manifold Learning

In [ ]:
from sklearn.manifold import SpectralEmbedding
In [ ]:
se = SpectralEmbedding()
digits_se = se.fit_transform(digits.data)
In [ ]:
plt.scatter(digits_se[:, 0], digits_se[:, 1], c=digits.target)

Tasks

  1. Compare the projection to the principal components to projecting to two rows of the original data.
  2. Play with the n_neighbors parameter of Spectral Embedding. How doe that change the outcome?
  3. Extract more components from the digits data using PCA. How can you visualize them?
In [ ]: