回到批发数据:探索
根据之前的分析,您发现 k = 2 的平均轮廓系数最高。本练习中,您将继续分析批发客户数据,构建并探索一个具有 2 个聚类的 k-means 模型。
本练习是课程的一部分
R 中的聚类分析
练习说明
- 使用
kmeans()函数并设定centers = 2,为customers_spend数据构建名为model_customers的 k-means 模型。 - 从模型中提取聚类分配向量
model_customers$cluster,并将其保存到变量clust_customers。 - 将聚类分配作为列
cluster追加到数据框customers_spend,并将结果保存为新的数据框segment_customers。 - 使用
count()计算每个聚类的大小。
交互式实操练习
通过完成这段示例代码来试试这个练习。
set.seed(42)
# Build a k-means model for the customers_spend with a k of 2
model_customers <- ___
# Extract the vector of cluster assignments from the model
clust_customers <- ___
# Build the segment_customers data frame
segment_customers <- mutate(___, cluster = ___)
# Calculate the size of each cluster
count(___, ___)
# Calculate the mean for each category
segment_customers %>%
group_by(cluster) %>%
summarise_all(list(mean))