同群分析

Python 的客群分群

Karolis Urbonas

Head of Data Science, Amazon

同群分析熱圖

列:

  • 首次活動
  • 這裡-取得月份

欄:

  • 距首次活動時間
  • 這裡-距取得後的月數

同群熱圖

Python 的客群分群

同群分析熱圖

列:

  • 首次活動
  • 這裡-取得月份

欄:

  • 距首次活動時間
  • 這裡-距取得後的月數

Python 的客群分群

線上零售資料

超過 50 萬筆來自英國線上零售商店的交易。

本課程全程會使用此資料集隨機抽樣的 20% 子集。

線上零售

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()

前五列-新增欄位

Python 的客群分群

從日期擷取整數值

定義函式以擷取 yearmonthday 的整數值。

你會在本課程全程使用它。

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 的客群分群

指派時間位移值

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()

前五列-新增時間位移

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 的客群分群

同群樞紐-活躍數

Python 的客群分群

換你來建立同群!

Python 的客群分群

Preparing Video For Download...