자연에서의 상관된 데이터
곡물 샘플의 너비와 길이를 담은 배열 grains가 주어졌습니다. 너비와 길이에 상관이 있을 것으로 예상하신다면, 이를 확인하기 위해 너비 대비 길이의 산점도를 만들고 피어슨 상관계수를 계산하세요.
이 연습은 강의의 일부입니다
Python으로 배우는 Unsupervised Learning
연습 안내
- 다음을 임포트하세요:
matplotlib.pyplot을plt로.scipy.stats에서pearsonr.
grains의 열0을width에, 열1을length에 할당하세요.- x축에는
width, y축에는length를 두고 산점도를 그리세요. pearsonr()함수를 사용해width와length의 피어슨 상관계수를 계산하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Perform the necessary imports
____
____
# Assign the 0th column of grains: width
width = ____
# Assign the 1st column of grains: length
length = ____
# Scatter plot width vs length
plt.scatter(____, ____)
plt.axis('equal')
plt.show()
# Calculate the Pearson correlation
correlation, pvalue = ____
# Display the correlation
print(correlation)