-
Notifications
You must be signed in to change notification settings - Fork 1
/
Upper Confidence Bound.py
62 lines (50 loc) · 1.97 KB
/
Upper Confidence Bound.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#Uppper Confidence Bound
#importing the libraries
#http://pandas.pydata.org/
import pandas as pd
#http://www.numpy.org/
import numpy as np
#Maths
import math
#Reduce function
from functools import reduce
#Step 1: Importing Data
#importing the dataset using pandas
def import_dataset(dataset_name, file_type):
dataset = getattr(pd, "read_{}".format(file_type))(dataset_name)
return dataset
#Step 1: Importing Data
dataset_name = 'Your dataset name here including file type'
dataset_type = 'File type'
dataset = import_dataset(dataset_name, dataset_type)
#Step 2: Number of selections with iteration
def all_rounds(dataset, number_bandits, total_rounds, default_confidence, default_average):
number_times = [0] * number_bandits
sum_reward = [0] * number_bandits
winner_index = 0
selected_ad = []
for i in range(0,total_rounds):
number_times[winner_index] += 1
sum_reward[winner_index] += dataset.values[i, winner_index]
average_reward = []
confidences = []
upper_bound = []
selected_ad.append(winner_index)
for i in range(0,number_bandits):
#Error handling in case number_times == 0
try:
average = (sum_reward[i]/number_times[i])
except:
average = default_average
try:
confidence = math.sqrt(1.5*math.log(i+1)/number_times[i])
except:
confidence = default_confidence
average_reward.append(average)
confidences.append(confidence)
upper_bound.append(average_reward[i] + confidences[i])
winner = max(upper_bound)
winner_index = upper_bound.index(winner)
return (number_times, sum_reward, selected_ad)
result = all_rounds(dataset, len(dataset.columns), len(dataset), 0.5, 0.5)
total_score = reduce(lambda x,y: x+y, result[1])