NMF로 이미지의 부분을 학습하기
이제 NMF에 대해 배운 내용을 활용해 digits 데이터셋을 분해해 보세요. 숫자 이미지는 다시 2차원 배열 samples로 제공됩니다. 이번에는 1차원 배열로 인코딩된 이미지를 표시해 주는 show_as_image() 함수도 함께 제공됩니다:
def show_as_image(sample):
bitmap = sample.reshape((13, 8))
plt.figure()
plt.imshow(bitmap, cmap='gray', interpolation='nearest')
plt.colorbar()
plt.show()
완료한 뒤에는 플롯을 살펴보면서, NMF가 숫자를 여러 성분의 합으로 어떻게 표현하는지 확인해 보세요!
이 연습은 강의의 일부입니다
Python으로 배우는 Unsupervised Learning
연습 안내
sklearn.decomposition에서NMF를 가져오세요.- 성분을
7개로 하는NMF인스턴스를model이라는 이름으로 만드세요. (7은 LED 디스플레이의 셀 개수입니다.) model의.fit_transform()메서드를samples에 적용하세요. 결과를features에 할당하세요.- 모델의 각 성분(
model.components_로 접근)에 대해, 루프 안에서 해당 성분에show_as_image()함수를 적용하세요. features의 0번째 행을digit_features에 할당하세요.digit_features를 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Import NMF
____
# Create an NMF model: model
model = ____
# Apply fit_transform to samples: features
features = ____
# Call show_as_image on each component
for component in model.components_:
____
# Select the 0th row of features: digit_features
digit_features = ____
# Print digit_features
print(digit_features)