Explore the LED digits dataset
In the following exercises, you'll use NMF to decompose grayscale images into their commonly occurring patterns. Firstly, explore the image dataset and see how it is encoded as an array. You are given 100 images as a 2D array samples, where each row represents a single 13x8 image. The images in your dataset are pictures of a LED digital display.
This exercise is part of the course
Unsupervised Learning in Python
Exercise instructions
- Import
matplotlib.pyplotasplt. - Select row
0ofsamplesand assign the result todigit. For example, to select column2of an arraya, you could usea[:,2]. Remember that sincesamplesis a NumPy array, you can't use the.loc[]oriloc[]accessors to select specific rows or columns. - Print
digit. This has been done for you. Notice that it is a 1D array of 0s and 1s. - Use the
.reshape()method ofdigitto get a 2D array with shape(13, 8). Assign the result tobitmap. - Print
bitmap, and notice that the 1s show the digit 7! - Use the
plt.imshow()function to displaybitmapas an image.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import pyplot
from matplotlib import pyplot as plt
# Select the 0th row: digit
digit = ____
# Print digit
print(digit)
# Reshape digit to a 13x8 array: bitmap
bitmap = ____
# Print bitmap
print(bitmap)
# Use plt.imshow to display bitmap
plt.____(____, cmap='gray', interpolation='nearest')
plt.colorbar()
plt.show()