Cohortanalyse

Klantsegmentatie in Python

Karolis Urbonas

Head of Data Science, Amazon

Heatmap van cohortanalyse

Rijen:

  • Eerste activiteit
  • Hier: maand van acquisitie

Kolommen:

  • Tijd sinds eerste activiteit
  • Hier: maanden sinds acquisitie

Cohort-heatmap

Klantsegmentatie in Python

Heatmap van cohortanalyse

Rijen:

  • Eerste activiteit
  • Hier: maand van acquisitie

Kolommen:

  • Tijd sinds eerste activiteit
  • Hier: maanden sinds acquisitie

Klantsegmentatie in Python

Online-retaildata

Meer dan 0,5 miljoen transacties van een Britse online retailer.

We gebruiken in de cursus een aselecte 20%-steekproef van deze dataset.

OnlineRetail

Klantsegmentatie in Python

Top 5 rijen van de data

online.head()

Top 5 rijen

Klantsegmentatie in Python

Acquisitiemaand-cohort toewijzen

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

Klantsegmentatie in Python

Gehele waarden uit data halen

Definieer een functie om gehele waarden voor year, month en day te halen.

We gebruiken die door de hele cursus.

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
Klantsegmentatie in Python

Tijdoffset berekenen

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-tijdoffset-toegevoegd

Klantsegmentatie in Python

Maandelijkse actieve klanten per cohort tellen

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)
Klantsegmentatie in Python

online-pivot-aantallen

Klantsegmentatie in Python

Jij bent aan de beurt: bouw cohorts!

Klantsegmentatie in Python

Preparing Video For Download...