開始使用免費開始

Elbow(Scree)圖

在前面的練習中,你已經計算了 k 從 1 到 10 的各個值所對應的 類內平方和總和(total within-cluster sum of squares)。你可以用折線圖視覺化這個關係,這種圖通常稱為 elbow 圖(或 scree 圖)。

在解讀 elbow 圖時,你會想看到從某個 k 到下一個 k 有明顯的下降,接著斜率逐漸趨緩。當圖的斜率開始趨於平緩時,斜率轉折點之前最後一個 k,通常就是「不錯」的 k 值。

本練習屬於課程

R 的叢集分析

檢視課程

練習說明

  • 延續上一題的工作,使用 elbow_df 中的數值繪製折線圖,呈現 k類內平方和總和(total within-cluster sum of squares) 的關係。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Use map_dbl to run many models with varying value of k (centers)
tot_withinss <- map_dbl(1:10,  function(k){
  model <- kmeans(x = lineup, centers = k)
  model$tot.withinss
})

# Generate a data frame containing both k and tot_withinss
elbow_df <- data.frame(
  k = 1:10,
  tot_withinss = tot_withinss
)

# Plot the elbow plot
ggplot(___, aes(x = ___, y = ___)) +
  geom_line() +
  scale_x_continuous(breaks = 1:10)
編輯並執行程式碼