Get startedGet started for free

ROI on ad spend

The return on investment (ROI) for ad spend can be categorized using the four outcomes from a confusion matrix. This quantity is defined as the ratio between the total return and the total cost. If this quantity is greater than 1, it indicates the total return was greater than the total cost and vice versa. In this exercise, you will compute a sample ROI assuming a fixed r, the return on a click per number of impressions, and cost, the cost per number of impressions.

The pandas module is available as pd in your workspace and the sample DataFrame is loaded as df. The arrays y_test (target values of testing set) and y_pred (predicted target values) are available for use. Additionally, DecisionTreeClassifier from sklearn.tree is available.

This exercise is part of the course

Predicting CTR with Machine Learning in Python

View Course

Exercise instructions

  • Compute the confusion matrix and get the four categories via flattening the matrix using .ravel().
  • Calculate the total return (using r) and total cost (using cost) by using quantities from the four categories.
  • Calculate the total ROI.

Hands-on interactive exercise

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

# Compute confusion matrix and get four categories
conf_matrix = ____(y_test, y_pred)
tn, fp, fn, tp = conf_matrix.____

# Calculate total return, total spent, and ROI
r = 0.2
cost = 0.05
total_return = ____ * r
total_cost = (____ + ____) * cost 
roi = ____ / ____
print("Total return: %s, Total cost: %s, ROI: %s" %(
  total_return, total_cost, roi))
Edit and Run Code