Analisi delle coorti

Customer Segmentation in Python

Karolis Urbonas

Head of Data Science, Amazon

Heatmap analisi coorti

Righe:

  • Prima attività
  • Qui: mese di acquisizione

Colonne:

  • Tempo dalla prima attività
  • Qui: mesi dall’acquisizione

Mappa di calore coorti

Customer Segmentation in Python

Heatmap analisi coorti

Righe:

  • Prima attività
  • Qui: mese di acquisizione

Colonne:

  • Tempo dalla prima attività
  • Qui: mesi dall’acquisizione

Customer Segmentation in Python

Dati di retail online

Oltre 500.000 transazioni di un negozio online nel Regno Unito.

Useremo un campione casuale del 20% di questo dataset per tutto il corso.

OnlineRetail

Customer Segmentation in Python

Prime 5 righe dei dati

online.head()

Prime 5 righe

Customer Segmentation in Python

Assegna mese di acquisizione alla coorte

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

prime5-coorte-aggiunta

Customer Segmentation in Python

Estrai valori interi dalla data

Definisci una funzione per estrarre i valori interi di year, month e day.

La useremo per tutto il corso.

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
Customer Segmentation in Python

Assegna valore di scostamento temporale

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

prime5-scostamento-temporale

Customer Segmentation in Python

Conteggia i clienti attivi mensili per coorte

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)
Customer Segmentation in Python

online-pivot-counts

Customer Segmentation in Python

Ora tocca a te creare alcune coorti!

Customer Segmentation in Python

Preparing Video For Download...