Get startedGet started for free

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.

This exercise is part of the course

Machine Learning for Marketing in Python

View Course

Exercise instructions

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

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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']
Edit and Run Code