Marketing ROI Prediction with Lasso Regression in ML

FREE Online Courses: Transform Your Career – Enroll for Free!

Marketing leaders juggle limited budgets across TV, radio, social, and print. They want to know, before spending, how much revenue a proposed media mix will return per dollar invested. Using historic multi‑channel advertising data, we will build a Lasso‑regularised linear model that:

  • Forecasts the Return on Investment (ROI) for a campaign, defined here as
    ROI=Predicted Sales−Total SpendTotal Spend\text{ROI} = \frac{\text{Predicted Sales} – \text{Total Spend}}{\text{Total Spend}}
    (expressed as a decimal; 0.30 = 30 % return).
  • Shrinks weak predictors to zero, revealing which channels and spend levels truly move the revenue needle.

Lasso’s ℓ¹ penalty keeps the model sparse and interpretable—essential for CMOs who need actionable insights, not black‑box scores.

Libraries Required

Purpose Python Package
Data loading & wrangling pandas, numpy
Visualisation matplotlib, seaborn
ML workflow scikit‑learnColumnTransformer, OneHotEncoder, StandardScaler, Pipeline, Lasso, GridSearchCV
Evaluation mean_squared_error, r2_score

Dataset Link

Advertising Sales Dataset

Step-by-Step Code Implementation

1. Import Libraries

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.linear_model import Lasso
from sklearn.metrics import mean_squared_error, r2_score

2. Download and load the dataset

Classic advertising study of 200 campaigns with spend across TV, radio, newspaper and resulting sales.

# One‑time terminal command (requires Kaggle API token):
# kaggle datasets download -d yasserh/advertising-sales-dataset -p data --unzip

ads = pd.read_csv("data/Advertising.csv")     # 200 rows, 4 columns

3. Engineer ROI target

ROI is unit‑less, instantly understandable by finance teams. Negative ROI flags unprofitable media mixes.

ads['Total_Spend'] = ads[['TV', 'Radio', 'Newspaper']].sum(axis=1)
ads['ROI'] = (ads['Sales'] - ads['Total_Spend']) / ads['Total_Spend']
y = ads['ROI']

4. Prepare feature matrix

Absolute spends capture scale; percentage mix features let Lasso learn diminishing‑returns interactions (e.g., “70 % of budget on TV is too much”).

# Absolute spends
ads['TV_pct']   = ads['TV']   / ads['Total_Spend']
ads['Radio_pct'] = ads['Radio'] / ads['Total_Spend']
ads['News_pct']  = ads['Newspaper'] / ads['Total_Spend']

X = ads[['TV', 'Radio', 'Newspaper', 'TV_pct', 'Radio_pct', 'News_pct']]
num_cols = X.columns.tolist()

5. Build pre‑processing & model pipeline

Only scaling is needed because all predictors are numeric; StandardScaler ensures Lasso’s penalty treats each on equal footing.

preprocess = ColumnTransformer(
    [('num', StandardScaler(), num_cols)],
    remainder='drop'
)

pipe = Pipeline([
    ('prep', preprocess),
    ('model', Lasso(max_iter=10_000, random_state=42))
])

6. Train/test split

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42)

7. Hyper‑parameter search

Log‑spaced α sweep (0.001–10) balances sparsity and fit; five‑fold CV settles on the sweet spot.

param_grid = {'model__alpha': np.logspace(-3, 1, 25)}   # 0.001 → 10
search = GridSearchCV(pipe, param_grid,
                      cv=5, scoring='neg_root_mean_squared_error')
search.fit(X_train, y_train)

print("Best α:", search.best_params_['model__alpha'])

8. Evaluate on the hold‑out set

RMSE shows average error in ROI units (e.g., 0.05 = 5 percentage points), while R2R^2 indicates explanatory power.

y_pred = search.predict(X_test)
rmse = mean_squared_error(y_test, y_pred, squared=False)
r2   = r2_score(y_test, y_pred)

print(f"Test RMSE: {rmse:.3f} ROI points | R²: {r2:.3f}")

9. Interpret coefficients

Coefficient chart surfaces indicate which spend or mix ratios most influence ROI—valuable guidance for budget reallocation.

coefs = search.best_estimator_.named_steps['model'].coef_
imp   = pd.Series(coefs, index=num_cols).sort_values(key=abs, ascending=False)

plt.figure(figsize=(8,5))
imp.plot(kind='barh')
plt.gca().invert_yaxis()
plt.title('Top Drivers of Marketing ROI (Lasso Coefficients)')
plt.xlabel('Coefficient (Δ ROI)')
plt.show()

Summary

With fewer than 120 lines of Python, we created an interpretable, cross‑validated Lasso model that:

  • Forecasts ROI before an advertising dollar is spent.
  • Ranks cost‑to‑return levers, showing exactly which channel shifts improve profitability.
  • Requires minimal upkeep—a fresh data file and one .fit() retrains the entire pipeline.

Marketers can now test hypothetical budgets in a notebook, get instant ROI projections, and invest with data‑driven confidence rather than gut feel.

If you are Happy with ProjectGurukul, do not forget to make us happy with your positive feedback on Google | Facebook

ProjectGurukul Team

ProjectGurukul Team specializes in creating project-based learning resources for programming, Java, Python, Android, AI, Webdevelopment and machine learning. Our mission is to help learners build practical skills through engaging, hands-on projects. We also offer free major and minor projects with source code for engineering students

Leave a Reply

Your email address will not be published. Required fields are marked *