PCA 특징의 분산
fish 데이터셋은 6차원입니다. 그렇다면 이 데이터의 고유 차원(intrinsic dimension)은 얼마일까요? 이를 알아보기 위해 PCA 특징들의 분산을 그래프로 그려 보세요. 이전과 마찬가지로 samples는 2차원 배열이며, 각 행이 하나의 물고기를 나타냅니다. 먼저 특징들을 표준화해야 합니다.
이 연습은 강의의 일부입니다
Python으로 배우는 Unsupervised Learning
연습 안내
StandardScaler인스턴스scaler를 만드세요.PCA인스턴스pca를 만드세요.make_pipeline()함수를 사용해scaler와pca를 연결한 파이프라인을 만드세요.pipeline의.fit()메서드를 사용해 물고기 샘플samples에 맞추세요.pca의.n_components_속성을 사용해 사용된 구성 요소 개수를 추출하고, 이를range()함수에 넣어features로 저장하세요.plt.bar()함수를 사용해 설명된 분산을 그리되, x축에는features, y축에는pca.explained_variance_를 사용하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Perform the necessary imports
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
import matplotlib.pyplot as plt
# Create scaler: scaler
scaler = ____
# Create a PCA instance: pca
pca = ____
# Create pipeline: pipeline
pipeline = ____
# Fit the pipeline to 'samples'
____
# Plot the explained variances
features = ____
plt.bar(____, ____)
plt.xlabel('PCA feature')
plt.ylabel('variance')
plt.xticks(features)
plt.show()