ComenzarEmpieza gratis

Build features

You are now fully equipped to build recency, frequency, monetary value and other customer level features for your regression model. Feature engineering is the most important step in the machine learning process. In this exercise you will create five customer-level features that you will then use in predicting next month's customer transactions. These features capture highly predictive customer behavior patterns.

The pandas and numpy libraries have been loaded as pd as np respectively. The online_X dataset has been imported for you. The datetime object NOW depicting the snapshot date you will use to calculate recency has been created for you.

Este ejercicio forma parte del curso

Machine Learning for Marketing in Python

Ver curso

Instrucciones del ejercicio

  • Calculate recency by subtracting the current date from the latest InvoiceDate.
  • Calculate frequency by counting the unique number of invoices.
  • Calculate monetary value by summing all spend values.
  • Calculate average and total quantity.

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

# Define the snapshot date
NOW = dt.datetime(2011,11,1)

# Calculate recency by subtracting current date from the latest InvoiceDate
features = online_X.___('CustomerID').agg({
  'InvoiceDate': lambda x: (NOW - x.max()).days,
  # Calculate frequency by counting unique number of invoices
  'InvoiceNo': pd.Series.___,
  # Calculate monetary value by summing all spend values
  'TotalSum': np.___,
  # Calculate average and total quantity
  'Quantity': ['___', 'sum']}).reset_index()

# Rename the columns
features.columns = ['CustomerID', 'recency', 'frequency', 'monetary', 'quantity_avg', 'quantity_total']
Editar y ejecutar código