코호트 분석

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

상위5-코호트-추가

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을 활용한 고객 세분화

시간 오프셋 값 지정

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

상위5-시간-오프셋-추가

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