1. Learn
  2. /
  3. Courses
  4. /
  5. Unsupervised Learning in Python

Exercise

NMF learns the parts of images

Now use what you've learned about NMF to decompose the digits dataset. You are again given the digit images as a 2D array samples. This time, you are also provided with a function show_as_image() that displays the image encoded by any 1D array:

def show_as_image(sample):
    bitmap = sample.reshape((13, 8))
    plt.figure()
    plt.imshow(bitmap, cmap='gray', interpolation='nearest')
    plt.colorbar()
    plt.show()

After you are done, take a moment to look through the plots and notice how NMF has expressed the digit as a sum of the components!

Instructions

100 XP
  • Import NMF from sklearn.decomposition.
  • Create an NMF instance called model with 7 components. (7 is the number of cells in an LED display).
  • Apply the .fit_transform() method of model to samples. Assign the result to features.
  • To each component of the model (accessed via model.components_), apply the show_as_image() function to that component inside the loop.
  • Assign the row 0 of features to digit_features.
  • Print digit_features.