Skip to content

Commit 74c67ce

Browse files
add regularization code and overfitting code for linear and logistic
1 parent c474881 commit 74c67ce

File tree

5 files changed

+191
-12
lines changed

5 files changed

+191
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# notes for this course can be found at:
2+
# https://www.udemy.com/data-science-linear-regression-in-python
3+
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
N = 10
8+
D = 3
9+
X = np.zeros((N, D))
10+
X[:,0] = 1 # bias term
11+
X[:5,1] = 1
12+
X[5:,2] = 1
13+
Y = np.array([0]*5 + [1]*5)
14+
15+
# print X so you know what it looks like
16+
print "X:", X
17+
18+
# won't work!
19+
# w = np.linalg.solve(X.T.dot(X), X.T.dot(Y))
20+
21+
# let's try gradient descent
22+
costs = [] # keep track of squared error cost
23+
w = np.random.randn(D) / np.sqrt(D) # randomly initialize w
24+
learning_rate = 0.001
25+
for t in xrange(1000):
26+
# update w
27+
Yhat = X.dot(w)
28+
delta = Yhat - Y
29+
w = w - learning_rate*X.T.dot(delta)
30+
31+
# find and store the cost
32+
mse = delta.dot(delta) / N
33+
costs.append(mse)
34+
35+
# plot the costs
36+
plt.plot(costs)
37+
plt.show()
38+
39+
print "final w:", w
40+
41+
# plot prediction vs target
42+
plt.plot(Yhat, label='prediction')
43+
plt.plot(Y, label='target')
44+
plt.legend()
45+
plt.show()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# notes for this course can be found at:
2+
# https://www.udemy.com/data-science-linear-regression-in-python
3+
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
N = 50
8+
D = 50
9+
10+
# uniformly distributed numbers between -5, +5
11+
X = (np.random.random((N, D)) - 0.5)*10
12+
13+
# true weights - only the first 3 dimensions of X affect Y
14+
true_w = np.array([1, 0.5, -0.5] + [0]*(D - 3))
15+
16+
# generate Y - add noise with variance 0.5
17+
Y = X.dot(true_w) + np.random.randn(N)*0.5
18+
19+
# perform gradient descent to find w
20+
costs = [] # keep track of squared error cost
21+
w = np.random.randn(D) / np.sqrt(D) # randomly initialize w
22+
learning_rate = 0.001
23+
l1 = 10.0 # Also try 5.0, 2.0, 1.0, 0.1 - what effect does it have on w?
24+
for t in xrange(500):
25+
# update w
26+
Yhat = X.dot(w)
27+
delta = Yhat - Y
28+
w = w - learning_rate*(X.T.dot(delta) + l1*np.sign(w))
29+
30+
# find and store the cost
31+
mse = delta.dot(delta) / N
32+
costs.append(mse)
33+
34+
# plot the costs
35+
plt.plot(costs)
36+
plt.show()
37+
38+
print "final w:", w
39+
40+
# plot our w vs true w
41+
plt.plot(true_w, label='true w')
42+
plt.plot(w, label='w_map')
43+
plt.legend()
44+
plt.show()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# demonstration of L2 regularization
2+
#
3+
# notes for this course can be found at:
4+
# https://www.udemy.com/data-science-linear-regression-in-python
5+
6+
import numpy as np
7+
import matplotlib.pyplot as plt
8+
9+
N = 50
10+
11+
# generate the data
12+
X = np.linspace(0,10,N)
13+
Y = 0.5*X + np.random.randn(N)
14+
15+
# make outliers
16+
Y[-1] += 30
17+
Y[-2] += 30
18+
19+
# plot the data
20+
plt.scatter(X, Y)
21+
plt.show()
22+
23+
# add bias term
24+
X = np.vstack([np.ones(N), X]).T
25+
26+
# plot the maximum likelihood solution
27+
w_ml = np.linalg.solve(X.T.dot(X), X.T.dot(Y))
28+
Yhat_ml = X.dot(w_ml)
29+
plt.scatter(X[:,1], Y)
30+
plt.plot(X[:,1], Yhat_ml)
31+
plt.show()
32+
33+
# plot the regularized solution
34+
# probably don't need an L2 regularization this high in many problems
35+
# everything in this example is exaggerated for visualization purposes
36+
l2 = 1000.0
37+
w_map = np.linalg.solve(l2*np.eye(2) + X.T.dot(X), X.T.dot(Y))
38+
Yhat_map = X.dot(w_map)
39+
plt.scatter(X[:,1], Y)
40+
plt.plot(X[:,1], Yhat_ml, label='maximum likelihood')
41+
plt.plot(X[:,1], Yhat_map, label='map')
42+
plt.legend()
43+
plt.show()

linear_regression_class/overfitting.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@
55
import numpy as np
66
import matplotlib.pyplot as plt
77

8-
# make up some data and plot it
9-
N = 100
10-
X = np.linspace(0, 6*np.pi, N)
11-
Y = np.sin(X)
12-
13-
plt.plot(X, Y)
14-
plt.show()
15-
168

179
def make_poly(X, deg):
1810
n = len(X)
@@ -48,9 +40,6 @@ def fit_and_display(X, Y, sample, deg):
4840
plt.title("deg = %d" % deg)
4941
plt.show()
5042

51-
for deg in (5, 6, 7, 8, 9):
52-
fit_and_display(X, Y, 10, deg)
53-
5443

5544
def get_mse(Y, Yhat):
5645
d = Y - Yhat
@@ -92,4 +81,15 @@ def plot_train_vs_test_curves(X, Y, sample=20, max_deg=20):
9281
plt.legend()
9382
plt.show()
9483

95-
plot_train_vs_test_curves(X, Y)
84+
if __name__ == "__main__":
85+
# make up some data and plot it
86+
N = 100
87+
X = np.linspace(0, 6*np.pi, N)
88+
Y = np.sin(X)
89+
90+
plt.plot(X, Y)
91+
plt.show()
92+
93+
for deg in (5, 6, 7, 8, 9):
94+
fit_and_display(X, Y, 10, deg)
95+
plot_train_vs_test_curves(X, Y)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# notes for this course can be found at:
2+
# https://www.udemy.com/data-science-logistic-regression-in-python
3+
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
def sigmoid(z):
8+
return 1/(1 + np.exp(-z))
9+
10+
N = 50
11+
D = 50
12+
13+
# uniformly distributed numbers between -5, +5
14+
X = (np.random.random((N, D)) - 0.5)*10
15+
16+
# true weights - only the first 3 dimensions of X affect Y
17+
true_w = np.array([1, 0.5, -0.5] + [0]*(D - 3))
18+
19+
# generate Y - add noise with variance 0.5
20+
Y = np.round(sigmoid(X.dot(true_w) + np.random.randn(N)*0.5))
21+
22+
# perform gradient descent to find w
23+
costs = [] # keep track of squared error cost
24+
w = np.random.randn(D) / np.sqrt(D) # randomly initialize w
25+
learning_rate = 0.001
26+
l1 = 3.0 # try different values - what effect does it have on w?
27+
for t in xrange(5000):
28+
# update w
29+
Yhat = sigmoid(X.dot(w))
30+
delta = Yhat - Y
31+
w = w - learning_rate*(X.T.dot(delta) + l1*np.sign(w))
32+
33+
# find and store the cost
34+
cost = -(Y*np.log(Yhat) + (1-Y)*np.log(1 - Yhat)).mean() + l1*np.abs(w).mean()
35+
costs.append(cost)
36+
37+
# plot the costs
38+
plt.plot(costs)
39+
plt.show()
40+
41+
print "final w:", w
42+
43+
# plot our w vs true w
44+
plt.plot(true_w, label='true w')
45+
plt.plot(w, label='w_map')
46+
plt.legend()
47+
plt.show()

0 commit comments

Comments
 (0)