กลับมาดูข้อมูลขายส่ง: การสำรวจข้อมูล
จากการวิเคราะห์ก่อนหน้า พบว่า k = 2 มีค่า average silhouette width สูงสุด ในแบบฝึกหัดนี้จะวิเคราะห์ข้อมูลลูกค้าขายส่งต่อ โดยสร้างและสำรวจโมเดล k-means ที่มี 2 คลัสเตอร์
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การวิเคราะห์กลุ่มข้อมูลใน R
คำแนะนำการฝึกหัด
- สร้างโมเดล k-means ชื่อ
model_customersสำหรับข้อมูลcustomers_spendโดยใช้ฟังก์ชันkmeans()พร้อมกำหนดcenters = 2 - ดึงเวกเตอร์การกำหนดคลัสเตอร์จากโมเดล
model_customers$clusterแล้วเก็บไว้ในตัวแปรclust_customers - เพิ่มการกำหนดคลัสเตอร์เป็นคอลัมน์
clusterใน data framecustomers_spendและบันทึกผลลัพธ์ไว้ใน data frame ใหม่ชื่อ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))