Sitemap
Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

What Drives Users to Click Ads? A Data Science Approach to Digital Marketing

6 min readFeb 28, 2025

--

This article is also available in Portuguese here.

In this post, we will explore an advertising dataset and build a predictive model to forecast ad clicks.

Press enter or click to view image in full size
Photo byJulia M Cameron (https://www.pexels.com/pt-br/@julia-m-cameron/) via Pexels

Understanding the Business

1.1 Problem Statement

Digital advertising plays a crucial role in reaching and engaging consumers online, but it’s not always easy to understand the factors that lead a user to click on an ad. In the context of this project, we are working with a fictitious data set that records information such as age, location, time of access and characteristics of the advertising campaign, as well as indicating whether or not the user clicked on the ad. The challenge is to identify patterns in this data that can explain user behavior when faced with different campaigns, allowing for a deeper understanding of the impact of digital marketing strategies.

1.2 Objective

This project aims to build a predictive model capable of determining the probability of a user clicking on an advertisement based on the data provided. Through exploratory analysis and the application of machine learning algorithms, we will try to highlight the most relevant factors for the user’s decision-making. This model can target more effective and personalized campaigns, promoting a better allocation of resources and a greater return on investment in advertising.

2 Understanding the Data

2.1. Description of data and collection

A dataset called advertising.csv has been made available which contains the following features:
‘Daily Time Spent on Site’: time spent on the site in minutes.
‘Age’: age of the consumer.
‘Area Income’: Average consumer income in the region.
‘Daily Internet Usage’: Average minutes per day the consumer is on the Internet.
‘Ad topic line’: Title of the ad.
‘City’: Consumer’s city.
‘Male’: Whether or not the shopper was male.
‘Country’: Shopper’s country.
‘Timestamp’: Time at which the shopper clicked on the ad or closed window.
‘Clicked on Ad’: 0 or 1 indicates whether or not the ad was clicked on.

Press enter or click to view image in full size
Figure 1 — database
Figure 2 — metadata

As timestamp is listed as an object type, it is important to convert it to a date.

2.2 Exploratory Analysis

Initially, a histogram was made about age with 30 bands:

Figure 3 — age histogram

Note that the graph shows a higher concentration from the age of 30 onwards. Next, a joint plot is made between age vs. area income:

Figure 4 — joint plot between Age vs. Area Income

Despite having a concentration above 50,000 and age above 30, there is no linear relationship between the two fields. This time we used a joint plot in KDE format between Daily Time Spent on Site vs. Age:

Figure 5 — joint plot in KDE format between Daily Time Spent on Site vs. Age

There seems to be a linear relationship between age and time spent on the site. The graph above shows that the more experienced a person is, the less time they spend on the site.

Figure 6 — joint plot between Daily Internet Usage vs. Daily Time Spent on Site

Apparently, there is no considerable linear relationship between Daily Internet Usage and Daily Time Spent on Site.

Figure 7 — pair plot of variables

In the pair plot above, data segmentation is clearly evident. Since we are building a classification model, we used the ‘Clicked on Ad’ field as a reference. Well-separated clusters facilitate the creation of an efficient predictive model, as shown in the graphs above.

3. Logistic Regression

When selecting data for training and testing, we excluded the hours variable due to the complexity of its mathematical transformations. The remaining variables are separated as follows:

Figure 8 — Train Test Split

3.1 Conclusions

Get Rodrigo Higashi’s stories in your inbox

Join Medium for free to get updates from this writer.

When training and adjusting the model, we will see a confusion matrix:

Figure 9 — Confusion Matrix

The model classifies most cases accurately, correctly identifying both users who click and those who don’t. There are few false positives, meaning the model rarely misidentifies a click from someone who didn’t click. However, there are some cases where the model failed to identify users who actually clicked on the ad.

The classification report confirms the model’s accuracy at 90%:

Figure 10 — Classification Report

The model’s precision improved to 98%, indicating that when it predicts someone will click, the likelihood of being correct is even higher. The recall also increased to 96%, showing that the model is missing very few actual clicks (only 7 false negatives). If the main goal is to minimize false negatives (FN), the current performance is already quite satisfactory, but it’s still possible to adjust the classification threshold to further improve sensitivity, even if that slightly reduces precision.

A ROC curve of the model was also generated:

Figure 11 — ROC Curve

The ROC curve climbs quickly toward the top-left corner, demonstrating that the model achieves a high true positive rate (sensitivity) with a low false positive rate. The area under the ROC curve is 0.9876, which indicates strong performance in predicting whether the user clicked on the ad. Lastly, a KS of 94.6% shows the model’s excellent ability to discriminate between users who clicked and those who didn’t.

To further validate the model’s performance, I applied 5-fold cross-validation and calculated several metrics. The average accuracy was 97.0%, confirming the model’s strong overall ability to make correct predictions. Precision was high at 98.2%, indicating that the model rarely predicts clicks incorrectly. Recall reached 95.8%, showing that the model successfully identifies most actual clicks. Finally, the F1-score — which balances precision and recall — was also high, at 96.9%, demonstrating the model’s robustness across all data splits. These results reinforce the model’s reliability beyond a single train-test split.

💼 Practical applications and business perspective

Based on the model results, it would be possible to:

  • Target advertising campaigns to users more likely to click
  • Reduce marketing costs by avoiding poorly segmented audiences
  • Increase conversion rates using predictive insights
  • Customize the user journey based on click propensity

This type of model can be applied in real-world scenarios like e-commerce, digital marketing, and conversion optimization.

Check out my complete projects on GitHub or see more articles in my portfolio.

Thank you for being a part of the community

Before you go:

--

--

Written by Rodrigo Higashi

Brazillian graduated in Mathematics and is studying Python and Statistics to be a Data Scientist and work with AI.

Published in Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.