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)