Identifying the right advertisement banner using MAB

Let us say you are running a website and you have five different banners for the same ad, and you want to know which banner attracts the user. We model this problem statement as a bandit problem. Let us say these five banners are the five arms of the bandit and we award one point if the user clicks the ad and award zero if the user does not click the ad.

In normal A/B testing, we will perform a complete exploration of all these five banners before deciding which banner is the best. But that will cost us a lot of energy and time. Instead, we will use a good exploration strategy for deciding which banner will give us the most rewards (most clicks). 

First, let us import the necessary libraries:

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

Let us simulate a dataset with 5 x 10,000 as the shape, where the column is the Banner_type ad and the rows are either 0 or 1, that is, whether the ad has been clicked (1) or not clicked (0) by the user respectively:

df = pd.DataFrame()
df['Banner_type_0'] = np.random.randint(0,2,100000)
df['Banner_type_1'] = np.random.randint(0,2,100000)
df['Banner_type_2'] = np.random.randint(0,2,100000)
df['Banner_type_3'] = np.random.randint(0,2,100000)
df['Banner_type_4'] = np.random.randint(0,2,100000)

Let us view a few rows of our data:

df.head()
num_banner = 5
no_of_iterations = 100000
banner_selected = []
count = np.zeros(num_banner)
Q = np.zeros(num_banner)
sum_rewards = np.zeros(num_banner)

Define an epsilon-greedy policy:

def epsilon_greedy(epsilon):

random_value = np.random.random()
choose_random = random_value < epsilon

if choose_random:
action = np.random.choice(num_banner)
else:
action = np.argmax(Q)

return action

 

for i in range(no_of_iterations):
banner = epsilon_greedy(0.5)

reward = df.values[i, banner]
count[banner] += 1
sum_rewards[banner]+=reward
Q[banner] = sum_rewards[banner]/count[banner]

banner_selected.append(banner)

We can plot the results and see which banner gives us the maximum number of clicks:

sns.distplot(banner_selected)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.187.210