開始使用免費開始

回到批發資料:探索

在前一個分析中,你發現 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))
編輯並執行程式碼