Module 11 · Logistic regression · Lesson 48 of 66
Project: who clicks the ad?
Predict whether a website visitor will click an ad from their age, income and habits.
In this lesson · about 25 minutes
- Explore a new data set visually
- Train and evaluate a logistic regression model on your own
A (made-up) advertising data set: one row per website visitor, with whether they clicked an ad. Columns:
Daily Time Spent on Sitein minutes, andDaily Internet Usagein minutesAge,Area Income(average income where they live) andMale(1 or 0)Ad Topic Line,City,CountryandTimestampClicked on Ad: 1 or 0, what we'll predict
Exercise
1. Ages of the visitors
Draw a histogram of Age with 30 bins, and label the x axis 'Age'.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
ad_data = pd.read_csv('advertising.csv')Exercise
2. Income and age
Make a joint plot of Age (x) against Area Income (y).
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
ad_data = pd.read_csv('advertising.csv')Exercise
3. Time on site and age
Make a red joint plot of Age (x) against Daily Time Spent on Site (y) showing density contours (kind='kde').
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
ad_data = pd.read_csv('advertising.csv')4. Two groups appear
Exercise
5. Split the data
Use the number columns 'Daily Time Spent on Site', 'Age', 'Area Income', 'Daily Internet Usage' and 'Male' as X, and 'Clicked on Ad' as y. Split with test_size=0.33 and random_state=42.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
ad_data = pd.read_csv('advertising.csv')Exercise
6. Train, predict and evaluate
Fit a LogisticRegression(max_iter=1000) called logmodel, save its test predictions as predictions, and print a classification report.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
ad_data = pd.read_csv('advertising.csv')
from sklearn.model_selection import train_test_split
X = ad_data[['Daily Time Spent on Site', 'Age', 'Area Income', 'Daily Internet Usage', 'Male']]
y = ad_data['Clicked on Ad']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)About 97 in 100 visitors are classified correctly from just five numbers. A real ad team would use a model like this to decide who to show an ad to, and keep testing it as behavior changes.
Exercises solved: 0 of 5
Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.