PCA doesn't learn parts
Unlike NMF, PCA doesn't learn the parts of things. Its components do not correspond to topics (in the case of documents) or to parts of images, when trained on images. Verify this for yourself by inspecting the components of a PCA model fit to the dataset of LED digit images from the previous exercise. The images are available as a 2D array samples
. Also available is a modified version of the show_as_image()
function which colors a pixel red if the value is negative.
After submitting the answer, notice that the components of PCA do not represent meaningful parts of images of LED digits!
This exercise is part of the course
Unsupervised Learning in Python
Exercise instructions
- Import
PCA
fromsklearn.decomposition
. - Create a
PCA
instance calledmodel
with7
components. - Apply the
.fit_transform()
method ofmodel
tosamples
. Assign the result tofeatures
. - To each component of the model (accessed via
model.components_
), apply theshow_as_image()
function to that component inside the loop.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import PCA
____
# Create a PCA instance: model
model = ____
# Apply fit_transform to samples: features
features = ____
# Call show_as_image on each component
for component in ____:
____