Analyse par cohorte

Segmentation de la clientèle en Python

Karolis Urbonas

Head of Data Science, Amazon

Carte thermique d'analyse par cohorte

Lignes :

  • Première activité
  • Ici : mois d'acquisition

Colonnes :

  • Temps depuis la première activité
  • Ici : mois depuis l'acquisition

Carte thermique de cohorte

Segmentation de la clientèle en Python

Carte thermique d'analyse par cohorte

Lignes :

  • Première activité
  • Ici : mois d'acquisition

Colonnes :

  • Temps depuis la première activité
  • Ici : mois depuis l'acquisition

Segmentation de la clientèle en Python

Données de vente en ligne

Plus de 0,5 million de transactions d'une boutique de vente en ligne au Royaume‑Uni.

Nous utiliserons un échantillon aléatoire de 20 % de cet ensemble de données tout au long du cours.

VenteEnLigne

Segmentation de la clientèle en Python

5 premières lignes des données

online.head()

5 premières lignes

Segmentation de la clientèle en Python

Attribuer le mois d'acquisition de la cohorte

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

Segmentation de la clientèle en Python

Extraire des valeurs entières des données

Définir une fonction pour extraire les valeurs entières de year, month et day.

Nous l'emploierons tout au long du cours.

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
Segmentation de la clientèle en Python

Attribuer la valeur de décalage temporel

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

décalage-temps-ajouté-top5

Segmentation de la clientèle en Python

Compter les clientes et clients actifs mensuels par cohorte

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)
Segmentation de la clientèle en Python

tableau-pivot-comptes-en-ligne

Segmentation de la clientèle en Python

À vous de créer des cohortes !

Segmentation de la clientèle en Python

Preparing Video For Download...