回到批發資料:探索
在前一個分析中,你發現 k = 2 具有最高的平均輪廓係數(average silhouette width)。在這個練習中,你會以 2 個群集建立並探索一個 k-means 模型,持續分析批發客戶資料。
本練習屬於課程
R 的叢集分析
練習說明
- 使用
kmeans()函式,為customers_spend資料建立名為model_customers的 k-means 模型,並設定centers = 2。 - 從模型
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))