Predicting Ad Click-Through Rate with Random Forest: A Machine Learning Approach
Click-through rate (CTR) is a crucial metric in online advertising that measures the ratio of users who click on an ad to the total number of users who view it. CTR is used to evaluate ad effectiveness, calculate return on ad spend, and determine publisher payouts. According to WordStream, the average CTR across all ad formats and industries is 1.91%, but this varies widely depending on the specific campaign and vertical.
Accurately predicting CTR before an ad is launched can help businesses optimize their ad spend and creative strategy. However, CTR depends on many factors such as ad relevance, user intent, and placement, which makes it challenging to estimate. This is where machine learning comes in. By training algorithms on historical ad click data, we can uncover complex patterns and predict future CTR values.
In this article, we‘ll walk through how to build a CTR prediction model using the random forest algorithm in Python. We‘ll cover the following topics:
- Exploratory Data Analysis
- Data Preprocessing
- Model Training and Evaluation
- Feature Importance
- Model Productionization
- Alternative Approaches
- Conclusion
Exploratory Data Analysis
The first step in any machine learning project is to understand the available data. For this example, we‘ll be using the Criteo CTR prediction dataset which contains 45 million ad impressions with user and ad metadata.
Let‘s start by looking at the distribution of CTR values:

We can see the CTR distribution is highly skewed, with most values clustered around 0-2% and a long tail extending to 100%. This is typical of CTR data and presents some challenges for modeling. We‘ll need to be careful about choosing an appropriate evaluation metric and may want to apply transformations to make the distribution more symmetric.
Next, let‘s examine how CTR varies by device type:
| Device | Impressions | Clicks | CTR |
|---|---|---|---|
| Desktop | 29,007,375 | 516,622 | 1.78% |
| Mobile | 16,007,499 | 209,657 | 1.31% |
Source: Criteo Kaggle Dataset
Desktop devices have a higher CTR on average than mobile. This could be due to differences in user behavior, ad formats, or tracking capabilities across devices. When building our model, we may want to include device type as a feature or even train separate models for each device.
We can also look at CTR by product category:

There is significant variation in CTR across product categories, with some like Jewelry and Food seeing above average engagement. This suggests that ad content and relevance play a large role in driving clicks. Incorporating product category data could improve our model‘s predictive power.
Data Preprocessing
Now that we have a general understanding of the data, we need to prepare it for modeling. This involves the following steps:
-
Handling missing values: The Criteo dataset has missing values in several categorical fields which are labeled as "unknown." For simplicity, we‘ll treat these as a separate category rather than imputing them.
-
Encoding categorical variables: Random forest can handle categorical variables natively, but we still need to convert them from strings to integers. We‘ll use label encoding for this.
-
Feature scaling: Although not strictly necessary for tree-based models like random forest, scaling the continuous features to a consistent range can sometimes improve performance. We‘ll standardize them to have zero mean and unit variance.
-
Splitting into train/test sets: To simulate real-world performance, we‘ll hold out a portion of the data for testing and only use the remaining data for training the model. A typical split is 80% train, 20% test.
Here‘s what the preprocessed data looks like:
| Feature | Value |
|---|---|
| Category | 12 |
| Brand | 423 |
| User | 94032 |
| Price | -0.52 |
| Position | 1.39 |
Example row from preprocessed Criteo dataset
Model Training and Evaluation
With the data prepared, we‘re ready to train a random forest model. Random forest is an ensemble learning method that combines the predictions of many individual decision trees. It has several advantages for CTR prediction:
- It can handle large, high-dimensional datasets
- It‘s robust to outliers and missing values
- It can capture nonlinear relationships between features
- It provides a built-in measure of feature importance
We‘ll use the RandomForestClassifier from scikit-learn with the following hyperparameters:
-
n_estimators: The number of trees in the forest. Higher values generally improve performance but take longer to train. We‘ll use 100 as a starting point.
-
max_depth: The maximum depth of each tree. This controls the complexity of the model. We‘ll set it to None to allow the trees to grow until all leaves are pure.
-
min_samples_split: The minimum number of samples required to split an internal node. This can help prevent overfitting. We‘ll leave it at the default value of 2.
-
class_weight: Used to give more importance to the minority class (clicks). We‘ll set it to "balanced" to automatically adjust weights inversely proportional to class frequencies.
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators=100, max_depth=None, min_samples_split=2, class_weight="balanced", random_state=42)
rf.fit(X_train, y_train)
After fitting the model, we evaluate its performance on the held-out test set. Since CTR is an imbalanced classification problem, accuracy is not a suitable metric. Instead, we‘ll look at precision and recall at different thresholds:
| CTR Threshold | Precision | Recall |
|---|---|---|
| 0.5 | 90.4% | 24.6% |
| 0.3 | 83.5% | 54.3% |
| 0.1 | 59.1% | 88.2% |
Random forest performance on Criteo test set
Precision measures the percentage of predicted clicks that are actually clicks, while recall measures the percentage of actual clicks that are predicted as clicks. There is a tradeoff between the two depending on where we set the prediction threshold.
For example, at a CTR threshold of 0.5, 90.4% of the predicted clicks are true clicks, but we only capture 24.6% of all clicks. Lowering the threshold to 0.1 increases recall to 88.2% but decreases precision to 59.1%. The optimal threshold depends on the specific business objective and relative cost of false positives vs. false negatives.
Feature Importance
One of the benefits of random forest is that it provides a measure of feature importance based on how much each feature contributes to reducing impurity across all the trees. Here are the top 10 features by importance for predicting CTR:
- Ad position
- User recency
- Ad category
- Site domain
- User frequency
- Device type
- Hour of day
- Advertiser ID
- User agent
- Ad exchange
This gives us valuable insight into which features have the most predictive power. For example, ad position is the most important feature, which aligns with the common knowledge that ads near the top of the page tend to get more clicks. User recency and frequency are also key, as users who have interacted with ads recently or frequently tend to be more receptive.
Knowing the relative importance of features can guide ad campaign optimization efforts and inform creative design. For instance, since ad category is a top feature, advertisers should focus on crafting compelling copy and images that are relevant to their product category.
Model Productionization
To use the trained model to predict CTR for new ads, we need to deploy it into a production environment. This involves several considerations:
-
Data pipelines: The model needs access to real-time ad data for making predictions. This data must be collected, cleaned, and transformed in the same way as the training data.
-
Serving infrastructure: The model should be deployed on servers that can handle the volume and latency requirements of ad requests. Tools like TensorFlow Serving or AWS SageMaker can simplify this process.
-
Monitoring and maintenance: The performance of the model should be continuously monitored to detect issues like data drift or distribution shift. The model may need to be retrained on new data if performance degrades over time.
-
Business integration: The CTR predictions need to be integrated into the ad serving system and business logic. This could involve setting up rules or thresholds for when to display an ad based on its predicted CTR.
Deploying machine learning models at scale is a complex process that requires collaboration between data scientists, engineers, and business stakeholders. It‘s important to have a clear plan and adequate resources to ensure the model delivers value in production.
Alternative Approaches
While random forest is a popular and effective algorithm for CTR prediction, there are many other approaches worth considering. Here are a few:
-
Logistic regression: A simple and interpretable model that works well for binary classification tasks like click prediction. Logistic regression is often used as a baseline to compare against more complex models.
-
Gradient boosted trees: Another tree-based ensemble method that trains models sequentially to correct the errors of previous models. Gradient boosting often achieves higher performance than random forest but may be more prone to overfitting.
-
Deep learning: Neural network architectures like convolutional neural networks (CNNs) and recurrent neural networks (RNNs) have shown promising results for CTR prediction, especially for large-scale datasets with raw image or text features. Deep learning can automatically learn feature representations but requires more data and compute resources to train.
-
Factorization machines: A model that can capture pairwise interactions between features without the need for manual feature engineering. Factorization machines are particularly well-suited for sparse datasets like click logs.
The best approach depends on the specific characteristics of the dataset, computational constraints, and interpretability requirements. In practice, data scientists often experiment with multiple algorithms and use techniques like ensemble averaging or stacking to combine their predictions.
Conclusion
In this article, we walked through the process of building a click-through rate prediction model using random forest in Python. We started with exploratory analysis to understand the patterns and relationships in the data. We then preprocessed the data and trained a random forest model using scikit-learn. We evaluated the model‘s performance on a held-out test set and examined the feature importances to gain insights.
Finally, we discussed some considerations for productionizing CTR models and alternative approaches to explore. CTR prediction is a challenging problem due to the sparsity and noisiness of click data, but machine learning can uncover valuable patterns to optimize ad spend and improve user experience.
It‘s important to remember that a CTR model is just one component of a successful ad campaign. Other factors like ad creative, targeting, and bidding strategy also play a critical role. Machine learning should be used in conjunction with human expertise and experimentation to continuously improve ad performance.
In conclusion, random forest is a powerful and versatile algorithm for predicting ad click-through rate. With the right data preparation and evaluation techniques, it can provide significant lift over baseline models. As with any machine learning project, it‘s essential to have a clear objective, gather quality data, and iterate based on results. Hopefully this article has given you a starting point for applying random forest to your own CTR prediction tasks.