BaşlayınÜcretsiz Başlayın

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.

Bu egzersiz

Machine Learning for Marketing in Python

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

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

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

# 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']
Kodu Düzenle ve Çalıştır