Skip to content

Commit 2f872c0

Browse files
author
Lazy Programmer
committed
py3
1 parent 0f1e28b commit 2f872c0

File tree

6 files changed

+44
-8
lines changed

6 files changed

+44
-8
lines changed

ab_testing/bayesian_bandit.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# From the course: Bayesin Machine Learning in Python: A/B Testing
22
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
33
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
4+
from __future__ import print_function, division
5+
from builtins import range
6+
# Note: you may need to update your version of future
7+
# sudo pip install -U future
8+
9+
410
import matplotlib.pyplot as plt
511
import numpy as np
612
from scipy.stats import beta
@@ -41,7 +47,7 @@ def experiment():
4147
bandits = [Bandit(p) for p in BANDIT_PROBABILITIES]
4248

4349
sample_points = [5,10,20,50,100,200,500,1000,1500,1999]
44-
for i in xrange(NUM_TRIALS):
50+
for i in range(NUM_TRIALS):
4551

4652
# take a sample from each bandit
4753
bestb = None
@@ -54,7 +60,7 @@ def experiment():
5460
maxsample = sample
5561
bestb = b
5662
if i in sample_points:
57-
print "current samples: %s" % allsamples
63+
print("current samples: %s" % allsamples)
5864
plot(bandits, i)
5965

6066
# pull the arm for the bandit with the largest sample

ab_testing/chisquare.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# From the course: Bayesin Machine Learning in Python: A/B Testing
22
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
33
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
4+
from __future__ import print_function, division
5+
from builtins import range
6+
# Note: you may need to update your version of future
7+
# sudo pip install -U future
8+
9+
410
import numpy as np
511
import matplotlib.pyplot as plt
612
from scipy.stats import chi2, chi2_contingency
@@ -47,7 +53,7 @@ def run_experiment(p1, p2, N):
4753
data = DataGenerator(p1, p2)
4854
p_values = np.empty(N)
4955
T = np.zeros((2, 2)).astype(np.float32)
50-
for i in xrange(N):
56+
for i in range(N):
5157
c1, c2 = data.next()
5258
T[0,c1] += 1
5359
T[1,c2] += 1

ab_testing/ci_comparison.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# From the course: Bayesin Machine Learning in Python: A/B Testing
22
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
33
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
4+
from __future__ import print_function, division
5+
from builtins import range
6+
# Note: you may need to update your version of future
7+
# sudo pip install -U future
8+
9+
410
import matplotlib.pyplot as plt
511
import numpy as np
612
from scipy.stats import beta, norm
@@ -10,7 +16,7 @@
1016
a, b = 1, 1 # beta priors
1117
plot_indices = (10, 20, 30, 50, 100, 200, 500)
1218
data = np.empty(T)
13-
for i in xrange(T):
19+
for i in range(T):
1420
x = 1 if np.random.random() < true_ctr else 0
1521
data[i] = x
1622

ab_testing/convergence.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# From the course: Bayesin Machine Learning in Python: A/B Testing
22
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
33
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
4+
from __future__ import print_function, division
5+
from builtins import range
6+
# Note: you may need to update your version of future
7+
# sudo pip install -U future
8+
9+
410
import matplotlib.pyplot as plt
511
import numpy as np
612
from bayesian_bandit import Bandit
@@ -11,7 +17,7 @@ def run_experiment(p1, p2, p3, N):
1117

1218
data = np.empty(N)
1319

14-
for i in xrange(N):
20+
for i in range(N):
1521
# thompson sampling
1622
j = np.argmax([b.sample() for b in bandits])
1723
x = bandits[j].pull()

ab_testing/demo.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# From the course: Bayesin Machine Learning in Python: A/B Testing
22
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
33
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
4+
from __future__ import print_function, division
5+
from builtins import range
6+
# Note: you may need to update your version of future
7+
# sudo pip install -U future
8+
9+
410
import numpy as np
511
import matplotlib.pyplot as plt
612
from scipy.stats import beta
@@ -16,7 +22,7 @@ def plot(a, b, trial, ctr):
1622
true_ctr = 0.3
1723
a, b = 1, 1 # beta parameters
1824
show = [0, 5, 10, 25, 50, 100, 200, 300, 500, 700, 1000, 1500]
19-
for t in xrange(1501):
25+
for t in range(1501):
2026
coin_toss_result = (np.random.random() < true_ctr)
2127
if coin_toss_result:
2228
a += 1

ab_testing/ttest.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# From the course: Bayesin Machine Learning in Python: A/B Testing
22
# https://deeplearningcourses.com/c/bayesian-machine-learning-in-python-ab-testing
33
# https://www.udemy.com/bayesian-machine-learning-in-python-ab-testing
4+
from __future__ import print_function, division
5+
from builtins import range
6+
# Note: you may need to update your version of future
7+
# sudo pip install -U future
8+
9+
410
import numpy as np
511
from scipy import stats
612

@@ -16,8 +22,8 @@
1622
t = (a.mean() - b.mean()) / (s * np.sqrt(2.0/N)) # t-statistic
1723
df = 2*N - 2 # degrees of freedom
1824
p = 1 - stats.t.cdf(np.abs(t), df=df) # one-sided test p-value
19-
print "t:\t", t, "p:\t", 2*p # two-sided test p-value
25+
print("t:\t", t, "p:\t", 2*p) # two-sided test p-value
2026

2127
# built-in t-test:
2228
t2, p2 = stats.ttest_ind(a, b)
23-
print "t2:\t", t2, "p2:\t", p2
29+
print("t2:\t", t2, "p2:\t", p2)

0 commit comments

Comments
 (0)