เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

กลับมาดูข้อมูลขายส่ง: การสำรวจข้อมูล

จากการวิเคราะห์ก่อนหน้า พบว่า 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 frame customers_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))
แก้ไขและรันโค้ด