CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Machine Learning for Marketing in Python

Afficher le cours

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.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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']
Modifier et exécuter le code