곡물은 몇 개의 클러스터로 나뉠까요?
영상에서 k-means 관성(inertia) 그래프를 사용해 데이터셋에 적합한 클러스터 수를 선택하는 방법을 배웠어요. 여기에는 곡물 샘플의 측정값(면적, 둘레, 길이 등)이 담긴 배열 samples가 주어져 있습니다. 이 경우 적절한 클러스터 수는 얼마일까요?
KMeans와 PyPlot(plt)은 이미 임포트되어 있습니다.
이 데이터셋은 UCI Machine Learning Repository에서 가져왔습니다.
이 연습은 강의의 일부입니다
Python으로 배우는 Unsupervised Learning
연습 안내
- 주어진 각
k값에 대해 다음을 수행하세요: k개의 클러스터를 갖는KMeans인스턴스model을 생성하세요.- 곡물 데이터
samples에 모델을 학습(fit)시키세요. model의inertia_속성 값을 리스트inertias에 추가하세요.ks와inertias를 플로팅하는 코드는 미리 작성되어 있으니, 제출을 눌러 그래프를 확인하세요!
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
ks = range(1, 6)
inertias = []
for k in ks:
# Create a KMeans instance with k clusters: model
____
# Fit model to samples
____
# Append the inertia to the list of inertias
____
# Plot ks vs inertias
plt.plot(ks, inertias, '-o')
plt.xlabel('number of clusters, k')
plt.ylabel('inertia')
plt.xticks(ks)
plt.show()