Skip to content

Commit

Permalink
py3 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyprogrammer committed Dec 6, 2017
1 parent eee6fee commit 85aea94
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 15 deletions.
8 changes: 7 additions & 1 deletion linear_regression_class/generate_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
# https://www.udemy.com/data-science-linear-regression-in-python

from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future



import numpy as np

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

8 changes: 7 additions & 1 deletion linear_regression_class/generate_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
# https://www.udemy.com/data-science-linear-regression-in-python

from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future



import numpy as np

Expand All @@ -12,5 +18,5 @@
with open('data_2d.csv', 'w') as f:
X = np.random.uniform(low=0, high=100, size=(N,2))
Y = np.dot(X, w) + 1 + np.random.normal(scale=5, size=N)
for i in xrange(N):
for i in range(N):
f.write("%s,%s,%s\n" % (X[i,0], X[i,1], Y[i]))
7 changes: 6 additions & 1 deletion linear_regression_class/generate_poly.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
# https://www.udemy.com/data-science-linear-regression-in-python

from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import numpy as np

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

12 changes: 9 additions & 3 deletions linear_regression_class/gradient_descent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
# https://www.udemy.com/data-science-linear-regression-in-python

from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import numpy as np
import matplotlib.pyplot as plt

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

# print X so you know what it looks like
print "X:", X
print("X:", X)

# won't work!
# w = np.linalg.solve(X.T.dot(X), X.T.dot(Y))
Expand All @@ -23,7 +29,7 @@
costs = [] # keep track of squared error cost
w = np.random.randn(D) / np.sqrt(D) # randomly initialize w
learning_rate = 0.001
for t in xrange(1000):
for t in range(1000):
# update w
Yhat = X.dot(w)
delta = Yhat - Y
Expand All @@ -37,7 +43,7 @@
plt.plot(costs)
plt.show()

print "final w:", w
print("final w:", w)

# plot prediction vs target
plt.plot(Yhat, label='prediction')
Expand Down
10 changes: 8 additions & 2 deletions linear_regression_class/l1_regularization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
# https://www.udemy.com/data-science-linear-regression-in-python

from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import numpy as np
import matplotlib.pyplot as plt

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

print "final w:", w
print("final w:", w)

# plot our w vs true w
plt.plot(true_w, label='true w')
Expand Down
7 changes: 7 additions & 0 deletions linear_regression_class/l2_regularization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
# https://www.udemy.com/data-science-linear-regression-in-python

from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future



import numpy as np
import matplotlib.pyplot as plt

Expand Down
6 changes: 6 additions & 0 deletions linear_regression_class/lr_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
# https://www.udemy.com/data-science-linear-regression-in-python

from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import numpy as np
import matplotlib.pyplot as plt

Expand Down
8 changes: 7 additions & 1 deletion linear_regression_class/lr_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
# https://www.udemy.com/data-science-linear-regression-in-python

from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -42,4 +48,4 @@
d1 = Y - Yhat
d2 = Y - Y.mean()
r2 = 1 - d1.dot(d1) / d2.dot(d2)
print "the r-squared is:", r2
print("the r-squared is:", r2)
7 changes: 7 additions & 0 deletions linear_regression_class/moore.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
# https://www.udemy.com/data-science-linear-regression-in-python
# transistor count from: https://en.wikipedia.org/wiki/Transistor_count

from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import re
import numpy as np
import matplotlib.pyplot as plt
Expand Down
11 changes: 8 additions & 3 deletions linear_regression_class/overfitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
# https://deeplearningcourses.com/c/data-science-linear-regression-in-python
# https://www.udemy.com/data-science-linear-regression-in-python

from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future


import numpy as np
import matplotlib.pyplot as plt
Expand All @@ -10,7 +15,7 @@
def make_poly(X, deg):
n = len(X)
data = [np.ones(n)]
for d in xrange(deg):
for d in range(deg):
data.append(X**(d+1))
return np.vstack(data).T

Expand Down Expand Up @@ -53,14 +58,14 @@ def plot_train_vs_test_curves(X, Y, sample=20, max_deg=20):
Xtrain = X[train_idx]
Ytrain = Y[train_idx]

test_idx = [idx for idx in xrange(N) if idx not in train_idx]
test_idx = [idx for idx in range(N) if idx not in train_idx]
# test_idx = np.random.choice(N, sample)
Xtest = X[test_idx]
Ytest = Y[test_idx]

mse_trains = []
mse_tests = []
for deg in xrange(max_deg+1):
for deg in range(max_deg+1):
Xtrain_poly = make_poly(Xtrain, deg)
w = fit(Xtrain_poly, Ytrain)
Yhat_train = Xtrain_poly.dot(w)
Expand Down
13 changes: 10 additions & 3 deletions linear_regression_class/systolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
# X2 = age in years
# X3 = weight in pounds

from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future



import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -41,9 +48,9 @@ def get_r2(X, Y):
r2 = 1 - d1.dot(d1) / d2.dot(d2)
return r2

print "r2 for x2 only:", get_r2(X2only, Y)
print "r2 for x3 only:", get_r2(X3only, Y)
print "r2 for both:", get_r2(X, Y)
print("r2 for x2 only:", get_r2(X2only, Y))
print("r2 for x3 only:", get_r2(X3only, Y))
print("r2 for both:", get_r2(X, Y))



Expand Down

0 comments on commit 85aea94

Please sign in to comment.