Get Started

Define target variable

Here, you'll build a pandas pivot table with customers as rows, invoice months as columns, and number of invoice counts as values. You will use the last month's value as the target variable. The remaining variables can be used as the so-called lagged features in the model. You will not use them, but are highly encouraged to check if adding these variables will improve your model performance beyond what you'll see in the upcoming exercises.

The pandas and numpy libraries have been loaded as pd as np respectively. The online dataset has been imported for you.

This is a part of the course

“Machine Learning for Marketing in Python”

View Course

Exercise instructions

  • Build a pivot table using the pivot_table() function counting invoices.
  • Store November 2011 sales data column name as a list.
  • Store the target value as Y.

Hands-on interactive exercise

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

# Build a pivot table counting invoices for each customer monthly
cust_month_tx = pd.___(data=online, values='___',
                               index=['___'], columns=['___'],
                               aggfunc=pd.Series.nunique, fill_value=0)

# Store November 2011 data column name as a list
target = ['2011-___']

# Store target value as `Y`
Y = cust_month_tx[___]
Edit and Run Code

This exercise is part of the course

Machine Learning for Marketing in Python

IntermediateSkill Level
4.0+
3 reviews

From customer lifetime value, predicting churn to segmentation - learn and implement Machine Learning use cases for Marketing in Python.

In this chapter, you will learn the basics of Customer Lifetime Value (CLV) and its different calculation methodologies. You will harness this knowledge to build customer level purchase features to predict next month's transactions using linear regression.

Exercise 1: Customer Lifetime Value (CLV) basicsExercise 2: Build retention and churn tablesExercise 3: Explore retention and churnExercise 4: Calculating and projecting CLVExercise 5: Calculate basic CLVExercise 6: Calculate granular CLVExercise 7: Calculate traditional CLVExercise 8: Data preparation for purchase predictionExercise 9: Build featuresExercise 10: Define target variable
Exercise 11: Split data to training and testingExercise 12: Predicting customer transactionsExercise 13: Predict next month transactionsExercise 14: Measure model fitExercise 15: Explore model coefficients

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free