コホート分析

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

top5-cohort-added

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

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で学ぶカスタマーセグメンテーション

コホートを作ってみましょう!

Pythonで学ぶカスタマーセグメンテーション

Preparing Video For Download...