การวิเคราะห์กลุ่มลูกค้า (Cohort Analysis)

การแบ่งกลุ่มลูกค้าด้วย Python

Karolis Urbonas

Head of Data Science, Amazon

Heatmap การวิเคราะห์กลุ่มลูกค้า

แถว:

  • กิจกรรมแรก
  • ที่นี่ — เดือนที่ได้ลูกค้า

คอลัมน์:

  • เวลาตั้งแต่กิจกรรมแรก
  • ที่นี่ — จำนวนเดือนนับตั้งแต่ได้ลูกค้า

Cohort heatmap

การแบ่งกลุ่มลูกค้าด้วย Python

Heatmap การวิเคราะห์กลุ่มลูกค้า

แถว:

  • กิจกรรมแรก
  • ที่นี่ — เดือนที่ได้ลูกค้า

คอลัมน์:

  • เวลาตั้งแต่กิจกรรมแรก
  • ที่นี่ — จำนวนเดือนนับตั้งแต่ได้ลูกค้า

การแบ่งกลุ่มลูกค้าด้วย Python

ข้อมูลร้านค้าออนไลน์

ข้อมูลธุรกรรมกว่า 500,000 รายการจากร้านค้าออนไลน์ในสหราชอาณาจักร

ตลอดคอร์สนี้จะใช้ข้อมูลสุ่มตัวอย่าง 20% ของชุดข้อมูลนี้

OnlineRetail

การแบ่งกลุ่มลูกค้าด้วย Python

5 แถวแรกของข้อมูล

online.head()

5 แถวแรกของข้อมูล

การแบ่งกลุ่มลูกค้าด้วย Python

กำหนดเดือนของกลุ่มลูกค้าตามเดือนที่ได้ลูกค้า

def get_month(x): return dt.datetime(x.year, x.month, 1)

online['InvoiceMonth'] = online['InvoiceDate'].apply(get_month)
grouping = online.groupby('CustomerID')['InvoiceMonth']
online['CohortMonth'] = grouping.transform('min')
online.head()

top5-cohort-added

การแบ่งกลุ่มลูกค้าด้วย Python

ดึงค่าจำนวนเต็มจากข้อมูลวันที่

กำหนดฟังก์ชันเพื่อดึงค่าจำนวนเต็มของ year, month และ day

ฟังก์ชันนี้จะใช้ตลอดคอร์ส

def get_date_int(df, column):
    year = df[column].dt.year
    month = df[column].dt.month
    day = df[column].dt.day
    return year, month, day
การแบ่งกลุ่มลูกค้าด้วย Python

กำหนดค่า Time Offset

invoice_year, invoice_month, _ = get_date_int(online, 'InvoiceMonth') 
cohort_year, cohort_month, _ = get_date_int(online, 'CohortMonth')

years_diff = invoice_year - cohort_year months_diff = invoice_month - cohort_month
online['CohortIndex'] = years_diff * 12 + months_diff + 1 online.head()

top5-time-offset-added

การแบ่งกลุ่มลูกค้าด้วย Python

นับจำนวนลูกค้าที่ใช้งานรายเดือนในแต่ละกลุ่ม

grouping = online.groupby(['CohortMonth', 'CohortIndex'])

cohort_data = grouping['CustomerID'].apply(pd.Series.nunique)
cohort_data = cohort_data.reset_index()
cohort_counts = cohort_data.pivot(index='CohortMonth', columns='CohortIndex', values='CustomerID')
print(cohort_counts)
การแบ่งกลุ่มลูกค้าด้วย Python

online-pivot-counts

การแบ่งกลุ่มลูกค้าด้วย Python

มาฝึกสร้าง Cohort กันเถอะ!

การแบ่งกลุ่มลูกค้าด้วย Python

Preparing Video For Download...