-
Notifications
You must be signed in to change notification settings - Fork 40
/
contextual_mab.py
executable file
·216 lines (159 loc) · 8.06 KB
/
contextual_mab.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# -*- coding: utf-8 -*-
import pandas as pd
from sklearn.preprocessing import StandardScaler
from mabwiser.mab import MAB, LearningPolicy, NeighborhoodPolicy
######################################################################################
#
# MABWiser
# Scenario: Advertisement Optimization
#
# An e-commerce website needs to solve the problem of which ad to display to _online users
# Each advertisement decision leads to generating different revenues
#
# What should the choice of advertisement be given the context of an _online user
# based on customer data such as age, click rate, subscriber?
#
######################################################################################
# Arms
ads = [1, 2, 3, 4, 5]
# Historical data of ad decisions with corresponding revenues and context information
train_df = pd.DataFrame({'ad': [1, 1, 1, 2, 4, 5, 3, 3, 2, 1, 4, 5, 3, 2, 5],
'revenues': [10, 17, 22, 9, 4, 20, 7, 8, 20, 9, 50, 5, 7, 12, 10],
'age': [22, 27, 39, 48, 21, 20, 19, 37, 52, 26, 18, 42, 55, 57, 38],
'click_rate': [0.2, 0.6, 0.99, 0.68, 0.15, 0.23, 0.75, 0.17,
0.33, 0.65, 0.56, 0.22, 0.19, 0.11, 0.83],
'subscriber': [1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0]}
)
# Arm features for warm start
arm_to_features = {1: [0, 1, 1], 2: [0, 0.5, 0.5], 3: [1, 1, 0.5], 4: [0.2, 1, 0], 5: [0, 1, 0.1], 6: [0, 0.5, 0.5]}
# Test data to for new prediction
test_df = pd.DataFrame({'age': [37, 52], 'click_rate': [0.5, 0.6], 'subscriber': [0, 1]})
test_df_revenue = pd.Series([7, 13])
# Scale the training and test data
scaler = StandardScaler()
train = scaler.fit_transform(train_df[['age', 'click_rate', 'subscriber']].values.astype('float64'))
test = scaler.transform(test_df.values.astype('float64'))
########################################################
# Radius Neighborhood Policy with UCB1 Learning Policy
########################################################
# Radius contextual policy with radius equals to 5 and ucb1 learning with alpha 1.25
radius = MAB(arms=ads,
learning_policy=LearningPolicy.UCB1(alpha=1.25),
neighborhood_policy=NeighborhoodPolicy.Radius(radius=5))
# Learn from previous ads shown and revenues generated
radius.fit(decisions=train_df['ad'], rewards=train_df['revenues'], contexts=train)
# Predict the next best ad to show
prediction = radius.predict(test)
# Expectation of each ad based on learning from past ad revenues
expectations = radius.predict_expectations(test)
# Results
print("Radius: ", prediction, " ", expectations)
assert(prediction == [4, 4])
# Online update of model
radius.partial_fit(decisions=prediction, rewards=test_df_revenue, contexts=test)
# Updating of the model with new arm
radius.add_arm(6)
# Warm start new arm
radius.warm_start(arm_to_features, distance_quantile=0.75)
########################################################
# KNearest Neighborhood Policy with UCB1 Learning Policy
########################################################
# KNearest context policy with k equals to 5 and ucb1 learning with alpha of 1.25
knearest = MAB(arms=ads,
learning_policy=LearningPolicy.UCB1(alpha=1.25),
neighborhood_policy=NeighborhoodPolicy.KNearest(k=5))
# Learn from previous ads shown and revenues generated
knearest.fit(decisions=train_df['ad'], rewards=train_df['revenues'], contexts=train)
# Predict the next best ad to show
prediction = knearest.predict(test)
# Expectation of each ad based on learning from past ad revenues
expectations = knearest.predict_expectations(test)
# Results
print("KNearest: ", prediction, " ", expectations)
assert(prediction == [5, 1])
# Online update of model
knearest.partial_fit(decisions=prediction, rewards=test_df_revenue, contexts=test)
# Updating of the model with new arm
knearest.add_arm(6)
########################################################
# KMeans Neighborhood Policy with UCB1 Learning Policy
########################################################
# KMeans clustering context policy with 4 clusters and ucb1 learning with alpha of 1.25
clusters = MAB(arms=ads,
learning_policy=LearningPolicy.UCB1(alpha=1.25),
neighborhood_policy=NeighborhoodPolicy.Clusters(n_clusters=4))
# Learn from previous ads shown and revenues generated
clusters.fit(decisions=train_df['ad'], rewards=train_df['revenues'], contexts=train)
# Predict the next best ad to show
prediction = clusters.predict(test)
# Expectation of each ad based on learning from past ad revenues
expectations = clusters.predict_expectations(test)
# Results
print("KMeans: ", prediction, " ", expectations)
assert(prediction == [5, 2])
# Online update of model
clusters.partial_fit(decisions=prediction, rewards=test_df_revenue, contexts=test)
# Updating of the model with new arm
clusters.add_arm(6)
#################################################################
# MiniBatchKMeans Neighborhood Policy with UCB1 Learning Policy
#################################################################
# MiniBatchKMeans clusters context policy with 4 clusters and ucb1 learning with alpha of 1.25
clusters = MAB(arms=ads,
learning_policy=LearningPolicy.UCB1(alpha=1.25),
neighborhood_policy=NeighborhoodPolicy.Clusters(n_clusters=4, is_minibatch=True))
# Learn from previous ads shown and revenues generated
clusters.fit(decisions=train_df['ad'], rewards=train_df['revenues'], contexts=train)
# Predict the next best ad to show
prediction = clusters.predict(test)
# Expectation of each ad based on learning from past ad revenues
expectations = clusters.predict_expectations(test)
# Results
print("MiniBatchKMeans: ", prediction, " ", expectations)
assert(prediction == [5, 2])
# Online update of model
clusters.partial_fit(decisions=prediction, rewards=test_df_revenue, contexts=test)
# Updating of the model with new arm
clusters.add_arm(6)
###############################################################
# LSH Approximate Neighborhood Policy with UCB1 Learning Policy
###############################################################
# LSH Approximate Neareset Neighbors contextual policy with n_dimenions and n_tables equal to 5
# and ucb1 learning with alpha 1.25
lshnearest = MAB(arms=ads,
learning_policy=LearningPolicy.UCB1(alpha=1.25),
neighborhood_policy=NeighborhoodPolicy.LSHNearest(n_dimensions=5, n_tables=5))
# Learn from previous ads shown and revenues generated
lshnearest.fit(decisions=train_df['ad'], rewards=train_df['revenues'], contexts=train)
# Predict the next best ad to show
prediction = lshnearest.predict(test)
# Expectation of each ad based on learning from past ad revenues
expectations = lshnearest.predict_expectations(test)
# Results
print("LSH Nearest: ", prediction, " ", expectations)
assert(prediction == [1, 4])
# Online update of model
lshnearest.partial_fit(decisions=prediction, rewards=test_df_revenue, contexts=test)
# Updating of the model with new arm
lshnearest.add_arm(6)
####################################################################
# TreeBandit Neighborhood Policy with Epsilon Greedy Learning Policy
####################################################################
# TreeBandit contextual policy with DecisionTreeClassifier's default tree parameters
# and ucb1 learning with alpha of 1.25
treebandit = MAB(arms=ads,
learning_policy=LearningPolicy.UCB1(alpha=1.25),
neighborhood_policy=NeighborhoodPolicy.TreeBandit())
# Learn from previous ads shown and revenues generated
treebandit.fit(decisions=train_df['ad'], rewards=train_df['revenues'], contexts=train)
# Predict the next best ad to show
prediction = treebandit.predict(test)
# Expectation of each ad based on learning from past ad revenues
expectations = treebandit.predict_expectations(test)
# Results
print("TreeBandit: ", prediction, " ", expectations)
assert(prediction == [4, 4])
# Online update of model
treebandit.partial_fit(decisions=prediction, rewards=test_df_revenue, contexts=test)
# Updating of the model with new arm
treebandit.add_arm(6)