始める無料で始める

卸売データの再考:データの探索

前回の分析から、k = 2 が最も高い平均シルエット幅を持つことがわかりました。この演習では、2つのクラスターを持つ k-means モデルを構築し、卸売顧客データの分析を続けましょう。

この演習はコースの一部です

Rによるクラスター分析

コースを見る

演習の手順

  • kmeans() 関数を使って、customers_spend データに対して centers = 2 を指定し、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))
コードを編集して実行