Predicting Ad Click-through Rate with Random Forest

Ata Amrullah 24 Apr, 2023 • 6 min read


Click-through Rate (CTR) is a crucial metric that shows the percentage of visitors who click on an ad, providing insights into ad effectiveness. Businesses might considerably benefit from studying the click-through rate when developing their advertising tactics. By analyzing CTR statistics, companies may determine whether advertisements resonate with their target demographic and generate more engagement. By allocating money to the most effective advertising and modifying their marketing strategy to increase CTR, they are able to optimize their ad campaigns through Random forest classifier. The main objective of predicting ad CTR is:

  • Optimize ad campaigns by identifying which ads will likely result in higher click-through rates.
  • Maximize ad revenue by strategically placing high-performing ads in key positions.
  • Ad performance may be improved by identifying underperforming advertising and taking the appropriate steps to enhance it.

Based on these goals, we will use the Random Forest to develop a model that can accurately estimate whether the user will click on an ad based on the user’s age, daily time spent on the site, daily internet usage, and gender. This article will guide you through predicting whether the user will click the ad using Random Forest Classifier. Now, let’s make the prediction through the steps in the article.

This article was published as a part of the Data Science Blogathon.

Table of Contents

Step 1: Import Library

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import plotly.graph_objects as go
import as px
import as pio
pio.templates.default = "plotly_white"

We imported the ‘plotly’ library for easy data visualization. The ‘graph_objects’ module is used to create interactive and customizable visualizations, including plots, charts, and graphs. The ‘express’ module provides a high-level interface to create data visualizations with less code and a simpler interface. The ‘io’ module is used to configure various settings related to visualisations, such as templates, themes, and rendering options. We call ‘RandomForestClassifier’ to model and predict ad CTR. And the last line of code sets the default template for the Plotly visualization to be “plotly_white”, which is a predefined light or white background color scheme.

Step 2: Read the Data

The availability of data is critical for any data analysis task. A dataset with all the characteristics and variables necessary for the particular task is crucial. The dataset that Gaurav Dutta uploaded on Kaggle is appropriate in this specific instance. However, I put it on my GitHub to ease the analysis process.

url = ""
data = pd.read_csv(url)
 Dataset ov| Random Forest | Random forest classifier | CTR | Click Through Rate

Dataset ov

Below are all the features in the dataset:

  1. Daily Time Spent on Site means the daily timespan of the user on the website;
  2. Age means the age of the user;
  3. Area Income means the average income in the area of the user;
  4. Daily Internet Usage means the daily Internet usage of the user;
  5. Ad Topic Line means the title of the ad;
  6. City means the city of the user;
  7. Gender means the gender of the user;
  8. Country means the country of the user;
  9. Timestamp means the time when the user visited the website;
  10. Clicked on Ad means 1 if the user clicked on the ad, otherwise 0;
data["Clicked on Ad"] = data["Clicked on Ad"].map({0: "No", 1: "Yes"})

The code above is for transforming the contents of the “Clicked on Ad” column where 0=No and 1=Yes

Step 3: Click-Through Rate Analysis

First, We do an analysis to find out if user activity affects CTR.

fig =, 
             x="Daily Time Spent on Site",  
             color="Clicked on Ad", 
             title="Click Through Rate based on Time Spent on Site", 
 CTR vs Time spent | Random Forest | Random forest classifier | CTR | Click Through Rate

CTR vs. Time spent

People seem to be more inclined to click on advertisements the longer they stay on internet pages. Second, we conducted an analysis to find out whether a user’s daily internet usage affects CTR.

fig =, 
             x="Daily Internet Usage",  
             color="Clicked on Ad", 
             title="Click Through Rate based on Daily Internet Usage", 
 CTR vs Daily internet usage | Random Forest | Random forest classifier | CTR | Click Through Rate

CTR vs Daily internet usage

Based on the graph, more daily internet users will click ads more often. Next, we analyze whether the user’s age has an effect on the click-through rate.

fig =, 
             color="Clicked on Ad", 
             title="Click Through Rate based on Age", 
 CTR vs Age | Random Forest | Random forest classifier | CTR | Click Through Rate

CTR vs Age

Based on the graph above, users around the age of 40 dominate how often they click on ads. Next, we test whether user revenue has any effect on click-through rates.

fig =, 
             x="Area Income",  
             color="Clicked on Ad", 
             title="Click Through Rate based on Income", 
 CTR vs Income | Random Forest| Random forest classifier | CTR | Click Through Rate

CTR vs Income

High-income consumers are less likely to click on advertisements, although there appears to be hardly any difference that is statistically significant. The overall click-through rate for the ad is then calculated. Here, we must determine the proportion of users who left an impression on the advertisement versus those who clicked on it. So let’s examine the user distribution.

data["Clicked on Ad"].value_counts()
 User click

User Click

Therefore, 4917 out of 10,000 people clicked on the advertising. Let’s determine the CTR.

click_through_rate = 4917 / 10000 * 100
 CTR score

CTR score

Therefore, the CTR is 49.17.

Step 4: Build Random Forest Model and Make Predictions

Next, let’s create a machine-learning model that can forecast the click-through rate. The dataset will first be divided into training and testing sets. Before, the “Gender” column’s values had to be transformed into numbers. By substituting “Male” for “1” and “Female” for “0,” this effectively encodes the category variable “Gender” into binary form for quicker analysis. Moreover, the “Ad Topic Line” and “City” columns from the “x” dataframe should be deleted as they did not serve as input variables for the machine learning model.

data["Gender"] = data["Gender"].map({"Male": 1,"Female": 0})

x=x.drop(['Ad Topic Line','City'],axis=1)


Now let’s deploy the random forecast classification model to train the data.

model = RandomForestClassifier(), y)

Next, let’s calculate the accuracy of the model

y_pred = model.predict(xtest)
 Accuracy score

Accuracy score

It turns out that the accuracy score is very good, which is equal to 95.2%.

Finally, we come to the model testing stage by making predictions based on existing features.

import warnings

print("Ads Click Through Rate Prediction : ")
a = float(input("Daily Time Spent on Site: "))
b = float(input("Age: "))
c = float(input("Area Income: "))
d = float(input("Daily Internet Usage: "))
e = input("Gender (Male = 1, Female = 0) : ")

features = np.array([[a, b, c, d, e]])
print("Will the user click on ad = ", model.predict(features))
 Model testing| Random Forest | Random forest classifier | CTR | Click Through Rate

Model Testing

Variables a, b, c, d, and e are the features inputted by the user, while the prediction results show “yes,” which indicates that if the daily time spent on site is 61.2, the age is 35, the income area is 5800, the daily internet usage is 115.21, and the gender is male, then the prediction result is “yes.”


This article starts by analyzing of the click-through rate based on daily time spent on the website, the age of the user, area income, daily internet usage, and user gander. Then, it calculates the CTR score based on the sum of users before predicting the ad click-through rate using Random Forest Classifier. Broadly speaking, in this post, we have discussed the following:

  • How to find the features that affect the ad click-through rate prediction?
  • How do you calculate the CTR score based on the number of users who either clicked on the ad?
  • How to use Random Forest Classifier model to predict the ad click-through rate?

Overall, the article provides a comprehensive guide on ad click-through rate prediction with a Random Forest classifier using Python. If you have any questions or comments, please leave them below. The complete code is here.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Ata Amrullah 24 Apr 2023

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers

  • [tta_listen_btn class="listen"]