卸売データの再考:データの探索
前回の分析から、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))