คำนวณ Retention Rate ด้วยตัวเอง
ได้เรียนรู้วิธีสร้างตาราง retention และค่าเฉลี่ยปริมาณสินค้าสำหรับ cohort ที่ได้ลูกค้าใหม่รายเดือนไปแล้ว ถึงเวลาลองสร้าง retention metrics ด้วยตัวเองบ้าง
ชุดข้อมูล online ถูกโหลดมาพร้อมกับ cohort รายเดือนและ cohort index ที่กำหนดไว้จากบทเรียนนี้แล้ว ลองพิมพ์ดูใน Console ได้เลย
นอกจากนี้ เราได้สร้างออบเจกต์ groupby ในชื่อ grouping เป็น DataFrame ด้วยคำสั่งนี้:
grouping = online.groupby(['CohortMonth', 'CohortIndex'])
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การแบ่งกลุ่มลูกค้าด้วย Python
คำแนะนำการฝึกหัด
- เลือกคอลัมน์ customer ID นับจำนวนค่าที่ไม่ซ้ำกัน เก็บผลลัพธ์ไว้เป็น
cohort_dataแล้ว reset index - สร้าง pivot โดยใช้ monthly cohort เป็น index, cohort index เป็น columns และ customer ID เป็น values
- เลือกคอลัมน์แรกและเก็บไว้เป็น
cohort_sizes - หาร cohort count ด้วย cohort sizes ตามแนวแถว
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Count the number of unique values per customer ID
cohort_data = grouping[____].apply(pd.Series.____).reset_index()
# Create a pivot
cohort_counts = cohort_data.____(index=____, columns=____, values=____)
# Select the first column and store it to cohort_sizes
cohort_sizes = cohort_counts.iloc[:,____]
# Divide the cohort count by cohort sizes along the rows
retention = cohort_counts.____(____, axis=____)