Skip to content

Commit 85aea94

Browse files
py3 compatible
1 parent eee6fee commit 85aea94

File tree

11 files changed

+82
-15
lines changed

11 files changed

+82
-15
lines changed

linear_regression_class/generate_1d.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
55
# https://www.udemy.com/data-science-linear-regression-in-python
66

7+
from __future__ import print_function, division
8+
from builtins import range
9+
# Note: you may need to update your version of future
10+
# sudo pip install -U future
11+
12+
713

814
import numpy as np
915

1016
N = 100
1117
with open('data_1d.csv', 'w') as f:
1218
X = np.random.uniform(low=0, high=100, size=N)
1319
Y = 2*X + 1 + np.random.normal(scale=5, size=N)
14-
for i in xrange(N):
20+
for i in range(N):
1521
f.write("%s,%s\n" % (X[i], Y[i]))
1622

linear_regression_class/generate_2d.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
55
# https://www.udemy.com/data-science-linear-regression-in-python
66

7+
from __future__ import print_function, division
8+
from builtins import range
9+
# Note: you may need to update your version of future
10+
# sudo pip install -U future
11+
12+
713

814
import numpy as np
915

@@ -12,5 +18,5 @@
1218
with open('data_2d.csv', 'w') as f:
1319
X = np.random.uniform(low=0, high=100, size=(N,2))
1420
Y = np.dot(X, w) + 1 + np.random.normal(scale=5, size=N)
15-
for i in xrange(N):
21+
for i in range(N):
1622
f.write("%s,%s,%s\n" % (X[i,0], X[i,1], Y[i]))

linear_regression_class/generate_poly.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
55
# https://www.udemy.com/data-science-linear-regression-in-python
66

7+
from __future__ import print_function, division
8+
from builtins import range
9+
# Note: you may need to update your version of future
10+
# sudo pip install -U future
11+
712

813
import numpy as np
914

@@ -12,6 +17,6 @@
1217
X = np.random.uniform(low=0, high=100, size=N)
1318
X2 = X*X
1419
Y = 0.1*X2 + X + 3 + np.random.normal(scale=10, size=N)
15-
for i in xrange(N):
20+
for i in range(N):
1621
f.write("%s,%s\n" % (X[i], Y[i]))
1722

linear_regression_class/gradient_descent.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
33
# https://www.udemy.com/data-science-linear-regression-in-python
44

5+
from __future__ import print_function, division
6+
from builtins import range
7+
# Note: you may need to update your version of future
8+
# sudo pip install -U future
9+
10+
511
import numpy as np
612
import matplotlib.pyplot as plt
713

@@ -14,7 +20,7 @@
1420
Y = np.array([0]*5 + [1]*5)
1521

1622
# print X so you know what it looks like
17-
print "X:", X
23+
print("X:", X)
1824

1925
# won't work!
2026
# w = np.linalg.solve(X.T.dot(X), X.T.dot(Y))
@@ -23,7 +29,7 @@
2329
costs = [] # keep track of squared error cost
2430
w = np.random.randn(D) / np.sqrt(D) # randomly initialize w
2531
learning_rate = 0.001
26-
for t in xrange(1000):
32+
for t in range(1000):
2733
# update w
2834
Yhat = X.dot(w)
2935
delta = Yhat - Y
@@ -37,7 +43,7 @@
3743
plt.plot(costs)
3844
plt.show()
3945

40-
print "final w:", w
46+
print("final w:", w)
4147

4248
# plot prediction vs target
4349
plt.plot(Yhat, label='prediction')

linear_regression_class/l1_regularization.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
33
# https://www.udemy.com/data-science-linear-regression-in-python
44

5+
from __future__ import print_function, division
6+
from builtins import range
7+
# Note: you may need to update your version of future
8+
# sudo pip install -U future
9+
10+
511
import numpy as np
612
import matplotlib.pyplot as plt
713

@@ -22,7 +28,7 @@
2228
w = np.random.randn(D) / np.sqrt(D) # randomly initialize w
2329
learning_rate = 0.001
2430
l1 = 10.0 # Also try 5.0, 2.0, 1.0, 0.1 - what effect does it have on w?
25-
for t in xrange(500):
31+
for t in range(500):
2632
# update w
2733
Yhat = X.dot(w)
2834
delta = Yhat - Y
@@ -36,7 +42,7 @@
3642
plt.plot(costs)
3743
plt.show()
3844

39-
print "final w:", w
45+
print("final w:", w)
4046

4147
# plot our w vs true w
4248
plt.plot(true_w, label='true w')

linear_regression_class/l2_regularization.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
55
# https://www.udemy.com/data-science-linear-regression-in-python
66

7+
from __future__ import print_function, division
8+
from builtins import range
9+
# Note: you may need to update your version of future
10+
# sudo pip install -U future
11+
12+
13+
714
import numpy as np
815
import matplotlib.pyplot as plt
916

linear_regression_class/lr_1d.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
55
# https://www.udemy.com/data-science-linear-regression-in-python
66

7+
from __future__ import print_function, division
8+
from builtins import range
9+
# Note: you may need to update your version of future
10+
# sudo pip install -U future
11+
12+
713
import numpy as np
814
import matplotlib.pyplot as plt
915

linear_regression_class/lr_2d.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
55
# https://www.udemy.com/data-science-linear-regression-in-python
66

7+
from __future__ import print_function, division
8+
from builtins import range
9+
# Note: you may need to update your version of future
10+
# sudo pip install -U future
11+
12+
713
import numpy as np
814
from mpl_toolkits.mplot3d import Axes3D
915
import matplotlib.pyplot as plt
@@ -42,4 +48,4 @@
4248
d1 = Y - Yhat
4349
d2 = Y - Y.mean()
4450
r2 = 1 - d1.dot(d1) / d2.dot(d2)
45-
print "the r-squared is:", r2
51+
print("the r-squared is:", r2)

linear_regression_class/moore.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
55
# https://www.udemy.com/data-science-linear-regression-in-python
66
# transistor count from: https://en.wikipedia.org/wiki/Transistor_count
7+
8+
from __future__ import print_function, division
9+
from builtins import range
10+
# Note: you may need to update your version of future
11+
# sudo pip install -U future
12+
13+
714
import re
815
import numpy as np
916
import matplotlib.pyplot as plt

linear_regression_class/overfitting.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
33
# https://www.udemy.com/data-science-linear-regression-in-python
44

5+
from __future__ import print_function, division
6+
from builtins import range
7+
# Note: you may need to update your version of future
8+
# sudo pip install -U future
9+
510

611
import numpy as np
712
import matplotlib.pyplot as plt
@@ -10,7 +15,7 @@
1015
def make_poly(X, deg):
1116
n = len(X)
1217
data = [np.ones(n)]
13-
for d in xrange(deg):
18+
for d in range(deg):
1419
data.append(X**(d+1))
1520
return np.vstack(data).T
1621

@@ -53,14 +58,14 @@ def plot_train_vs_test_curves(X, Y, sample=20, max_deg=20):
5358
Xtrain = X[train_idx]
5459
Ytrain = Y[train_idx]
5560

56-
test_idx = [idx for idx in xrange(N) if idx not in train_idx]
61+
test_idx = [idx for idx in range(N) if idx not in train_idx]
5762
# test_idx = np.random.choice(N, sample)
5863
Xtest = X[test_idx]
5964
Ytest = Y[test_idx]
6065

6166
mse_trains = []
6267
mse_tests = []
63-
for deg in xrange(max_deg+1):
68+
for deg in range(max_deg+1):
6469
Xtrain_poly = make_poly(Xtrain, deg)
6570
w = fit(Xtrain_poly, Ytrain)
6671
Yhat_train = Xtrain_poly.dot(w)

linear_regression_class/systolic.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
# X2 = age in years
99
# X3 = weight in pounds
1010

11+
from __future__ import print_function, division
12+
from builtins import range
13+
# Note: you may need to update your version of future
14+
# sudo pip install -U future
15+
16+
17+
1118
import matplotlib.pyplot as plt
1219
import numpy as np
1320
import pandas as pd
@@ -41,9 +48,9 @@ def get_r2(X, Y):
4148
r2 = 1 - d1.dot(d1) / d2.dot(d2)
4249
return r2
4350

44-
print "r2 for x2 only:", get_r2(X2only, Y)
45-
print "r2 for x3 only:", get_r2(X3only, Y)
46-
print "r2 for both:", get_r2(X, Y)
51+
print("r2 for x2 only:", get_r2(X2only, Y))
52+
print("r2 for x3 only:", get_r2(X3only, Y))
53+
print("r2 for both:", get_r2(X, Y))
4754

4855

4956

0 commit comments

Comments
 (0)