<jupyter_start><jupyter_text>Advertisement CTR optimisation  Upper Confidence Bound (UCB)  Importing the libraries<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd<jupyter_output><empty_output><jupyter_text>Importing the dataset<jupyter_code>dataset = pd.read_csv('Ads_CTR_Optimisation.csv')<jupyter_output><empty_output><jupyter_text>Upper Confidence Bound Algorithm   Step 1 At each round n, we consider two numbers for each ad i:  Ni(n) - the number of times the ad i was selected up to round n  Ri(n) - the sum of rewards of the ad i up to round n      Step 2  From these two numbers we compute:    The average reward of ad i upto round n       r_avgi(n) = Ri(n) / Ni(n)      The confidence interval [ r_avgi(n) - &Delta;i(n) , r_avgi(n) + &Delta;i(n) ] at    round n with       &Delta;i(n) = &radic;3log(n) / 2 Ni(n))           Step 3 We select the ad i that has the maximum UCB r_avgi(n) + &Delta;i(n)      Implementing UCB<jupyter_code>import math
N = 1000
d = 10
ads_selected = []
numbers_of_selections = [0] * d
sums_of_rewards = [0] * d
total_reward = 0
for n in range(0, N):
    ad = 0
    max_upper_bound = 0
    for i in range(0, d):
        if (numbers_of_selections[i] > 0):
            average_reward = sums_of_rewards[i] / numbers_of_selections[i]
            delta_i = math.sqrt(3/2 * math.log(n + 1) / numbers_of_selections[i])
            upper_bound = average_reward + delta_i
        else:
            upper_bound = 1e400
        if upper_bound > max_upper_bound:
            max_upper_bound = upper_bound
            ad = i
    ads_selected.append(ad)
    numbers_of_selections[ad] = numbers_of_selections[ad] + 1
    reward = dataset.values[n, ad]
    sums_of_rewards[ad] = sums_of_rewards[ad] + reward
    total_reward = total_reward + reward<jupyter_output><empty_output><jupyter_text>Visualising the results<jupyter_code>plt.hist(ads_selected)
plt.title('Histogram of ads selections')
plt.xlabel('Ads')
plt.ylabel('Number of times each ad was selected')
plt.show()<jupyter_output><empty_output><jupyter_text>Thompson Sampling  Implementing Thompson Sampling  Thompson Sampling Algorithm Step 1 At each round n, we consider two numbers for each ad i:  Ni1(n) - the number of times the ad i got reward 1 up to round n  Ni0(n) - the number of times the ad i got reward 1 up to round n   Step 2 For each ad i, we take a random draw from the distribution below:  &theta;i(n) = &beta;( Ni1(n) + 1 , Ni0(n) + 1 )   Step 3  We select the ad that has the highest &theta;i(n)<jupyter_code>import random
N = 500
d = 10
ads_selected = []
numbers_of_rewards_1 = [0] * d
numbers_of_rewards_0 = [0] * d
total_reward = 0
for n in range(0, N):
    ad = 0
    max_random = 0
    for i in range(0, d):
        random_beta = random.betavariate(numbers_of_rewards_1[i] + 1, numbers_of_rewards_0[i] + 1)
        if random_beta > max_random:
            max_random = random_beta
            ad = i
    ads_selected.append(ad)
    reward = dataset.values[n, ad]
    if reward == 1:
        numbers_of_rewards_1[ad] = numbers_of_rewards_1[ad] + 1
    else:
        numbers_of_rewards_0[ad] = numbers_of_rewards_0[ad] + 1
    total_reward = total_reward + reward<jupyter_output><empty_output><jupyter_text>Visualising the results - Histogram<jupyter_code>plt.hist(ads_selected)
plt.title('Histogram of ads selections')
plt.xlabel('Ads')
plt.ylabel('Number of times each ad was selected')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09555427251732102, 'fraction_numerical': 0.018187066974595843, 'mean_word_length': 3.688768606224628, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '491154', 'license': 'MIT', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1228, 'n_words': 355, 'path': 'Ads-CTR-Optimisation/Ads_optimise.ipynb', 'repo_name': 'Abhishek-Aditya-bs/Machine-Learning-Projects', 'size': '3464'}