-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
56 lines (43 loc) · 1.57 KB
/
model.py
File metadata and controls
56 lines (43 loc) · 1.57 KB
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
# -*- coding: utf-8 -*-
"""Copy of upper_confidence_bound.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1swscvJa1y8ioQ3Qp-8zW1gCWcqc7QYR-
# Upper Confidence Bound (UCB)
## Importing the libraries
"""
import numpy as np
import pandas as pd
"""## Importing the dataset"""
dataset = pd.read_csv('Book1.csv')
"""## Implementing UCB"""
import math
def mod(pt):
N = pt#dropbox ka selection idhar aayega but by default yeh value honi chahiye
d = 10#column daal diyo
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
"""## Visualising the results"""
p=ads_selected[len(ads_selected)-1]
print(numbers_of_selections[p]/N)
return (ads_selected[len(ads_selected)-1])