K-means: 엘보 분석
이전 연습 문제에서는 덴드로그램을 활용해 3개의 클러스터를 제안했어요. 이번 연습 문제에서는 k-means 엘보 플롯을 이용해 "가장 적절한" 클러스터 개수를 제안해 봅시다.
이 연습은 강의의 일부입니다
R로 배우는 군집 분석
연습 안내
map_dbl()을 사용해 k 값을 1부터 10까지 바꿔 가며oes데이터로kmeans()를 실행하고, 각 모델에서 클러스터 내 제곱합의 전체 합 값(model$tot.withinss)을 추출하세요. 결과 벡터는tot_withinss로 저장하세요.- k 값과 클러스터 내 제곱합의 전체 합 벡터를 담은 새 데이터 프레임
elbow_df를 만드세요. elbow_df의 값을 사용해 k와 클러스터 내 제곱합의 전체 합 간의 관계를 나타내는 선 그래프를 그리세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Use map_dbl to run many models with varying value of k (centers)
tot_withinss <- map_dbl(1:10, function(k){
model <- kmeans(x = ___, centers = ___)
model$tot.withinss
})
# Generate a data frame containing both k and tot_withinss
elbow_df <- data.frame(
k = ___,
tot_withinss = ___
)
# Plot the elbow plot
ggplot(elbow_df, aes(x = ___, y = ___)) +
geom_line() +
scale_x_continuous(breaks = 1:10)