PCA로 곡물 측정치의 상관성 제거하기
이전 연습 문제에서 곡물의 너비와 길이 측정값에 상관관계가 있음을 확인했어요. 이제 PCA를 사용해 이러한 측정값의 상관성을 제거한 다음, 상관성이 제거된 점들을 시각화하고 Pearson 상관계수를 계산해 보겠습니다.
이 연습은 강의의 일부입니다
Python으로 배우는 Unsupervised Learning
연습 안내
sklearn.decomposition에서PCA를 가져오세요.model이라는 이름의PCA인스턴스를 생성하세요.model의.fit_transform()메서드를 사용해 PCA 변환을grains에 적용하고, 결과를pca_features에 할당하세요.- 이어지는 코드는
pca_features의 처음 두 열을 추출·시각화하고 Pearson 상관계수를 계산하도록 미리 작성되어 있으니, 결과를 확인하려면 Submit Answer를 눌러 주세요!
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Import PCA
____
# Create PCA instance: model
model = ____
# Apply the fit_transform method of model to grains: pca_features
pca_features = ____
# Assign 0th column of pca_features: xs
xs = pca_features[:,0]
# Assign 1st column of pca_features: ys
ys = pca_features[:,1]
# Scatter plot xs vs ys
plt.scatter(xs, ys)
plt.axis('equal')
plt.show()
# Calculate the Pearson correlation of xs and ys
correlation, pvalue = pearsonr(xs, ys)
# Display the correlation
print(correlation)