楔子:
Thompson抽样算法.PNG 贝叶斯推理.PNG
1、数据预处理
代码:
# Thompson Sampling
# Importing the dataset
dataset = read.csv('Ads_CTR_Optimisation.csv')
2、数据初始化
代码:
# Implementing Thompson Sampling
N = 10000
d = 10
ads_selected = integer(0)
numbers_of_rewards_1 = integer(d)
numbers_of_rewards_0 = integer(d)
total_reward = 0
3、ThompsonSampling
代码:
for (n in 1:N) {
ad = 0
max_random = 0
for (i in 1:d) {
random_beta = rbeta(n = 1,
shape1 = numbers_of_rewards_1[i] + 1,
shape2 = numbers_of_rewards_0[i] + 1)
if (random_beta > max_random) {
max_random = random_beta
ad = i
}
}
ads_selected = append(ads_selected, ad)
reward = dataset[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
}
4、数据可视化
代码:
# Visualising the results
hist(ads_selected,
col = 'blue',
main = 'Histogram of ads selections',
xlab = 'Ads',
ylab = 'Number of times each ad was selected')
5、结果
最佳广告.PNG 点击次数.PNG我们可以看出:无论是点击次数还是寻找最佳广告的速度,Thompson抽样算法都更胜一筹。
网友评论