分群分析

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-已添加分群月

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-已添加时间偏移

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...