Skip to content

Commit 74ce083

Browse files
authoredNov 10, 2017
Merge pull request manvillej#21 from manvillej/PEP8-update
Pep8 update
2 parents 7150664 + d966fd3 commit 74ce083

28 files changed

+3598
-3014
lines changed
 

‎__pycache__/ex1helper.cpython-36.pyc

1.77 KB
Binary file not shown.

‎__pycache__/ex2helper.cpython-36.pyc

3.9 KB
Binary file not shown.

‎__pycache__/ex3helper.cpython-36.pyc

2.44 KB
Binary file not shown.

‎data/createVocabList.py

+55-59
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,80 @@
1+
'''creatVocabList is used to translate vocabList.txt into a python
2+
dictionary object. It will associate each word with a representational
3+
number in a python dictionary and use the PrettyPrint module to create
4+
a python file from the resulting dictionary to be used in other files.
15
'''
2-
creatVocabList is used to translate vocabList.txt into a python dictionary object.
3-
It will associate each word with a representational number in a python dictionary
4-
and use the PrettyPrint module to create a python file from the resulting dictionary
5-
to be used in other files.
6-
'''
6+
7+
# Imports
78
import os
89
import pprint
910

10-
def main():
11-
pyDict = pyDictCreator('./vocab.txt')
12-
pyDict.creatDict()
13-
pyDict.createPyDict()
1411

12+
def main():
13+
pyDict = pyDictCreator('./vocab.txt')
14+
pyDict.creatDict()
15+
pyDict.createPyDict()
1516

1617

1718
class pyDictCreator():
18-
def __init__(self,filePath):
19-
20-
#if the file path is absolute, make it an absolute path
21-
if(not os.path.isabs(filePath)):
22-
filepath = os.path.abspath(filePath)
23-
24-
25-
#set variables
26-
self.directory = os.path.dirname(filepath)
27-
self.basename = os.path.basename(filepath)
28-
self.filepath = filepath
29-
30-
#sets up empty dictionary
31-
self.dictionary = {}
32-
self.inverseDictionary = {}
33-
34-
def setFilePath(self, filepath):
35-
#if the file path is absolute, make it an absolute path
36-
if(not os.path.isabs(filePath)):
37-
filepath = os.path.abspath(filePath)
38-
19+
def __init__(self, filePath):
3920

40-
#set variables
41-
self.directory = os.path.dirname(filepath)
42-
self.basename = os.path.basename(filepath)
43-
self.filepath = filepath
21+
# if the file path is absolute, make it an absolute path
22+
if(not os.path.isabs(filePath)):
23+
filepath = os.path.abspath(filePath)
4424

45-
#sets up empty dictionary
46-
self.dictionary = {}
25+
# set variables
26+
self.directory = os.path.dirname(filepath)
27+
self.basename = os.path.basename(filepath)
28+
self.filepath = filepath
4729

48-
def creatDict(self):
49-
dictionary = {}
30+
# sets up empty dictionary
31+
self.dictionary = {}
32+
self.inverseDictionary = {}
5033

51-
txtFile = open(self.filepath, 'r')
34+
def setFilePath(self, filepath):
35+
# if the file path is absolute, make it an absolute path
36+
if(not os.path.isabs(filePath)):
37+
filepath = os.path.abspath(filePath)
5238

53-
#get the dictionary words
54-
words = txtFile.readlines()
55-
txtFile.close()
39+
# set variables
40+
self.directory = os.path.dirname(filepath)
41+
self.basename = os.path.basename(filepath)
42+
self.filepath = filepath
5643

57-
#value to start representing numbers
58-
#iterate through words adding them to the dictionary
59-
for word in words:
60-
value = word.split()
61-
dictionary[value[1]] = int(value[0])
44+
# sets up empty dictionary
45+
self.dictionary = {}
6246

63-
self.dictionary = dictionary
47+
def creatDict(self):
48+
dictionary = {}
6449

65-
return dictionary
50+
txtFile = open(self.filepath, 'r')
6651

52+
# get the dictionary words
53+
words = txtFile.readlines()
54+
txtFile.close()
6755

68-
def createPyDict(self):
69-
os.chdir(self.directory)
56+
# value to start representing numbers
57+
# iterate through words adding them to the dictionary
58+
for word in words:
59+
value = word.split()
60+
dictionary[value[1]] = int(value[0])
7061

71-
#open new file with the same name as the original, but with extension .py
72-
name = self.basename.split('.')
73-
name = name[0] + '.py'
74-
pyDict = open(name, 'w')
62+
self.dictionary = dictionary
7563

64+
return dictionary
7665

77-
pyDict.write('dictionary = ' + pprint.pformat(self.dictionary) + '\n')
78-
pyDict.write('dictionary = ' + pprint.pformat(self.dictionary) + '\n')
79-
pyDict.close()
66+
def createPyDict(self):
67+
os.chdir(self.directory)
8068

69+
# open new file with the same name as the original,
70+
# but with extension .py
71+
name = self.basename.split('.')
72+
name = name[0] + '.py'
73+
pyDict = open(name, 'w')
8174

75+
pyDict.write('dictionary = ' + pprint.pformat(self.dictionary) + '\n')
76+
pyDict.write('dictionary = ' + pprint.pformat(self.dictionary) + '\n')
77+
pyDict.close()
8278

8379
if __name__ == '__main__':
84-
main()
80+
main()

‎ex1.py

+167-130
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
## Machine Learning Online Class - Exercise 1: Linear Regression
2-
3-
# Instructions
4-
# ------------
5-
#
6-
# This is the initialization file for exercise 1 of Andrew Ng's Machine learning course
7-
# All of the file have been converted into a python implementation instead of the original
8-
# Matlab implementation. This
9-
#
10-
# warmUpExercise.py - complete
11-
# plotData.py - complete
12-
# gradientDescent.py - complete
13-
# computeCost.py - complete
14-
# gradientDescentMulti.py - complete
15-
# computeCostMulti.py - complete
16-
# featureNormalize.py - complete
17-
# normalEqn.py - complete
18-
#
19-
# x refers to the population size in 10,000s
20-
# y refers to the profit in $10,000s
21-
#
22-
#
23-
## ==================== Part 1: Basic Function ====================
24-
# Complete warmUpExercise.py
1+
""" Machine Learning Online Class - Exercise 1: Linear Regression
2+
Instructions
3+
------------
4+
This is the initialization file for exercise 1
5+
of Andrew Ng's Machine learning course. All of
6+
the file have been converted into a python
7+
implementation instead of the original.
8+
Matlab implementation.
9+
warmUpExercise - Complete
10+
plotData - Complete
11+
gradientDescent - Complete
12+
computeCost - Complete
13+
gradientDescentMulti - Complete
14+
computeCostMulti - Complete
15+
featureNormalize - Complete
16+
normalEqn - Complete
17+
refers to the population size in 10,000s
18+
refers to the profit in $10,000s
19+
"""
20+
21+
# Imports
2522
import numpy as np
2623
import ex1helper as helper
2724
from mpl_toolkits.mplot3d import Axes3D
@@ -30,109 +27,149 @@
3027
import matplotlib
3128

3229

33-
print("running warmUpExercise...")
34-
print('5x5 Identity Matrix:')
35-
36-
eye = np.identity(5)
37-
print(eye)
38-
39-
input('\nPart 1 completed. Program paused. Press enter to continue: ')
40-
41-
## ======================= Part 2: Plotting =======================
42-
print('Plotting Data ...')
43-
data = np.genfromtxt('./data/ex1data1.txt', delimiter=',')
44-
45-
x=np.array(data[:,0])
46-
x=np.expand_dims(x,axis=0)
47-
x=np.append(np.ones_like(x),x,axis=0)
48-
y=np.array(data[:,1])
49-
50-
plt.scatter(x[1], y, label = "scatter", marker='x', color='r', s=10)
51-
plt.xlabel('Population of City in 10,000s')
52-
plt.ylabel('Profit in $10,000s')
53-
plt.title('Raw Data')
54-
plt.show()
55-
input('\nPart 2 completed. Program paused. Press enter to continue: ')
56-
57-
## =================== Part 3: Cost and Gradient descent ===================
58-
theta = np.zeros(x.shape[0])
59-
60-
# Some gradient descent settings
61-
iterations = 1500;
62-
alpha = 0.01;
63-
64-
print('Testing the cost function ...')
65-
# compute and display initial cost
66-
J = helper.computeCost(x,y,theta)
67-
print("With theta = [0 0], \nCost computed = ", J)
68-
print("Expected cost value (approx) 32.07")
69-
70-
71-
# further testing of the cost function
72-
J = helper.computeCost(x, y, [-1, 2]);
73-
print("With theta = [-1 2], \nCost computed = ", J)
74-
print('Expected cost value (approx) 54.24');
75-
76-
input('\n Program paused. Press enter to continue: ')
77-
78-
print('Running Gradient Descent...')
79-
#run gradient descent
80-
theta, cost = helper.gradientDescent(x, y, theta, alpha, iterations)
81-
82-
#print theta to screen
83-
print('Theta found by gradient descent:');
84-
print(theta)
85-
print('\nExpected theta values (approx):');
86-
print('[-3.6303 1.1664]');
87-
88-
# Plot the linear fit
89-
plt.scatter(x[1], y, label = "scatter", marker='x', color='r', s=10)
90-
plt.plot(x[1],np.matmul(x.transpose(),theta), color='blue', linestyle='solid')
91-
plt.xlabel('Population of City in 10,000s')
92-
plt.ylabel('Profit in $10,000s')
93-
plt.title('Raw Data + Linear Fit')
94-
plt.show()
95-
96-
# Predict values for population sizes of 35,000 and 70,000
97-
predict1 = np.matmul([1, 3.5],theta)
98-
print('For population = 35,000, we predict a profit of ', predict1*10000);
99-
predict2 = np.matmul([1, 7],theta)
100-
print('For population = 70,000, we predict a profit of ', predict2*10000);
101-
102-
input('\nPart 3 completed. Program paused. Press enter to continue: ')
103-
104-
# ============= Part 4: Visualizing J(theta_0, theta_1) =============
105-
print('Visualizing J(theta_0, theta_1) ...')
106-
107-
108-
#Grid over which we will calculate J
109-
110-
theta0 = np.linspace(-10, 10, 100)
111-
theta1 = np.linspace(-1, 4, 100)
112-
theta0Vals, theta1Vals = np.meshgrid(theta0,theta1)
113-
zs = np.array([helper.computeCost(x,y,[i,j]) for i,j in zip(np.ravel(theta0Vals), np.ravel(theta1Vals))])
114-
ZCosts = zs.reshape(theta0Vals.shape)
115-
116-
min = np.amin(ZCosts)
117-
max = np.amax(ZCosts)
118-
norm = matplotlib.colors.Normalize(vmin=min, vmax=max, clip=True)
119-
120-
121-
122-
fig = plt.figure(1)
123-
ax = fig.add_subplot(111, projection='3d')
124-
125-
126-
ax.plot_surface(theta0Vals,theta1Vals,ZCosts,cmap=cm.coolwarm, norm=norm)
127-
128-
129-
ax.set_xlabel('theta0')
130-
ax.set_ylabel('theta1')
131-
ax.set_zlabel('Cost')
132-
133-
plt.figure(2)
134-
CS = plt.contour(theta0Vals, theta1Vals, ZCosts, np.logspace(-2,3,20))
135-
plt.scatter(theta[0], theta[1], label = "scatter", marker='x', color='r', s=10)
136-
plt.clabel(CS, inline=1, fontsize=10)
137-
plt.title('Simplest default with labels')
138-
plt.show()
30+
def main():
31+
32+
# ==================== Part 1: Basic Function ====================
33+
# Complete warmUpExercise.py
34+
print("running warmUpExercise...")
35+
print('5x5 Identity Matrix:')
36+
37+
eye = np.identity(5)
38+
print(eye)
39+
40+
input('\nPart 1 completed. Program paused. Press enter to continue: ')
41+
42+
# ======================= Part 2: Plotting =======================
43+
print('Plotting Data ...')
44+
data = np.genfromtxt('./data/ex1data1.txt', delimiter=',')
45+
46+
x = np.array(data[:, 0])
47+
x = np.expand_dims(x, axis=0)
48+
x = np.append(np.ones_like(x), x, axis=0)
49+
y = np.array(data[:, 1])
50+
51+
plt.scatter(x[1], y, label="scatter", marker='x', color='r', s=10)
52+
plt.xlabel('Population of City in 10,000s')
53+
plt.ylabel('Profit in $10,000s')
54+
plt.title('Raw Data')
55+
plt.show()
56+
input('\nPart 2 completed. Program paused. Press enter to continue: ')
57+
58+
# =================== Part 3: Cost and Gradient descent ===================
59+
theta = np.zeros(x.shape[0])
60+
61+
# Some gradient descent settings
62+
iterations = 1500
63+
alpha = 0.01
64+
65+
print('Testing the cost function ...')
66+
# compute and display initial cost
67+
J = helper.computeCost(x, y, theta)
68+
print("With theta = [0 0], \nCost computed = ", J)
69+
print("Expected cost value (approx) 32.07")
70+
71+
# further testing of the cost function
72+
J = helper.computeCost(x, y, [-1, 2])
73+
print("With theta = [-1 2], \nCost computed = ", J)
74+
print('Expected cost value (approx) 54.24')
75+
76+
input('\n Program paused. Press enter to continue: ')
77+
78+
print('Running Gradient Descent...')
79+
# run gradient descent
80+
theta, cost = helper.gradientDescent(x, y, theta, alpha, iterations)
81+
82+
# print theta to screen
83+
print('Theta found by gradient descent:')
84+
print(theta)
85+
print('\nExpected theta values (approx):')
86+
print('[-3.6303 1.1664]')
87+
88+
# Plot the linear fit
89+
plt.scatter(
90+
x[1],
91+
y,
92+
label="scatter",
93+
marker='x',
94+
color='r',
95+
s=10)
96+
97+
plt.plot(
98+
x[1],
99+
np.matmul(
100+
x.transpose(),
101+
theta),
102+
color='blue',
103+
linestyle='solid')
104+
105+
plt.xlabel('Population of City in 10,000s')
106+
plt.ylabel('Profit in $10,000s')
107+
plt.title('Raw Data + Linear Fit')
108+
plt.show()
109+
110+
# Predict values for population sizes of 35,000 and 70,000
111+
predict1 = np.matmul([1, 3.5], theta)
112+
print('For population = 35,000, we predict a profit of ', predict1*10000)
113+
predict2 = np.matmul([1, 7], theta)
114+
print('For population = 70,000, we predict a profit of ', predict2*10000)
115+
116+
input('\nPart 3 completed. Program paused. Press enter to continue: ')
117+
118+
# ============= Part 4: Visualizing J(theta_0, theta_1) =============
119+
print('Visualizing J(theta_0, theta_1) ...')
120+
121+
# Grid over which we will calculate J
122+
123+
theta0 = np.linspace(-10, 10, 100)
124+
theta1 = np.linspace(-1, 4, 100)
125+
theta0Vals, theta1Vals = np.meshgrid(theta0, theta1)
126+
zs = np.array(
127+
[helper.computeCost(x, y, [i, j]) for i, j in zip(
128+
np.ravel(theta0Vals),
129+
np.ravel(theta1Vals))])
130+
131+
ZCosts = zs.reshape(theta0Vals.shape)
132+
133+
min = np.amin(ZCosts)
134+
max = np.amax(ZCosts)
135+
norm = matplotlib.colors.Normalize(vmin=min, vmax=max, clip=True)
136+
137+
fig = plt.figure(1)
138+
ax = fig.add_subplot(111, projection='3d')
139+
140+
ax.plot_surface(
141+
theta0Vals,
142+
theta1Vals,
143+
ZCosts,
144+
cmap=cm.coolwarm,
145+
norm=norm)
146+
147+
ax.set_xlabel('theta0')
148+
ax.set_ylabel('theta1')
149+
ax.set_zlabel('Cost')
150+
151+
plt.figure(2)
152+
CS = plt.contour(
153+
theta0Vals,
154+
theta1Vals,
155+
ZCosts,
156+
np.logspace(-2, 3, 20))
157+
158+
plt.scatter(
159+
theta[0],
160+
theta[1],
161+
label="scatter",
162+
marker='x',
163+
color='r',
164+
s=10)
165+
166+
plt.clabel(
167+
CS,
168+
inline=1,
169+
fontsize=10)
170+
171+
plt.title('Simplest default with labels')
172+
plt.show
173+
174+
if __name__ == '__main__':
175+
main()

‎ex1_multi.py

+158-154
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,163 @@
1-
## Machine Learning Online Class
2-
# Exercise 1: Linear regression with multiple variables
3-
#
4-
# Instructions
5-
# ------------
6-
#
7-
# This file contains code that helps you get started on the
8-
# linear regression exercise.
9-
#
10-
# You will need to complete the following functions in this
11-
# exericse:
12-
#
13-
# warmUpExercise.py - complete
14-
# plotData.py - complete
15-
# gradientDescent.py - complete
16-
# computeCost.py - complete
17-
# gradientDescentMulti.py - complete
18-
# computeCostMulti.py - complete
19-
# featureNormalize.py - complete
20-
# normalEqn.py - complete
21-
#
22-
# For this part of the exercise, you will need to change some
23-
# parts of the code below for various experiments (e.g., changing
24-
# learning rates).
25-
#
26-
#
27-
## Initialization
28-
#
29-
## ================ Part 1: Feature Normalization ================
30-
#
31-
## Clear and Close Figures
1+
"""Machine Learning Online Class Exercise 1: Linear regression with multiple variables
2+
Instructions
3+
------------
4+
This file contains code that helps you get started on the
5+
linear regression exercise.
6+
You will need to complete the following functions in this
7+
exericse:
8+
warmUpExercise - Complete
9+
plotData - Complete
10+
gradientDescent - Complete
11+
computeCost - Complete
12+
gradientDescentMulti - Complete
13+
computeCostMulti - Complete
14+
featureNormalize - Complete
15+
normalEqn - Complete
16+
For this part of the exercise, you will need to change some
17+
parts of the code below for various experiments (e.g., changing
18+
learning rates).
19+
"""
20+
21+
# imports
3222
import numpy as np
3323
import ex1helper as helper
3424
import matplotlib.pyplot as plt
3525

3626

37-
38-
print('Loading data...')
39-
data = np.genfromtxt('./data/ex1data2.txt', delimiter=',')
40-
x = np.array(data[:,:2])
41-
y = np.array(data[:,2])
42-
m = y.shape[0]
43-
44-
45-
# Print out some data points
46-
print('First 10 examples from the dataset: ')
47-
for i in range(0,10):
48-
print("x = [%.0f %.0f], y = %.0f" % (x[i,0], x[i,1], y[i]))
49-
50-
input('Program paused. Press enter to continue: ')
51-
52-
print('\nNormalize Features...')
53-
54-
x,mu,sigma = helper.featureNormalize(x)
55-
56-
57-
#add bias unit
58-
r = x
59-
x = np.ones((x.shape[0], x.shape[1]+1))
60-
x[:,1:] = r
61-
62-
63-
## ================ Part 2: Gradient Descent ================
64-
65-
# ====================== YOUR CODE HERE ======================
66-
# Instructions: We have provided you with the following starter
67-
# code that runs gradient descent with a particular
68-
# learning rate (alpha).
69-
#
70-
# Your task is to first make sure that your functions -
71-
# computeCost and gradientDescent already work with
72-
# this starter code and support multiple variables.
73-
#
74-
# After that, try running gradient descent with
75-
# different values of alpha and see which one gives
76-
# you the best result.
77-
#
78-
# Finally, you should complete the code at the end
79-
# to predict the price of a 1650 sq-ft, 3 br house.
80-
#
81-
# Hint: By using the 'hold on' command, you can plot multiple
82-
# graphs on the same figure.
83-
#
84-
# Hint: At prediction, make sure you do the same feature normalization.
85-
#
86-
print('\n\nPart 1 complete.')
87-
print('\nRunning gradient descent ...');
88-
89-
# Choose some alpha value
90-
alpha = 0.01
91-
num_iters = 400
92-
93-
# Init Theta and Run Gradient Descent
94-
theta = np.zeros(3);
95-
[theta, J_history] = helper.gradientDescentMulti(x, y, theta, alpha, num_iters);
96-
plt.plot(range(0,num_iters), J_history, color='blue', linestyle='solid')
97-
plt.xlabel('iterations')
98-
plt.ylabel('Cost J')
99-
plt.show()
100-
print('Theta computed from gradient descent:', theta)
101-
102-
# Estimate the price of a 1650 sq-ft, 3 br house
103-
# ====================== YOUR CODE HERE ======================
104-
# Recall that the first column of X is all-ones. Thus, it does
105-
# not need to be normalized.
106-
107-
108-
r = np.array([1650, 3])
109-
r = (r - mu)/sigma
110-
r2 = np.ones(r.shape[0]+1)
111-
r2[1:] = r
112-
r = r2
113-
price = np.matmul(r,theta)
114-
115-
# ============================================================
116-
print('\nPredicted price of a 1650 sq-ft, 3 br house (using gradient descent): ${0:.2f}'.format(price))
117-
118-
input('\nPart 2 complete. Program paused. Press enter to continue: ')
119-
120-
## ================ Part 3: Normal Equations ================
121-
122-
print('Solving with normal equations...');
123-
124-
# ====================== YOUR CODE HERE ======================
125-
# Instructions: The following code computes the closed form
126-
# solution for linear regression using the normal
127-
# equations. You should complete the code in
128-
# normalEqn.m
129-
#
130-
# After doing so, you should complete this code
131-
# to predict the price of a 1650 sq-ft, 3 br house.
132-
#
133-
134-
## Load Data
135-
data = np.genfromtxt('./data/ex1data2.txt', delimiter=',')
136-
x = np.array(data[:,:2])
137-
y = np.array(data[:,2])
138-
m = y.shape[0]
139-
140-
# Add intercept term to X
141-
r = x
142-
x = np.ones((x.shape[0], x.shape[1]+1))
143-
x[:,1:] = r
144-
145-
# Calculate the parameters from the normal equation
146-
theta = helper.normalEqn(x, y)
147-
148-
# Display normal equation's result
149-
print('Theta computed from the normal equations: ', theta)
150-
151-
152-
# Estimate the price of a 1650 sq-ft, 3 br house
153-
# ====================== YOUR CODE HERE ======================
154-
155-
r = np.array([1, 1650, 3])
156-
price = np.matmul(r,theta)
157-
158-
# ============================================================
159-
print('Predicted price of a 1650 sq-ft, 3 br house (using normal equations): ${0:.2f}'.format(price))
27+
def main():
28+
# ================ Part 1: Feature Normalization ================
29+
#
30+
# Clear and Close Figures
31+
32+
print('Loading data...')
33+
data = np.genfromtxt('./data/ex1data2.txt', delimiter=',')
34+
x = np.array(data[:, :2])
35+
y = np.array(data[:, 2])
36+
m = y.shape[0]
37+
38+
# Print out some data points
39+
print('First 10 examples from the dataset: ')
40+
for i in range(0, 10):
41+
print("x = [%.0f %.0f], y = %.0f" % (x[i, 0], x[i, 1], y[i]))
42+
43+
input('Program paused. Press enter to continue: ')
44+
45+
print('\nNormalize Features...')
46+
47+
x, mu, sigma = helper.featureNormalize(x)
48+
49+
# add bias unit
50+
r = x
51+
x = np.ones((x.shape[0], x.shape[1]+1))
52+
x[:, 1:] = r
53+
54+
# ================ Part 2: Gradient Descent ================
55+
56+
# ====================== YOUR CODE HERE ======================
57+
# Instructions: We have provided you with the following starter
58+
# code that runs gradient descent with a particular
59+
# learning rate (alpha).
60+
#
61+
# Your task is to first make sure that your functions -
62+
# computeCost and gradientDescent already work with
63+
# this starter code and support multiple variables.
64+
#
65+
# After that, try running gradient descent with
66+
# different values of alpha and see which one gives
67+
# you the best result.
68+
#
69+
# Finally, you should complete the code at the end
70+
# to predict the price of a 1650 sq-ft, 3 br house.
71+
#
72+
# Hint: By using the 'hold on' command, you can plot multiple
73+
# graphs on the same figure.
74+
#
75+
# Hint: At prediction, make sure you do the same feature normalization.
76+
#
77+
print('\n\nPart 1 complete.')
78+
print('\nRunning gradient descent ...')
79+
80+
# Choose some alpha value
81+
alpha = 0.01
82+
num_iters = 400
83+
84+
# Init Theta and Run Gradient Descent
85+
theta = np.zeros(3)
86+
87+
[theta, J_history] = helper.gradientDescentMulti(
88+
x,
89+
y,
90+
theta,
91+
alpha,
92+
num_iters)
93+
94+
plt.plot(
95+
range(0, num_iters),
96+
J_history,
97+
color='blue',
98+
linestyle='solid')
99+
100+
plt.xlabel('iterations')
101+
plt.ylabel('Cost J')
102+
plt.show()
103+
print('Theta computed from gradient descent:', theta)
104+
105+
# Estimate the price of a 1650 sq-ft, 3 br house
106+
# ====================== YOUR CODE HERE ======================
107+
# Recall that the first column of X is all-ones. Thus, it does
108+
# not need to be normalized.
109+
110+
r = np.array([1650, 3])
111+
r = (r - mu)/sigma
112+
r2 = np.ones(r.shape[0]+1)
113+
r2[1:] = r
114+
r = r2
115+
price = np.matmul(r, theta)
116+
117+
# ============================================================
118+
print('\nPredicted price of a 1650 sq-ft, 3 br house (using gradient descent): ${0:.2f}'.format(price))
119+
120+
input('\nPart 2 complete. Program paused. Press enter to continue: ')
121+
122+
# ================ Part 3: Normal Equations ================
123+
124+
print('Solving with normal equations...')
125+
126+
# ====================== YOUR CODE HERE ======================
127+
# Instructions: The following code computes the closed form
128+
# solution for linear regression using the normal
129+
# equations. You should complete the code in
130+
# normalEqn.m
131+
#
132+
# After doing so, you should complete this code
133+
# to predict the price of a 1650 sq-ft, 3 br house.
134+
#
135+
136+
# Load Data
137+
data = np.genfromtxt('./data/ex1data2.txt', delimiter=',')
138+
x = np.array(data[:, :2])
139+
y = np.array(data[:, 2])
140+
m = y.shape[0]
141+
142+
# Add intercept term to X
143+
r = x
144+
x = np.ones((x.shape[0], x.shape[1]+1))
145+
x[:, 1:] = r
146+
147+
# Calculate the parameters from the normal equation
148+
theta = helper.normalEqn(x, y)
149+
150+
# Display normal equation's result
151+
print('Theta computed from the normal equations: ', theta)
152+
153+
# Estimate the price of a 1650 sq-ft, 3 br house
154+
# ====================== YOUR CODE HERE ======================
155+
156+
r = np.array([1, 1650, 3])
157+
price = np.matmul(r, theta)
158+
159+
# ============================================================
160+
print('Predicted price of a 1650 sq-ft, 3 br house (using normal equations): ${0:.2f}'.format(price))
161+
162+
if __name__ == '__main__':
163+
main()

‎ex1helper.py

+41-37
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,59 @@
11
import numpy as np
22
from numpy.linalg import inv
33

4-
def computeCost(x,y,theta):
5-
6-
j = 1/2*np.mean(np.power(np.matmul(x.transpose(),theta)-y,2))
74

8-
return j
5+
def computeCost(x, y, theta):
6+
7+
j = 1/2*np.mean(np.power(np.matmul(x.transpose(), theta)-y, 2))
8+
9+
return j
910

1011

1112
def gradientDescent(x, y, theta, alpha, iterations):
12-
m = x.shape[1]
13-
costHistory = np.zeros(iterations)
14-
for i in range(iterations):
13+
m = x.shape[1]
14+
costHistory = np.zeros(iterations)
15+
for i in range(iterations):
16+
17+
error = np.matmul(x.transpose(), theta)-y
18+
gradient = np.dot(x, error)
1519

16-
error = np.matmul(x.transpose(),theta)-y
17-
gradient = np.dot(x,error)
18-
19-
theta = theta - alpha*gradient/m
20-
costHistory[i]= computeCost(x,y,theta)
20+
theta = theta - alpha*gradient/m
21+
costHistory[i] = computeCost(x, y, theta)
2122

22-
return [theta, costHistory]
23+
return [theta, costHistory]
2324

24-
def normalize(x,mu,std):
25-
return (x - mu)/std
25+
26+
def normalize(x, mu, std):
27+
return (x - mu)/std
2628

2729

2830
def featureNormalize(x):
29-
vfunc = np.vectorize(normalize)
30-
mu = np.mean(x,axis=0)
31-
sigma = np.std(x,axis=0)
32-
x_norm = vfunc(x,mu,sigma)
31+
vfunc = np.vectorize(normalize)
32+
mu = np.mean(x, axis=0)
33+
sigma = np.std(x, axis=0)
34+
x_norm = vfunc(x, mu, sigma)
35+
36+
return[x_norm, mu, sigma]
3337

34-
return[x_norm, mu, sigma]
3538

3639
def computeCostMulti(x, y, theta):
37-
m=y.shape[0]
38-
j = np.sum(np.power(np.matmul(x,theta)-y,2))/(2*m)
39-
return j
40+
m = y.shape[0]
41+
j = np.sum(np.power(np.matmul(x, theta)-y, 2))/(2*m)
42+
return j
43+
4044

4145
def gradientDescentMulti(x, y, theta, alpha, num_iters):
42-
m = y.shape[0]
43-
j_history = np.zeros(num_iters)
44-
for i in range(0,num_iters):
45-
error = np.matmul(x,theta)-y
46-
theta = theta - alpha*np.dot(error,x)/m
47-
j_history[i] = computeCostMulti(x,y,theta)
48-
49-
return [theta, j_history]
50-
51-
52-
def normalEqn(X,Y):
53-
X= np.matmul(inv(np.matmul(X.transpose(),X)),X.transpose())
54-
theta = np.matmul(X,Y)
55-
return theta
46+
m = y.shape[0]
47+
j_history = np.zeros(num_iters)
48+
for i in range(0, num_iters):
49+
error = np.matmul(x, theta)-y
50+
theta = theta - alpha*np.dot(error, x)/m
51+
j_history[i] = computeCostMulti(x, y, theta)
52+
53+
return [theta, j_history]
54+
55+
56+
def normalEqn(X, Y):
57+
X = np.matmul(inv(np.matmul(X.transpose(), X)), X.transpose())
58+
theta = np.matmul(X, Y)
59+
return theta

‎ex2.py

+122-127
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,127 @@
1-
## Machine Learning Online Class - Exercise 2: Logistic Regression
2-
#
3-
# Instructions
4-
# ------------
5-
#
6-
# This file contains code that helps you get started on the logistic
7-
# regression exercise. You will need to complete the following functions
8-
# in this exericse:
9-
#
10-
# sigmoid - complete
11-
# costFunction - complete
12-
# predict - complete
13-
# costFunctionReg - complete
14-
#
15-
# For this exercise, you will not need to change any code in this file,
16-
# or any other files other than those mentioned above.
17-
#
18-
19-
## Initialization
20-
21-
## Load Data
22-
# The first two columns contains the exam scores and the third column
23-
# contains the label.
1+
""" Machine Learning Online Class - Exercise 2: Logistic Regression
2+
Instructions
3+
-----------
4+
This file contains code that helps you get started on the logistic
5+
regression exercise. You will need to complete the following functions
6+
in this exericse:
7+
sigmoid - complete
8+
costFunction - complete
9+
predict - complete
10+
costFunctionReg - complete
11+
For this exercise, you will not need to change any code in this file,
12+
or any other files other than those mentioned above.
13+
"""
14+
15+
# Imports
2416
import numpy as np
2517
import ex2helper as helper
2618
import matplotlib.pyplot as plt
27-
data = np.genfromtxt('./data/ex2data1.txt', delimiter=',')
28-
y = np.array(data[:,2])
29-
x = np.array(data[:,0:2])
3019

3120

32-
## ==================== Part 1: Plotting ====================
33-
# We start the exercise by first plotting the data to understand the
34-
# the problem we are working with.
35-
[m,n] = x.shape
36-
37-
r = x
38-
x = np.ones((m, n+1))
39-
x[:,1:] = r
40-
41-
print('\nPlotting data with \'o\' indicating (y = 1) examples and \'x\' indicating (y = 0) examples.')
42-
43-
helper.plotData(x,y)
44-
plt.xlabel('Exam Score 1')
45-
plt.ylabel('Exam Score 2')
46-
plt.show()
47-
48-
input('\nPart 1 completed. Program paused. Press enter to continue: ')
49-
## ============ Part 2: Compute Cost and Gradient ============
50-
# In this part of the exercise, you will implement the cost and gradient
51-
# for logistic regression. You neeed to complete the code in
52-
# costFunction.m
53-
#
54-
# Setup the data matrix appropriately, and add ones for the intercept term
55-
56-
57-
theta = np.zeros(n+1)
58-
59-
cost = helper.costFunction(theta,x,y)
60-
grad = helper.gradient(theta, x, y)
61-
62-
63-
print('Cost at initial theta (zeros): {0:.3f}'.format(cost))
64-
print('Expected cost (approx): 0.693')
65-
print('Gradient at initial theta (zeros): ')
66-
print(grad)
67-
print('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628')
68-
69-
70-
# Compute and display cost and gradient with non-zero theta
71-
test_theta = np.array([-24, 0.2, 0.2])
72-
cost = helper.costFunction(test_theta, x, y)
73-
grad = helper.gradient(test_theta, x, y)
74-
75-
print('Cost at initial theta (zeros): {0:.3f}'.format(cost))
76-
print('Expected cost (approx): 0.218')
77-
print('Gradient at initial theta (zeros): ')
78-
print(grad)
79-
print('Expected gradients (approx):\n 0.043\n 2.566\n 2.647')
80-
81-
input('\nPart 2 completed. Program paused. Press enter to continue: ')
82-
83-
## ============= Part 3: Optimizing using fminunc =============
84-
# In this exercise, you will use a built-in function (fminunc) to find the
85-
# optimal parameters theta.
86-
87-
# Set options for fminunc
88-
89-
results = helper.optimize(theta,x,y)
90-
theta = results.x
91-
cost = results.fun
92-
93-
# Print theta to screen
94-
print('Cost at theta found by scipy.optimize.minimize with TNC: {0:.3f}'.format(cost))
95-
print('Expected cost (approx): 0.203')
96-
print('theta:')
97-
print(theta)
98-
print('Expected theta (approx):')
99-
print('[ -25.161 0.206 0.201]')
100-
helper.plotDecisionBoundary(theta,x,y)
101-
plt.xlabel('Exam Score 1')
102-
plt.ylabel('Exam Score 2')
103-
plt.show()
104-
105-
input('\nPart 3 completed. Program paused. Press enter to continue: ')
106-
107-
108-
## ============== Part 4: Predict and Accuracies ==============
109-
# After learning the parameters, you'll like to use it to predict the outcomes
110-
# on unseen data. In this part, you will use the logistic regression model
111-
# to predict the probability that a student with score 45 on exam 1 and
112-
# score 85 on exam 2 will be admitted.
113-
#
114-
# Furthermore, you will compute the training and test set accuracies of
115-
# our model.
116-
#
117-
# Your task is to complete the code in predict.m
118-
# Predict probability for a student with score 45 on exam 1
119-
# and score 85 on exam 2
120-
121-
prob = helper.sigmoid(np.matmul(np.array([1, 45, 85]), theta))
122-
print('For a student with scores 45 and 85, we predict an admission probability of ', prob)
123-
print('Expected value: 0.775 +/- 0.002');
124-
125-
# Compute accuracy on our training set
126-
p = helper.predict(theta, x)
127-
predictions = np.zeros(p.shape)
128-
predictions[np.where(p==y)] = 1
129-
130-
131-
print('Train Accuracy: ', np.mean(predictions) * 100)
132-
print('Expected accuracy (approx): 89.0\n')
21+
def main():
22+
# Load Data
23+
# The first two columns contains the exam scores and the third column
24+
# contains the label.
25+
data = np.genfromtxt('./data/ex2data1.txt', delimiter=',')
26+
y = np.array(data[:, 2])
27+
x = np.array(data[:, 0:2])
28+
29+
# ==================== Part 1: Plotting ====================
30+
# We start the exercise by first plotting the data to understand the
31+
# the problem we are working with.
32+
[m, n] = x.shape
33+
34+
r = x
35+
x = np.ones((m, n+1))
36+
x[:, 1:] = r
37+
38+
print('\nPlotting data with \'o\' indicating (y = 1) examples and \'x\' indicating (y = 0) examples.')
39+
40+
helper.plotData(x, y)
41+
plt.xlabel('Exam Score 1')
42+
plt.ylabel('Exam Score 2')
43+
plt.show()
44+
45+
input('\nPart 1 completed. Program paused. Press enter to continue: ')
46+
# ============ Part 2: Compute Cost and Gradient ============
47+
# In this part of the exercise, you will implement the cost and gradient
48+
# for logistic regression. You neeed to complete the code in
49+
# costFunction.m
50+
#
51+
# Setup the data matrix appropriately, and add ones for the intercept term
52+
53+
theta = np.zeros(n+1)
54+
55+
cost = helper.costFunction(theta, x, y)
56+
grad = helper.gradient(theta, x, y)
57+
58+
print('Cost at initial theta (zeros): {0:.3f}'.format(cost))
59+
print('Expected cost (approx): 0.693')
60+
print('Gradient at initial theta (zeros): ')
61+
print(grad)
62+
print('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628')
63+
64+
# Compute and display cost and gradient with non-zero theta
65+
test_theta = np.array([-24, 0.2, 0.2])
66+
cost = helper.costFunction(test_theta, x, y)
67+
grad = helper.gradient(test_theta, x, y)
68+
69+
print('Cost at initial theta (zeros): {0:.3f}'.format(cost))
70+
print('Expected cost (approx): 0.218')
71+
print('Gradient at initial theta (zeros): ')
72+
print(grad)
73+
print('Expected gradients (approx):\n 0.043\n 2.566\n 2.647')
74+
75+
input('\nPart 2 completed. Program paused. Press enter to continue: ')
76+
77+
# ============= Part 3: Optimizing using fminunc =============
78+
# In this exercise, you will use a built-in function (fminunc) to find the
79+
# optimal parameters theta.
80+
81+
# Set options for fminunc
82+
83+
results = helper.optimize(theta, x, y)
84+
theta = results.x
85+
cost = results.fun
86+
87+
# Print theta to screen
88+
print('Cost at theta found by scipy.optimize.minimize with TNC: {0:.3f}'.format(cost))
89+
print('Expected cost (approx): 0.203')
90+
print('theta:')
91+
print(theta)
92+
print('Expected theta (approx):')
93+
print('[ -25.161 0.206 0.201]')
94+
helper.plotDecisionBoundary(theta, x, y)
95+
plt.xlabel('Exam Score 1')
96+
plt.ylabel('Exam Score 2')
97+
plt.show()
98+
99+
input('\nPart 3 completed. Program paused. Press enter to continue: ')
100+
101+
# ============== Part 4: Predict and Accuracies ==============
102+
# After learning the parameters, you'll like to use it to
103+
# predict the outcomes on unseen data. In this part, you will
104+
# use the logistic regression model to predict the probability
105+
# that a student with score 45 on exam 1 and score 85 on exam 2
106+
# will be admitted
107+
# Furthermore, you will compute the training and test set accuracies
108+
# of our model
109+
# Your task is to complete the code in predict.m
110+
# Predict probability for a student with score 45 on exam 1
111+
# and score 85 on exam 2
112+
113+
prob = helper.sigmoid(np.matmul(np.array([1, 45, 85]), theta))
114+
print('For a student with scores 45 and 85,')
115+
print('We predict an admission probability of ', prob)
116+
print('Expected value: 0.775 +/- 0.002')
117+
118+
# Compute accuracy on our training set
119+
p = helper.predict(theta, x)
120+
predictions = np.zeros(p.shape)
121+
predictions[np.where(p == y)] = 1
122+
123+
print('Train Accuracy: ', np.mean(predictions) * 100)
124+
print('Expected accuracy (approx): 89.0\n')
125+
126+
if __name__ == '__main__':
127+
main()

‎ex2_reg.py

+125-121
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,129 @@
1-
## Machine Learning Online Class - Exercise 2: Logistic Regression
2-
#
3-
# Instructions
4-
# ------------
5-
#
6-
# This file contains code that helps you get started on the second part
7-
# of the exercise which covers regularization with logistic regression.
8-
#
9-
# You will need to complete the following functions in this exericse:
10-
#
11-
# sigmoid.m - complete
12-
# costFunction.m - complete
13-
# predict.m - complete
14-
# costFunctionReg.m - complete
15-
#
16-
# For this exercise, you will not need to change any code in this file,
17-
# or any other files other than those mentioned above.
18-
#
19-
20-
## Initialization
21-
22-
## Load Data
23-
# The first two columns contains the X values and the third column
24-
# contains the label (y).
1+
""" Machine Learning Online Class - Exercise 2: Logistic Regression
2+
Instructions
3+
------------
4+
This file contains code that helps you get started on the second part
5+
of the exercise which covers regularization with logistic regression.
6+
You will need to complete the following functions in this exericse:
7+
sigmoid - complete
8+
costFunction - complete
9+
predict - complete
10+
costFunctionReg - complete
11+
For this exercise, you will not need to change any code in this file,
12+
or any other files other than those mentioned above.
13+
"""
14+
15+
# Imports
2516
import numpy as np
2617
import ex2helper as helper
2718
import matplotlib.pyplot as plt
2819

29-
data = np.genfromtxt('./data/ex2data2.txt', delimiter=',')
30-
y = np.array(data[:,2])
31-
x = np.array(data[:,0:2])
32-
33-
## =========== Part 1: Regularized Logistic Regression ============
34-
# In this part, you are given a dataset with data points that are not
35-
# linearly separable. However, you would still like to use logistic
36-
# regression to classify the data points.
37-
#
38-
# To do so, you introduce more features to use -- in particular, you add
39-
# polynomial features to our data matrix (similar to polynomial
40-
# regression).
41-
#
42-
43-
# Add Polynomial Features
44-
45-
x = helper.mapFeatures(x)
46-
47-
# Initialize fitting parameters
48-
initial_theta = np.zeros(x.shape[1])
49-
50-
# Set regularization parameter lambda to 1
51-
lambdaVal = 1
52-
53-
# Compute and display initial cost and gradient for regularized logistic
54-
# regression
55-
cost = helper.costFunctionReg(initial_theta, x, y, lambdaVal)
56-
grad = helper.gradientReg(initial_theta, x, y, lambdaVal)
57-
58-
print('Cost at initial theta (zeros): {:.3f}'.format(cost))
59-
print('Expected cost (approx): 0.693\n')
60-
print('\nGradient at initial theta (zeros) - first five values only:')
61-
print(" {:.4f} {:.4f} {:.4f} {:.4f} {:.4f}".format(grad[0], grad[1],grad[2],grad[3],grad[4]))
62-
print('Expected gradients (approx) - first five values only:')
63-
print(' 0.0085 0.0188 0.0001 0.0503 0.0115\n')
64-
65-
66-
# Compute and display cost and gradient
67-
# with all-ones theta and lambda = 10
68-
test_theta = np.ones(x.shape[1])
69-
cost = helper.costFunctionReg(test_theta, x, y, 10)
70-
grad = helper.gradientReg(test_theta, x, y, 10)
71-
72-
73-
print('Cost at test theta (with lambda = 10): {:.2f}'.format(cost))
74-
print('Expected cost (approx): 3.16')
75-
print('\nGradient at initial theta (zeros) - first five values only:')
76-
print(" {:.4f} {:.4f} {:.4f} {:.4f} {:.4f}".format(grad[0], grad[1],grad[2],grad[3],grad[4]))
77-
print('Expected gradients (approx) - first five values only:')
78-
print(' 0.3460 0.1614 0.1948 0.2269 0.0922');
79-
80-
input('\nPart 1 completed. Program paused. Press enter to continue: ')
81-
82-
83-
## ============= Part 2: Regularization and Accuracies =============
84-
# Optional Exercise:
85-
# In this part, you will get to try different values of lambda and
86-
# see how regularization affects the decision coundart
87-
#
88-
# Try the following values of lambda (0, 1, 10, 100).
89-
#
90-
# How does the decision boundary change when you vary lambda? How does
91-
# the training set accuracy vary?
92-
#
93-
94-
# Initialize fitting parameters
95-
theta = np.zeros(x.shape[1])
96-
97-
# Set regularization parameter lambda to 1 (you should vary this)
98-
lambdaVal = 1
99-
100-
results = helper.optimizeReg(theta,x,y,lambdaVal)
101-
print(x.shape)
102-
print(theta.shape)
103-
print(y.shape)
104-
theta = results.x
105-
cost = results.fun
106-
107-
helper.plotData(x,y)
108-
plt.xlabel('Microchip Test 1')
109-
plt.ylabel('Microchip Test 2')
110-
plt.title('Raw Data')
111-
plt.show()
112-
113-
helper.plotDecisionBoundary(theta,x,y)
114-
plt.xlabel('Microchip Test 1')
115-
plt.ylabel('Microchip Test 2')
116-
plt.title('Lambda = {:}'.format(lambdaVal))
117-
plt.show()
118-
119-
# Compute accuracy on our training set
120-
p = helper.predict(theta, x)
121-
predictions = np.zeros(p.shape)
122-
predictions[np.where(p==y)] = 1
123-
124-
print('Train Accuracy: {:.1f}'.format(np.mean(predictions) * 100))
125-
print('Expected accuracy (with lambda = 1): 83.1 (approx)')
20+
21+
def main():
22+
# Load Data
23+
# The first two columns contains the X values and the third column
24+
# contains the label (y).
25+
data = np.genfromtxt('./data/ex2data2.txt', delimiter=',')
26+
y = np.array(data[:, 2])
27+
x = np.array(data[:, 0:2])
28+
29+
# =========== Part 1: Regularized Logistic Regression ============
30+
# In this part, you are given a dataset with data points that are not
31+
# linearly separable. However, you would still like to use logistic
32+
# regression to classify the data points.
33+
#
34+
# To do so, you introduce more features to use -- in particular, you add
35+
# polynomial features to our data matrix (similar to polynomial
36+
# regression).
37+
#
38+
39+
# Add Polynomial Features
40+
x = helper.mapFeatures(x)
41+
42+
# Initialize fitting parameters
43+
initial_theta = np.zeros(x.shape[1])
44+
45+
# Set regularization parameter lambda to 1
46+
lambdaVal = 1
47+
48+
# Compute and display initial cost and gradient for regularized logistic
49+
# regression
50+
cost = helper.costFunctionReg(initial_theta, x, y, lambdaVal)
51+
grad = helper.gradientReg(initial_theta, x, y, lambdaVal)
52+
53+
print('Cost at initial theta (zeros): {:.3f}'.format(cost))
54+
print('Expected cost (approx): 0.693\n')
55+
print('\nGradient at initial theta (zeros) - first five values only:')
56+
print(" {:.4f} {:.4f} {:.4f} {:.4f} {:.4f}".format(
57+
grad[0],
58+
grad[1],
59+
grad[2],
60+
grad[3],
61+
grad[4]))
62+
print('Expected gradients (approx) - first five values only:')
63+
print(' 0.0085 0.0188 0.0001 0.0503 0.0115\n')
64+
65+
# Compute and display cost and gradient
66+
# with all-ones theta and lambda = 10
67+
test_theta = np.ones(x.shape[1])
68+
cost = helper.costFunctionReg(test_theta, x, y, 10)
69+
grad = helper.gradientReg(test_theta, x, y, 10)
70+
71+
print('Cost at test theta (with lambda = 10): {:.2f}'.format(cost))
72+
print('Expected cost (approx): 3.16')
73+
print('\nGradient at initial theta (zeros) - first five values only:')
74+
print(" {:.4f} {:.4f} {:.4f} {:.4f} {:.4f}".format(
75+
grad[0],
76+
grad[1],
77+
grad[2],
78+
grad[3],
79+
grad[4]))
80+
print('Expected gradients (approx) - first five values only:')
81+
print(' 0.3460 0.1614 0.1948 0.2269 0.0922')
82+
83+
input('\nPart 1 completed. Program paused. Press enter to continue: ')
84+
85+
# ============= Part 2: Regularization and Accuracies =============
86+
# Optional Exercise:
87+
# In this part, you will get to try different values of lambda and
88+
# see how regularization affects the decision coundart
89+
#
90+
# Try the following values of lambda (0, 1, 10, 100).
91+
#
92+
# How does the decision boundary change when you vary lambda? How does
93+
# the training set accuracy vary?
94+
95+
# Initialize fitting parameters
96+
theta = np.zeros(x.shape[1])
97+
98+
# Set regularization parameter lambda to 1 (you should vary this)
99+
lambdaVal = 1
100+
101+
results = helper.optimizeReg(theta, x, y, lambdaVal)
102+
print(x.shape)
103+
print(theta.shape)
104+
print(y.shape)
105+
theta = results.x
106+
cost = results.fun
107+
108+
helper.plotData(x, y)
109+
plt.xlabel('Microchip Test 1')
110+
plt.ylabel('Microchip Test 2')
111+
plt.title('Raw Data')
112+
plt.show()
113+
114+
helper.plotDecisionBoundary(theta, x, y)
115+
plt.xlabel('Microchip Test 1')
116+
plt.ylabel('Microchip Test 2')
117+
plt.title('Lambda = {:}'.format(lambdaVal))
118+
plt.show()
119+
120+
# Compute accuracy on our training set
121+
p = helper.predict(theta, x)
122+
predictions = np.zeros(p.shape)
123+
predictions[np.where(p == y)] = 1
124+
125+
print('Train Accuracy: {:.1f}'.format(np.mean(predictions) * 100))
126+
print('Expected accuracy (with lambda = 1): 83.1 (approx)')
127+
128+
if __name__ == '__main__':
129+
main()

‎ex2helper.py

+34-9
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@
44

55

66
def plotData(X, Y):
7-
# np.extract(Y==1,X[0]) returns an array of values in X where the value is 1 in the same location in Y
8-
positiveExamples = plt.scatter(np.extract(Y == 1, X[:, 1]), np.extract(Y == 1, X[:, 2]), label="y=1", marker='o',
9-
color='b', s=10)
10-
negativeExamples = plt.scatter(np.extract(Y == 0, X[:, 1]), np.extract(Y == 0, X[:, 2]), label="y=0", marker='x',
11-
color='r', s=10)
7+
positiveExamples = plt.scatter(
8+
np.extract(Y == 1, X[:, 1]),
9+
np.extract(Y == 1, X[:, 2]),
10+
label="y=1",
11+
marker='o',
12+
color='b',
13+
s=10)
14+
15+
negativeExamples = plt.scatter(
16+
np.extract(Y == 0, X[:, 1]),
17+
np.extract(Y == 0, X[:, 2]),
18+
label="y=0",
19+
marker='x',
20+
color='r',
21+
s=10)
22+
1223
plt.legend(handles=[positiveExamples, negativeExamples], loc='lower left')
1324

1425

@@ -31,7 +42,12 @@ def gradient(theta, x, y):
3142

3243

3344
def optimize(theta, x, y):
34-
return op.minimize(fun=costFunction, x0=theta, args=(x, y), method='TNC', jac=gradient)
45+
return op.minimize(
46+
fun=costFunction,
47+
x0=theta,
48+
args=(x, y),
49+
method='TNC',
50+
jac=gradient)
3551

3652

3753
def predict(theta, x):
@@ -83,7 +99,9 @@ def costFunctionReg(theta, x, y, lambdaVal):
8399
y = np.squeeze(y)
84100
m = x.shape[0]
85101
if (y.shape[0] != m):
86-
raise ValueError('Y & X are not compatible: X.shape = {} & y.shape = {}'.format(X.shape, y.shape))
102+
raise ValueError('Y & X are not compatible: X.shape = {} & y.shape = {}'.format(
103+
X.shape,
104+
y.shape))
87105

88106
z = sigmoid(np.matmul(x, theta))
89107

@@ -104,7 +122,9 @@ def gradientReg(theta, x, y, lambdaVal):
104122
y = np.squeeze(y)
105123
m = x.shape[0]
106124
if (y.shape[0] != m):
107-
raise ValueError('Y & X are not compatible: X.shape = {} & y.shape = {}'.format(X.shape, y.shape))
125+
raise ValueError('Y & X are not compatible: X.shape = {} & y.shape = {}'.format(
126+
X.shape,
127+
y.shape))
108128

109129
z = sigmoid(np.matmul(x, theta))
110130

@@ -118,4 +138,9 @@ def gradientReg(theta, x, y, lambdaVal):
118138

119139

120140
def optimizeReg(theta, x, y, lambdaVal):
121-
return op.minimize(fun=costFunctionReg, x0=theta, args=(x, y, lambdaVal), method='TNC', jac=gradientReg)
141+
return op.minimize(
142+
fun=costFunctionReg,
143+
x0=theta,
144+
args=(x, y, lambdaVal),
145+
method='TNC',
146+
jac=gradientReg)

‎ex3.py

+74-73
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,100 @@
1-
## Machine Learning Online Class - Exercise 3 | Part 1: One-vs-all
2-
3-
# Instructions
4-
# ------------
5-
#
6-
# This file contains code that helps you get started on the
7-
# linear exercise. You will need to complete the following functions
8-
# in this exericse:
9-
#
10-
# lrCostFunction (logistic regression cost function) - complete
11-
# oneVsAll - complete
12-
# predictOneVsAll - complete
13-
# predict - complated
14-
#
15-
# For this exercise, you will not need to change any code in this file,
16-
# or any other files other than those mentioned above.
17-
#
18-
19-
## Initialization
1+
""" Machine Learning Online Class - Exercise 3 | Part 1: One-vs-all
2+
Instructions
3+
------------
4+
This file contains code that helps you get started on the
5+
linear exercise. You will need to complete the following functions
6+
in this exericse:
7+
lrCostFunction (logistic regression cost function) - complete
8+
oneVsAll - complete
9+
predictOneVsAll - complete
10+
predict - complated
11+
For this exercise, you will not need to change any code in this file,
12+
or any other files other than those mentioned above.
13+
"""
14+
15+
# Imports
2016
import numpy as np
2117
import matplotlib.pyplot as plt
2218
import scipy.io as io
2319
import ex2helper as helper2
2420
import ex3helper as helper
2521

26-
## Setup the parameters you will use for this part of the exercise
27-
input_layer_size = 400; # 20x20 Input Images of Digits
28-
num_labels = 10; # 10 labels, from 1 to 10
29-
# (note that we have mapped "0" to label 10)
3022

31-
## =========== Part 1: Loading and Visualizing Data =============
32-
# We start the exercise by first loading and visualizing the dataset.
33-
# You will be working with a dataset that contains handwritten digits.
34-
#
23+
def main():
24+
# Setup the parameters you will use for this part of the exercise
25+
input_layer_size = 400 # 20x20 Input Images of Digits
26+
num_labels = 10 # 10 labels, from 1 to 10
27+
# (note that we have mapped "0" to label 10)
3528

36-
# Load Training Data
37-
print('Loading and Visualizing Data ...')
38-
mat = io.loadmat('./data/ex3data1.mat')
39-
X = mat['X']
40-
y = np.squeeze(mat['y'])
29+
# =========== Part 1: Loading and Visualizing Data =============
30+
# We start the exercise by first loading and visualizing the dataset.
31+
# You will be working with a dataset that contains handwritten digits.
4132

33+
# Load Training Data
34+
print('Loading and Visualizing Data ...')
35+
mat = io.loadmat('./data/ex3data1.mat')
36+
X = mat['X']
37+
y = np.squeeze(mat['y'])
4238

43-
m = X.shape[0]
39+
m = X.shape[0]
4440

45-
# Randomly select 100 data points to display
46-
perm = np.random.permutation(m)
47-
sel = X[perm[0:100],:]
41+
# Randomly select 100 data points to display
42+
perm = np.random.permutation(m)
43+
sel = X[perm[0:100], :]
4844

49-
#display data as image
50-
helper.displayData(sel)
51-
plt.show()
52-
53-
input('\nPart 1 completed. Program paused. Press enter to continue: ')
45+
# display data as image
46+
helper.displayData(sel)
47+
plt.show()
5448

55-
## ============ Part 2a: Vectorize Logistic Regression ============
56-
# In this part of the exercise, you will reuse your logistic regression
57-
# code from the last exercise. You task here is to make sure that your
58-
# regularized logistic regression implementation is vectorized. After
59-
# that, you will implement one-vs-all classification for the handwritten
60-
# digit dataset.
49+
input('\nPart 1 completed. Program paused. Press enter to continue: ')
6150

51+
# ============ Part 2a: Vectorize Logistic Regression ============
52+
# In this part of the exercise, you will reuse your logistic regression
53+
# code from the last exercise. You task here is to make sure that your
54+
# regularized logistic regression implementation is vectorized. After
55+
# that, you will implement one-vs-all classification for the handwritten
56+
# digit dataset.
6257

63-
# Test case for lrCostFunction
64-
print('\nTesting lrCostFunction() with regularization')
58+
# Test case for lrCostFunction
59+
print('\nTesting lrCostFunction() with regularization')
6560

66-
theta_t = np.array([-2,-1,1,2])
67-
X_t = np.concatenate((np.array([np.ones(5)]),np.divide(np.arange(1,16,1),10).reshape(3,5)),axis=0).transpose()
68-
Y_t = np.array([1,0,1,0,1])
69-
lambda_t = 3
61+
theta_t = np.array([-2, -1, 1, 2])
62+
X_t = np.concatenate(
63+
(np.array([np.ones(5)]),
64+
np.divide(np.arange(15) + 1, 10)
65+
.reshape(3, 5)),
66+
axis=0).transpose()
7067

71-
J = helper2.costFunctionReg(theta_t,X_t,Y_t,lambda_t)
72-
grad = helper2.gradientReg(theta_t,X_t,Y_t,lambda_t)
68+
Y_t = np.array([1, 0, 1, 0, 1])
69+
lambda_t = 3
7370

74-
print('Cost: {:.6f}'.format(J))
75-
print('Expected cost: 2.534819')
76-
print('Gradients:')
77-
print(grad)
78-
print('Expected gradients:')
79-
print('[0.146561 -0.548558 0.724722 1.398003]')
71+
J = helper2.costFunctionReg(theta_t, X_t, Y_t, lambda_t)
72+
grad = helper2.gradientReg(theta_t, X_t, Y_t, lambda_t)
8073

74+
print('Cost: {:.6f}'.format(J))
75+
print('Expected cost: 2.534819')
76+
print('Gradients:')
77+
print(grad)
78+
print('Expected gradients:')
79+
print('[0.146561 -0.548558 0.724722 1.398003]')
8180

82-
input('\nPart 2a completed. Program paused. Press enter to continue: ')
81+
input('\nPart 2a completed. Program paused. Press enter to continue: ')
8382

84-
## ============ Part 2b: One-vs-All Training ============
85-
print('\nTraining One-vs-All Logistic Regression...')
83+
# ============ Part 2b: One-vs-All Training ============
84+
print('\nTraining One-vs-All Logistic Regression...')
8685

87-
lambdaVal = .1
88-
allTheta = helper.OneVsAll(X, y, np.unique(y), lambdaVal)
86+
lambdaVal = .1
87+
allTheta = helper.OneVsAll(X, y, np.unique(y), lambdaVal)
8988

89+
input('\nPart 2b completed. Program paused. Press enter to continue: ')
90+
# ================ Part 3: Predict for One-Vs-All ================
9091

91-
input('\nPart 2b completed. Program paused. Press enter to continue: ')
92-
## ================ Part 3: Predict for One-Vs-All ================
92+
p = helper.predictOneVsAll(allTheta, X)
93+
predictions = np.zeros(p.shape)
94+
predictions[np.where(p == y)] = 1
9395

94-
p = helper.predictOneVsAll(allTheta,X)
95-
predictions = np.zeros(p.shape)
96-
predictions[np.where(p==y)] = 1
96+
print('Train Accuracy: {:.1f}%'.format(np.mean(predictions) * 100))
97+
print('Expected Accuracy: 96.5%')
9798

98-
print('Train Accuracy: {:.1f}%'.format(np.mean(predictions) * 100))
99-
print('Expected Accuracy: 96.5%')
99+
if __name__ == '__main__':
100+
main()

‎ex3_nn.py

+71-76
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,96 @@
1-
## Machine Learning Online Class - Exercise 3 | Part 2: Neural Networks
2-
3-
# Instructions
4-
# ------------
5-
#
6-
# This file contains code that helps you get started on the
7-
# linear exercise. You will need to complete the following functions
8-
# in this exericse:
9-
#
10-
# lrCostFunction (logistic regression cost function) - completed
11-
# oneVsAll - completed
12-
# predictOneVsAll - completed
13-
# predict - completed
14-
#
15-
# For this exercise, you will not need to change any code in this file,
16-
# or any other files other than those mention d above.
17-
#
18-
19-
## Initialization
1+
""" Machine Learning Online Class - Exercise 3 | Part 2: Neural Networks
2+
Instructions
3+
------------
4+
This file contains code that helps you get started on the
5+
linear exercise. You will need to complete the following functions
6+
in this exericse:
7+
lrCostFunction (logistic regression cost function) - completed
8+
oneVsAll - completed
9+
predictOneVsAll - completed
10+
predict - completed
11+
For this exercise, you will not need to change any code in this file,
12+
or any other files other than those mentioned above.
13+
"""
14+
15+
# Imports
2016
import numpy as np
2117
import matplotlib.pyplot as plt
2218
import scipy.io as io
2319
import ex2helper as helper2
2420
import ex3helper as helper
2521

26-
## Setup the parameters you will use for this exercise
27-
input_layer_size = 400; # 20x20 Input Images of Digits
28-
hidden_layer_size = 25; # 25 hidden units
29-
num_labels = 10; # 10 labels, from 1 to 10
30-
# (note that we have mapped "0" to label 10)
3122

32-
## =========== Part 1: Loading and Visualizing Data =============
33-
# We start the exercise by first loading and visualizing the dataset.
34-
# You will be working with a dataset that contains handwritten digits.
35-
#
23+
def main():
24+
# Setup the parameters you will use for this exercise
25+
input_layer_size = 400 # 20x20 Input Images of Digits
26+
hidden_layer_size = 25 # 25 hidden units
27+
num_labels = 10 # 10 labels, from 1 to 10
28+
# (note that we have mapped "0" to label 10)
3629

37-
# Load Training Data
38-
print('Loading and Visualizing Data ...')
30+
# =========== Part 1: Loading and Visualizing Data =============
31+
# We start the exercise by first loading and visualizing the dataset.
32+
# You will be working with a dataset that contains handwritten digits.
3933

40-
mat = io.loadmat('./data/ex3data1.mat')
41-
X = mat['X']
42-
y = np.squeeze(mat['y'])
34+
# Load Training Data
35+
print('Loading and Visualizing Data ...')
4336

37+
mat = io.loadmat('./data/ex3data1.mat')
38+
X = mat['X']
39+
y = np.squeeze(mat['y'])
4440

45-
m = y.shape[0]
41+
m = y.shape[0]
4642

47-
# Randomly select 100 data points to display
48-
perm = np.random.permutation(m)
49-
sel = X[perm[0:100],:]
43+
# Randomly select 100 data points to display
44+
perm = np.random.permutation(m)
45+
sel = X[perm[0:100], :]
5046

51-
#display data as image
52-
helper.displayData(sel)
53-
plt.show()
47+
# display data as image
48+
helper.displayData(sel)
49+
plt.show()
5450

51+
input('\nPart 1 completed. Program paused. Press enter to continue: ')
5552

56-
input('\nPart 1 completed. Program paused. Press enter to continue: ')
53+
# ================ Part 2: Loading Pameters ================
54+
# In this part of the exercise, we load some pre-initialized
55+
# neural network parameters.
5756

57+
print('\nLoading Saved Neural Network Parameters ...')
5858

59-
## ================ Part 2: Loading Pameters ================
60-
# In this part of the exercise, we load some pre-initialized
61-
# neural network parameters.
59+
# Load the weights into variables Theta1 and Theta2
60+
mat = io.loadmat('./data/ex3weights.mat')
61+
theta1 = mat['Theta1']
62+
theta2 = mat['Theta2']
6263

63-
print('\nLoading Saved Neural Network Parameters ...')
64+
# ================= Part 3: Implement Predict =================
65+
# After training the neural network, we would like to use it to predict
66+
# the labels. You will now implement the "predict" function to use the
67+
# neural network to predict the labels of the training set. This lets
68+
# you compute the training set accuracy.
6469

65-
# Load the weights into variables Theta1 and Theta2
66-
mat = io.loadmat('./data/ex3weights.mat')
67-
theta1 = mat['Theta1']
68-
theta2 = mat['Theta2']
70+
p = helper.predict(theta1, theta2, X)
71+
predictions = np.zeros(p.shape)
72+
predictions[np.where(p == y)] = 1
6973

70-
## ================= Part 3: Implement Predict =================
71-
# After training the neural network, we would like to use it to predict
72-
# the labels. You will now implement the "predict" function to use the
73-
# neural network to predict the labels of the training set. This lets
74-
# you compute the training set accuracy.
74+
print('Train Set Accuracy: {:.1f}%'.format(np.mean(predictions) * 100))
7575

76-
p = helper.predict(theta1, theta2, X)
77-
predictions = np.zeros(p.shape)
78-
predictions[np.where(p==y)] = 1
76+
input('\nPart 3 completed. Program paused. Press enter to continue: ')
7977

80-
print('Train Set Accuracy: {:.1f}%'.format(np.mean(predictions) * 100))
78+
# Randomly select 100 data points to display
79+
perm = np.random.permutation(m)
80+
for i in range(0, m):
81+
print('\n Displaying Example Image...\n')
82+
example = X[perm[i], :]
83+
example = example[np.newaxis, :]
8184

82-
input('\nPart 3 completed. Program paused. Press enter to continue: ')
85+
helper.displayData(example)
86+
plt.show()
87+
p = helper.predict(theta1, theta2, example)
88+
print(' Neural Network Prediction: {}'.format(p[0] % 10))
89+
print(' Correct Answer: {}\n'.format(y[perm[i]] % 10))
8390

84-
# Randomly select 100 data points to display
85-
perm = np.random.permutation(m)
86-
for i in range(0,m):
87-
print('\n Displaying Example Image...\n')
88-
example = X[perm[i],:]
89-
example = example[np.newaxis,:]
91+
answer = input('Paused - press enter to continue, q to exit:')
92+
if(answer == 'q'):
93+
break
9094

91-
helper.displayData(example)
92-
plt.show()
93-
p = helper.predict(theta1, theta2, example)
94-
print(' Neural Network Prediction: {}'.format(p[0]%10))
95-
print(' Correct Answer: {}\n'.format(y[perm[i]]%10))
96-
97-
98-
99-
answer = input('Paused - press enter to continue, q to exit:')
100-
if(answer=='q'):
101-
break
95+
if __name__ == '__main__':
96+
main()

‎ex3helper.py

+111-96
Original file line numberDiff line numberDiff line change
@@ -3,115 +3,130 @@
33
import ex2helper as helper
44
import math
55

6+
67
def OneVsAll(X, y, numlabels, lambdaVal):
7-
m = X.shape[0] #number of examples
8-
n = X.shape[1] #number of data points
9-
10-
X = np.insert(X,0,np.ones(X.shape[0]),axis=1) # adding bias unit
11-
theta = np.array([])#initialize theta
8+
m = X.shape[0] # number of examples
9+
n = X.shape[1] # number of data points
10+
11+
X = np.insert(X, 0, np.ones(X.shape[0]), axis=1) # adding bias unit
12+
theta = np.array([]) # initialize theta
1213

14+
for i in numlabels:
15+
yTemp = np.zeros(y.shape[0])
16+
yTemp[np.where(y == i)] = 1
17+
thetaTemp = np.zeros(n + 1)
1318

14-
for i in numlabels:
15-
yTemp = np.zeros(y.shape[0])
16-
yTemp[np.where(y==i)] = 1
17-
thetaTemp = np.zeros(n + 1)
19+
# run regularized optimization
20+
results = helper.optimizeReg(thetaTemp, X, yTemp, lambdaVal)
21+
thetaTemp = results.x
1822

19-
#run regularized optimization
20-
results = helper.optimizeReg(thetaTemp, X, yTemp, lambdaVal)
21-
thetaTemp = results.x
23+
# get prediction accuracy
24+
p = helper.predict(thetaTemp, X)
25+
predictions = np.zeros(p.shape)
26+
predictions[np.where(p == yTemp)] = 1
27+
p = helper.sigmoid(np.matmul(X, thetaTemp))
2228

23-
#get prediction accuracy
24-
p = helper.predict(thetaTemp, X)
25-
predictions = np.zeros(p.shape)
26-
predictions[np.where(p==yTemp)] = 1
27-
p = helper.sigmoid(np.matmul(X,thetaTemp))
29+
# Validating that the function is working
30+
print('Train Accuracy: {:.1f}%'.format(np.mean(predictions) * 100))
31+
print('cost for {} = {:.3f}, max = {:.3f}'.format(
32+
i % 10,
33+
results.fun,
34+
np.max(p)))
2835

29-
#calculating cost and accuracy to validate that the function is working correctly
30-
print('Train Accuracy: {:.1f}%'.format(np.mean(predictions) * 100))
31-
print('cost for {} = {:.3f}, max = {:.3f}'.format(i%10,results.fun,np.max(p)))
36+
# appending discovered theta to theta
37+
theta = np.append(theta, thetaTemp)
3238

33-
theta = np.append(theta, thetaTemp)#appending discovered theta to theta
39+
# Struggled on this for awhile.
40+
# Reshape works from left to right, top to bottom.
41+
# So if your data needs to be in columns instead of rows,
42+
# It messes it all up, but it still "works"
43+
theta = np.reshape(theta, (numlabels.shape[0], n + 1))
44+
return theta.transpose()
3445

35-
#struggled on this for awhile. Reshape works from left to right, top to bottom.
36-
#so if your data needs to be in columns instead of rows. It messes it all up, but it still works
37-
theta = np.reshape(theta, (numlabels.shape[0],n + 1))
38-
return theta.transpose()
3946

4047
def predictOneVsAll(allTheta, X):
41-
X = np.insert(X,0,np.ones(X.shape[0]),axis=1) # adding bias unit
48+
X = np.insert(X, 0, np.ones(X.shape[0]), axis=1)
49+
# adding bias unit
50+
51+
pred = helper.sigmoid(np.matmul(X, allTheta))
52+
# calculate predictions for all thetas
53+
54+
# return vector of position of maximum for each
55+
# row +1 to adjust for arrays initializing at 0
56+
return(np.argmax(pred, axis=1)+1)
4257

43-
pred = helper.sigmoid(np.matmul(X,allTheta))#calculate predictions for all thetas
44-
45-
#return vector of position of maximum for each row +1 to adjust for arrays initializing at 0
46-
return(np.argmax(pred,axis=1)+1)
4758

4859
def displayData(X, **keywordParameters):
49-
#set example width automatically if not given
50-
if('exampleWidth' in keywordParameters):
51-
exampleWidth = keywordParameters['exampleWidth']
52-
else:
53-
exampleWidth = round(math.sqrt(X.shape[1]))
54-
55-
#calculate size of rows and columns
56-
[m, n] = X.shape
57-
exampleHeight = n//exampleWidth #eliminating float with // divide
58-
59-
#calculate number of items to display
60-
displayRows = math.floor(math.sqrt(m))
61-
displayColumns = math.ceil(m/displayRows)
62-
63-
#set padding between images
64-
padding = 1
65-
66-
#set up blank display
67-
displayHeight = padding + displayRows * (exampleHeight + padding)
68-
displayWidth = padding + displayColumns * (exampleWidth + padding)
69-
70-
displayArray = - np.ones([displayHeight, displayWidth])
71-
72-
#Copy each example into a path on the display array
73-
currentExample = 0
74-
for j in range(0,displayRows):
75-
for i in range(0, displayColumns):
76-
if(currentExample > m):
77-
break
78-
79-
#Copy the Patch
80-
81-
#1. get the max value of the patch
82-
maxValue = np.amax(np.absolute(X[currentExample,:]))
83-
84-
#2. get current example in the correct shape
85-
example = np.reshape(X[currentExample,:], [exampleHeight, exampleWidth])/maxValue
86-
example = example.transpose()
87-
88-
#3. calculate current position height and width
89-
currentPositionHeight = padding + j * (exampleHeight + padding)
90-
currentPositionWidth = padding + i * (exampleWidth + padding)
91-
92-
#4. assign current example to correct position in the display array
93-
displayArray[currentPositionHeight:currentPositionHeight + exampleHeight, currentPositionWidth:currentPositionWidth + exampleWidth] = example
94-
95-
#5. iterate current example
96-
currentExample = currentExample + 1
97-
98-
if(currentExample>m):
99-
break
100-
101-
#show image
102-
imgplot = plt.imshow(displayArray, cmap='gray')
103-
plt.axis('off')
60+
# set example width automatically if not given
61+
if('exampleWidth' in keywordParameters):
62+
exampleWidth = keywordParameters['exampleWidth']
63+
else:
64+
exampleWidth = round(math.sqrt(X.shape[1]))
65+
66+
# calculate size of rows and columns
67+
[m, n] = X.shape
68+
exampleHeight = n//exampleWidth # eliminating float with // divide
69+
70+
# calculate number of items to display
71+
displayRows = math.floor(math.sqrt(m))
72+
displayColumns = math.ceil(m/displayRows)
73+
74+
# set padding between images
75+
padding = 1
76+
77+
# set up blank display
78+
displayHeight = padding + displayRows * (exampleHeight + padding)
79+
displayWidth = padding + displayColumns * (exampleWidth + padding)
80+
81+
displayArray = - np.ones([displayHeight, displayWidth])
82+
83+
# Copy each example into a path on the display array
84+
currentExample = 0
85+
for j in range(0, displayRows):
86+
for i in range(0, displayColumns):
87+
if(currentExample > m):
88+
break
89+
90+
# Copy the Patch
91+
92+
# 1. get the max value of the patch
93+
maxValue = np.amax(np.absolute(X[currentExample, :]))
94+
95+
# 2. get current example in the correct shape
96+
example = np.reshape(
97+
X[currentExample, :],
98+
[exampleHeight, exampleWidth])/maxValue
99+
example = example.transpose()
100+
101+
# 3. calculate current position height and width
102+
positionHeight = padding + j * (exampleHeight + padding)
103+
positionWidth = padding + i * (exampleWidth + padding)
104+
105+
# 4. assign current example to correct position in the displayarray
106+
displayArray[
107+
positionHeight:positionHeight + exampleHeight,
108+
positionWidth:positionWidth + exampleWidth] = example
109+
110+
# 5. iterate current example
111+
currentExample = currentExample + 1
112+
113+
if(currentExample > m):
114+
break
115+
116+
# show image
117+
imgplot = plt.imshow(displayArray, cmap='gray')
118+
plt.axis('off')
119+
104120

105121
def predict(theta1, theta2, X):
106-
m = X.shape[0]
107-
num_labels = theta2.shape[0]
108-
109-
X = np.insert(X,0,np.ones(X.shape[0]),axis=1) # adding bias unit
110-
a1 = np.matmul(X,theta1.transpose())
111-
a1 = helper.sigmoid(a1)
112-
a1 = np.insert(a1,0,np.ones(a1.shape[0]),axis=1) # adding bias unit
113-
a2 = np.matmul(a1,theta2.transpose())
114-
a2 = helper.sigmoid(a2)
115-
116-
return(np.argmax(a2,axis=1)+1)
122+
m = X.shape[0]
123+
num_labels = theta2.shape[0]
124+
125+
X = np.insert(X, 0, np.ones(X.shape[0]), axis=1) # adding bias unit
126+
a1 = np.matmul(X, theta1.transpose())
127+
a1 = helper.sigmoid(a1)
128+
a1 = np.insert(a1, 0, np.ones(a1.shape[0]), axis=1) # adding bias unit
129+
a2 = np.matmul(a1, theta2.transpose())
130+
a2 = helper.sigmoid(a2)
117131

132+
return(np.argmax(a2, axis=1)+1)

‎ex4.py

+206-173
Large diffs are not rendered by default.

‎ex4Checker.py

+96-65
Original file line numberDiff line numberDiff line change
@@ -8,79 +8,110 @@
88

99

1010
def main():
11-
checkNNGradients(0)
12-
#checkNNGradients(1)
11+
checkNNGradients(0)
1312

14-
def checkNNGradients(lambdaVal):
15-
# CHECKNNGRADIENTS(lambda) Creates a small neural network to check the
16-
# backpropagation gradients, it will output the analytical gradients
17-
# produced by your backprop code and the numerical gradients (computed
18-
# using computeNumericalGradient). These two gradient computations should
19-
# result in very similar values.
20-
#
21-
22-
inputLayerSize = 3
23-
hiddenLayerSize = 5
24-
numLabels = 3
25-
m = 5
26-
27-
#We generate some 'random' test data
28-
theta1 = debugInitializeWeights(hiddenLayerSize, inputLayerSize)
29-
theta2 = debugInitializeWeights(numLabels, hiddenLayerSize)
30-
31-
# Reusing debugInitializeWeights to generate X
32-
X = debugInitializeWeights(m, inputLayerSize - 1);
33-
y = np.remainder(np.arange(m),numLabels) + 1
34-
35-
#unroll parameters
36-
nnParams = np.append(theta1.flatten(), theta2.flatten())
37-
38-
#calculate gradient with backprop
39-
grad = helper.BackPropagation(nnParams, inputLayerSize, hiddenLayerSize, numLabels, X, y, lambdaVal)
4013

41-
#calculate difference between backprop and numerical gradient
42-
diff = op.check_grad(costMask, backPropMask, nnParams, inputLayerSize, hiddenLayerSize, numLabels, X, y, lambdaVal, epsilon=.0001)
43-
44-
numGrad = op.approx_fprime(nnParams, costMask, .001 , inputLayerSize, hiddenLayerSize, numLabels, X, y, lambdaVal)
45-
# Visually examine the two gradient computations. The two columns you get should be very similar.
14+
def checkNNGradients(lambdaVal):
15+
# CHECKNNGRADIENTS(lambda) Creates a small neural network to check the
16+
# backpropagation gradients, it will output the analytical gradients
17+
# produced by your backprop code and the numerical gradients (computed
18+
# using computeNumericalGradient). These two gradient computations should
19+
# result in very similar values.
20+
#
21+
22+
inputLayerSize = 3
23+
hiddenLayerSize = 5
24+
numLabels = 3
25+
m = 5
26+
27+
# We generate some 'random' test data
28+
theta1 = debugInitializeWeights(hiddenLayerSize, inputLayerSize)
29+
theta2 = debugInitializeWeights(numLabels, hiddenLayerSize)
30+
31+
# Reusing debugInitializeWeights to generate X
32+
X = debugInitializeWeights(m, inputLayerSize - 1)
33+
y = np.remainder(np.arange(m), numLabels) + 1
34+
35+
# unroll parameters
36+
nnParams = np.append(theta1.flatten(), theta2.flatten())
37+
38+
# calculate gradient with backprop
39+
grad = helper.BackPropagation(
40+
nnParams,
41+
inputLayerSize,
42+
hiddenLayerSize,
43+
numLabels,
44+
X,
45+
y,
46+
lambdaVal)
47+
48+
# calculate difference between backprop and numerical gradient
49+
diff = op.check_grad(
50+
costMask,
51+
backPropMask,
52+
nnParams,
53+
inputLayerSize,
54+
hiddenLayerSize,
55+
numLabels,
56+
X,
57+
y,
58+
lambdaVal,
59+
epsilon=.0001)
60+
61+
numGrad = op.approx_fprime(
62+
nnParams,
63+
costMask,
64+
.001,
65+
inputLayerSize,
66+
hiddenLayerSize,
67+
numLabels,
68+
X,
69+
y,
70+
lambdaVal)
71+
72+
# Visually examine the two gradient computations.
73+
# The two columns you get should be very similar.
74+
print('\nComparing Gradients: (numGrad, grad, absolute difference)')
75+
76+
for i in range(numGrad.shape[0]):
77+
print("{}: {:.9f}, {:.9f} {:.9f}".format(
78+
i+1,
79+
numGrad[i],
80+
grad[i],
81+
abs(numGrad[i] - grad[i])))
82+
83+
print('The above left two columns you get should be very similar.')
84+
print('(Left-Your Numerical Gradient, Right-Analytical Gradient)')
85+
86+
# Evaluate the norm of the difference between two solutions.
87+
# If you have a correct implementation
88+
# and you used EPSILON = 0.0001
89+
# in computeNumericalGradient,
90+
# then diff below should be less than 1e-9
91+
92+
print('If your backpropagation implementation is correct, then ')
93+
print('the relative difference will be small (less than 1e-9).')
94+
print('Relative Difference: {}'.format(diff))
4695

4796

48-
print('\nComparing Gradients: (numGrad, grad, absolute difference)')
97+
def debugInitializeWeights(fanOut, fanIn):
98+
# Initialize W using "sin", this ensures that vW is always of the same
99+
# values and will be useful for debugging
100+
# numel ~ number of elements. equivalent to size, w.size
101+
# size, equivalent of shape, w.shape
49102

50-
for i in range(0,numGrad.shape[0]):
51-
print("{}: {:.9f}, {:.9f} {:.9f}".format(i+1, numGrad[i], grad[i], abs(numGrad[i] - grad[i])))
52-
103+
W = np.arange(fanOut*(fanIn+1))
104+
W = W.reshape(fanOut, fanIn+1)
105+
W = np.sin(W)/10
106+
return W
53107

54-
print('The above left two columns you get should be very similar.')
55-
print('(Left-Your Numerical Gradient, Right-Analytical Gradient)')
56108

57-
# Evaluate the norm of the difference between two solutions.
58-
# If you have a correct implementation, and assuming you used EPSILON = 0.0001
59-
# in computeNumericalGradient.m, then diff below should be less than 1e-9
109+
def backPropMask(nnParams, *args):
110+
return helper.BackPropagation(nnParams, *args)
60111

61-
print('If your backpropagation implementation is correct, then ')
62-
print('the relative difference will be small (less than 1e-9).')
63-
print('Relative Difference: {}'.format(diff))
64112

65-
66-
def debugInitializeWeights(fanOut, fanIn):
67-
# Initialize W using "sin", this ensures that vW is always of the same
68-
# values and will be useful for debugging
69-
# W = zeros(fan_out, 1 + fan_in);
70-
# W = reshape(sin(1:numel(W)), size(W)) / 10;
71-
# numel ~ number of elements. equivalent to size, w.size
72-
# size, equivalent of shape, w.shape
73-
W = np.arange(fanOut*(fanIn+1))
74-
W = W.reshape(fanOut, fanIn+1)
75-
W = np.sin(W)/10
76-
return W
77-
78-
def backPropMask(nnParams,*args):
79-
return helper.BackPropagation(nnParams,*args)
80-
81-
def costMask(nnParams,*args):
82-
return helper.nnCostFunction(nnParams,*args)
113+
def costMask(nnParams, *args):
114+
return helper.nnCostFunction(nnParams, *args)
83115

84116
if __name__ == '__main__':
85-
main()
86-
117+
main()

‎ex4helper.py

+168-106
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,202 @@
1-
import numpy as np
1+
import numpy as np
22
import matplotlib.pyplot as plt
33
import scipy.optimize as op
44
import math
55
import matplotlib.image as mpimg
66

7-
def nnCostFunction(nnParams, inputSize, hiddenLayerSize, outputSize, X, y, lambdaVal):
8-
#get num examples
9-
m = X.shape[0]
107

11-
#get Theta Matrices
12-
[theta1, theta2] = getThetas(nnParams,inputSize,hiddenLayerSize,outputSize)
8+
def nnCostFunction(
9+
nnParams,
10+
inputSize,
11+
hiddenLayerSize,
12+
outputSize,
13+
X,
14+
y,
15+
lambdaVal):
16+
# get num examples
17+
m = X.shape[0]
18+
19+
# get Theta Matrices
20+
[theta1, theta2] = getThetas(
21+
nnParams,
22+
inputSize,
23+
hiddenLayerSize,
24+
outputSize)
25+
26+
# prepare Y matrix for cost function
27+
Y = getYMatrix(y)
28+
29+
# forward Pass
30+
[a1, z1, a2, z2, h2] = forwardPass(np.array([theta1, theta2]), X)
31+
32+
# getting regulation parameters
33+
R1 = theta1[:, 1:]
34+
R2 = theta2[:, 1:]
35+
36+
# calculating the cost of regulation
37+
costRegulation = lambdaVal*(np.sum(
38+
np.square(R1.flatten())) + np.sum(
39+
np.square(R2.flatten())))/(2*m)
40+
41+
# calculating true cost without regulation
42+
cost = np.sum(
43+
np.log(np.extract(Y == 1, h2))) + np.sum(
44+
np.log(1-np.extract(Y == 0, h2)))
45+
46+
cost = -cost/m
47+
48+
# calculate total cost
49+
totalCost = cost + costRegulation
50+
51+
return totalCost
52+
53+
54+
def BackPropagation(
55+
nnParams,
56+
inputSize,
57+
hiddenLayerSize,
58+
outputSize,
59+
X,
60+
y,
61+
lambdaVal):
62+
# get num examples
63+
m = X.shape[0]
64+
# get Theta Matrices
65+
[theta1, theta2] = getThetas(
66+
nnParams,
67+
inputSize,
68+
hiddenLayerSize,
69+
outputSize)
70+
71+
# prepare Y matrix for cost function
72+
Y = getYMatrix(y) # 5x3
73+
74+
# forward Pass
75+
[a1, z1, a2, z2, h2] = forwardPass(np.array([theta1, theta2]), X)
76+
# a1 = 5x4, z1 = 5x5, a2 = 5x5, a2 = 5x6, z2 = 5x3, h2 = 5x3
77+
78+
# backward
79+
theta2Error = h2-Y # 5x3
80+
theta1Error = np.matmul(theta2Error, theta2[:, 1:])*sigmoidGradient(z1)
81+
82+
D1 = np.matmul(theta1Error.transpose(), a1)
83+
D2 = np.matmul(theta2Error.transpose(), a2)
84+
85+
# average the gradient per example
86+
theta1Grad = D1/m
87+
theta2Grad = D2/m
88+
89+
# calculate regulation terms
90+
theta1Reg = lambdaVal*theta1/m
91+
theta2Reg = lambdaVal*theta2/m
92+
theta1Reg[:, 0] = 0
93+
theta2Reg[:, 0] = 0
94+
95+
# combine gradient and regulation terms
96+
theta1Grad = theta1Grad + theta1Reg
97+
theta2Grad = theta2Grad + theta2Reg
98+
99+
return np.append(theta1Grad.flatten(), theta2Grad.flatten())
13100

14-
#prepare Y matrix for cost function
15-
Y = getYMatrix(y)
16101

17-
#forward Pass
18-
[a1, z1, a2, z2, h2] = forwardPass(np.array([theta1, theta2]), X)
19-
20-
21-
#getting regulation parameters
22-
R1 = theta1[:,1:]
23-
R2 = theta2[:,1:]
24-
25-
# calculating the cost of regulation
26-
costRegulation = lambdaVal*(np.sum(np.square(R1.flatten())) + np.sum(np.square(R2.flatten())))/(2*m)
27-
28-
#calculating true cost without regulation
29-
cost = np.sum(np.log(np.extract(Y==1,h2))) + np.sum(np.log(1-np.extract(Y==0,h2)))
30-
31-
cost = -cost/m
32-
33-
#calculate total cost
34-
totalCost = cost + costRegulation
35-
36-
return totalCost
37-
38-
def BackPropagation(nnParams, inputSize, hiddenLayerSize, outputSize, X, y, lambdaVal):
39-
#get num examples
40-
m = X.shape[0]
41-
#get Theta Matrices
42-
[theta1, theta2] = getThetas(nnParams,inputSize,hiddenLayerSize,outputSize)
43-
44-
45-
#prepare Y matrix for cost function
46-
Y = getYMatrix(y) #5x3
102+
def forwardPass(nnParams, X):
103+
theta1 = nnParams[0]
104+
theta2 = nnParams[1]
47105

48-
#forward Pass
49-
[a1, z1, a2, z2, h2] = forwardPass(np.array([theta1, theta2]), X)
50-
#a1 = 5x4, z1 = 5x5, a2 = 5x5, a2 = 5x6, z2 = 5x3, h2 = 5x3
106+
# left side is the example count
107+
# layer 1
108+
a1 = np.insert(X, 0, np.ones(X.shape[0]), axis=1) # 5x4
109+
z1 = np.matmul(a1, theta1.transpose()) # 5x5
110+
a2 = sigmoid(z1) # 5x5
51111

52-
#backward
53-
theta2Error = h2-Y #5x3
54-
theta1Error = np.matmul(theta2Error,theta2[:,1:])*sigmoidGradient(z1)
55-
56-
D1 = np.matmul(theta1Error.transpose(),a1)
57-
D2 = np.matmul(theta2Error.transpose(),a2)
112+
# layer 2
113+
a2 = np.insert(
114+
a2,
115+
0,
116+
np.ones(a1.shape[0]), axis=1) # adding bias unit 5x6
58117

59-
#average the gradient per example
60-
theta1Grad = D1/m
61-
theta2Grad = D2/m
118+
z2 = np.matmul(a2, theta2.transpose()) # 5x3
119+
a3 = sigmoid(z2) # 5x3
62120

63-
#calculate regulation terms
64-
theta1Reg = lambdaVal*theta1/m
65-
theta2Reg = lambdaVal*theta2/m
66-
theta1Reg[:,0] = 0
67-
theta2Reg[:,0] = 0
121+
return [a1, z1, a2, z2, a3]
68122

69-
#combine gradient and regulation terms
70-
theta1Grad = theta1Grad + theta1Reg
71-
theta2Grad = theta2Grad + theta2Reg
72123

73-
return np.append(theta1Grad.flatten(), theta2Grad.flatten())
124+
def predictNN(nnParams, X):
125+
results = forwardPass(nnParams, X)
126+
pred = results[4]
127+
return (np.argmax(pred, axis=1)+1)
74128

75-
def forwardPass(nnParams, X):
76-
theta1 = nnParams[0]
77-
theta2 = nnParams[1]
78129

79-
#left side is the example count
80-
#layer 1
81-
a1 = np.insert(X,0,np.ones(X.shape[0]),axis=1)#5x4
82-
z1 = np.matmul(a1,theta1.transpose())#5x5
83-
a2 = sigmoid(z1)#5x5
130+
def nnAccuracy(nnParams, X, inputLayerSize, hiddenLayerSize, numLabels, y):
84131

132+
thetas = getThetas(
133+
nnParams,
134+
inputLayerSize,
135+
hiddenLayerSize,
136+
numLabels)
85137

86-
#layer 2
87-
a2 = np.insert(a2,0,np.ones(a1.shape[0]),axis=1) # adding bias unit 5x6
88-
z2 = np.matmul(a2,theta2.transpose()) #5x3
89-
a3 = sigmoid(z2) #5x3
138+
p = predictNN(thetas, X)
90139

91-
return [a1, z1, a2, z2, a3]
140+
predictions = np.zeros(p.shape)
141+
predictions[np.where(p == y)] = 1
92142

93-
def predictNN(nnParams, X):
94-
results = forwardPass(nnParams, X)
95-
pred = results[4]
96-
return(np.argmax(pred,axis=1)+1)
143+
return np.mean(predictions) * 100
97144

98-
def nnAccuracy(nnParams, X, inputLayerSize, hiddenLayerSize, numLabels, y):
99145

100-
thetas = getThetas(nnParams, inputLayerSize, hiddenLayerSize, numLabels)
101-
102-
p = predictNN(thetas, X)
146+
def getYMatrix(y):
147+
# prepare Y matrix for cost function
148+
numLabels = np.unique(y).shape[0]
103149

104-
predictions = np.zeros(p.shape)
105-
predictions[np.where(p==y)] = 1
150+
# create boolean array of value or not out of 1s and 0s
151+
Y = (y == 1).astype(int)
152+
for i in range(2, numLabels + 1):
153+
Y = np.append(Y, (y == i).astype(int))
106154

107-
return np.mean(predictions) * 100
155+
# reshape so first dimension corresponds with label
156+
Y = Y.reshape(numLabels, y.shape[0])
157+
return Y.transpose()
108158

109-
def getYMatrix(y):
110-
#prepare Y matrix for cost function
111-
numLabels = np.unique(y).shape[0]
112159

113-
#create boolean array of value or not out of 1s and 0s
114-
Y = (y==1).astype(int)
115-
for i in range(2, numLabels + 1):
116-
Y = np.append(Y,(y==i).astype(int))
117-
#reshape so first dimension corresponds with label
118-
Y = Y.reshape(numLabels,y.shape[0])
119-
return Y.transpose()
160+
def getThetas(nnParams, inputSize, hiddenLayerSize, outputSize):
161+
theta1Length = (inputSize+1)*hiddenLayerSize
120162

121-
def getThetas(nnParams,inputSize,hiddenLayerSize,outputSize):
122-
theta1Length = (inputSize+1)*hiddenLayerSize
163+
theta1 = nnParams[:theta1Length]
164+
theta2 = nnParams[theta1Length:]
123165

124-
theta1 = nnParams[:theta1Length]
125-
theta2 = nnParams[theta1Length:]
166+
theta1 = theta1.reshape(hiddenLayerSize, inputSize+1)
167+
theta2 = theta2.reshape(outputSize, hiddenLayerSize+1)
126168

127-
theta1 = theta1.reshape(hiddenLayerSize,inputSize+1)
128-
theta2 = theta2.reshape(outputSize,hiddenLayerSize+1)
169+
return[theta1, theta2]
129170

130-
return[theta1, theta2]
131171

132172
def sigmoidGradient(Z):
133-
R = sigmoid(Z)
134-
return R*(1-R)
173+
R = sigmoid(Z)
174+
return R*(1-R)
135175

136-
def sigmoid(Z):
137-
return 1/(1+np.exp(-Z))
138176

139-
def optimizeNN(nnParams, inputSize, hiddenLayerSize, outputSize, X, y, lambdaVal, maxIter):
140-
return op.minimize(fun=nnCostFunction, x0=nnParams, args=(inputSize, hiddenLayerSize, outputSize, X, y, lambdaVal), method='TNC', jac=BackPropagation, options={'maxiter': maxIter, 'disp': True})
177+
def sigmoid(Z):
178+
return 1/(1+np.exp(-Z))
179+
180+
181+
def optimizeNN(
182+
nnParams,
183+
inputSize,
184+
hiddenLayerSize,
185+
outputSize,
186+
X,
187+
y,
188+
lambdaVal,
189+
maxIter):
190+
return op.minimize(
191+
fun=nnCostFunction,
192+
x0=nnParams,
193+
args=(
194+
inputSize,
195+
hiddenLayerSize,
196+
outputSize,
197+
X,
198+
y,
199+
lambdaVal),
200+
method='TNC',
201+
jac=BackPropagation,
202+
options={'maxiter': maxIter, 'disp': True})

‎ex5.py

+280-211
Large diffs are not rendered by default.

‎ex5helper.py

+187-112
Original file line numberDiff line numberDiff line change
@@ -2,136 +2,211 @@
22
import scipy.optimize as op
33
import matplotlib.pyplot as plt
44

5+
56
def linearRegressionCost(theta, X, y, lambdaVal):
6-
'''
7-
Calculate the cost for the linear regression model
8-
'''
9-
m = y.shape[0]
10-
X = np.insert(X,0,np.ones(X.shape[1]),axis=0)
7+
'''
8+
Calculate the cost for the linear regression model
9+
'''
10+
m = y.shape[0]
11+
X = np.insert(
12+
X,
13+
0,
14+
np.ones(X.shape[1]),
15+
axis=0)
1116

12-
pred = linearRegressionPredict(X, theta)
17+
pred = linearRegressionPredict(X, theta)
1318

14-
j = np.sum(np.power(pred-y,2))
15-
16-
reg = np.power(theta,2)
17-
reg[0] = 0
18-
reg = lambdaVal*np.sum(reg)
19+
j = np.sum(np.power(pred-y, 2))
1920

20-
return (j+reg)/(2*m)
21+
reg = np.power(theta, 2)
22+
reg[0] = 0
23+
reg = lambdaVal*np.sum(reg)
2124

22-
def linearRegressionGradient(theta, X, y, lambdaVal):
23-
'''
24-
Calculate the gradient for the linear regression model
25-
'''
26-
m = y.shape[0]
27-
X = np.insert(X,0,np.ones(X.shape[1]),axis=0)
28-
29-
prediction = linearRegressionPredict(X, theta)
30-
31-
error = prediction - y
32-
33-
grad = np.matmul(X,error)/m
34-
35-
reg = lambdaVal/m*theta
36-
reg[0] = 0
37-
38-
return (grad + reg)
25+
return (j+reg)/(2*m)
3926

40-
def linearRegressionPredict(X, theta, **kwargs):
41-
'''
42-
predict the value for the provided linear regression model
43-
'''
44-
addBias = kwargs.pop('addBias', False)
4527

46-
if(addBias):
47-
X = np.insert(X,0,np.ones(X.shape[1]),axis=0)
28+
def linearRegressionGradient(theta, X, y, lambdaVal):
29+
'''
30+
Calculate the gradient for the linear regression model
31+
'''
32+
m = y.shape[0]
33+
X = np.insert(
34+
X,
35+
0,
36+
np.ones(X.shape[1]),
37+
axis=0)
4838

49-
return np.matmul(X.transpose(),theta)
39+
prediction = linearRegressionPredict(X, theta)
5040

51-
def trainLinearRegressionModel(theta, X, y, lambdaVal):
52-
'''
53-
train the Linear Regression model
54-
'''
55-
return op.minimize(fun=linearRegressionCost, x0=theta, args=(X, y, lambdaVal), method='CG', jac=linearRegressionGradient)
41+
error = prediction - y
5642

57-
def learningCurve(X,y,Xval,yval,lambdaVal):
58-
'''
59-
Iterate throught each number of possible learning sample sizes and
60-
calculate the error for the training and validation sets
61-
'''
43+
grad = np.matmul(X, error)/m
6244

63-
m = X.shape[1]
64-
theta = np.ones(X.shape[0]+1)
45+
reg = lambdaVal/m*theta
46+
reg[0] = 0
6547

66-
errorTrain = np.array([])
67-
errorValidation = np.array([])
68-
for i in range(m):
69-
results = trainLinearRegressionModel(theta, X[:,:i+1], y[:i+1], lambdaVal)
70-
theta = results.x
71-
errorTrain = np.append(errorTrain,linearRegressionCost(theta, X[:,:i+1], y[:i+1], lambdaVal))
72-
errorValidation = np.append(errorValidation,linearRegressionCost(theta, Xval, yval, lambdaVal))
48+
return (grad + reg)
7349

74-
return [errorTrain, errorValidation]
7550

76-
def polyFeatures(X,p):
77-
'''
78-
map the features of X to polynomial features for nonlinear solutions
79-
'''
80-
results = X
51+
def linearRegressionPredict(X, theta, **kwargs):
52+
'''
53+
predict the value for the provided linear regression model
54+
'''
55+
addBias = kwargs.pop('addBias', False)
8156

82-
for i in range(2,p+1):
83-
results = np.append(results,np.power(X,i), axis=0)
57+
if(addBias):
58+
X = np.insert(
59+
X,
60+
0,
61+
np.ones(X.shape[1]),
62+
axis=0)
8463

85-
return results
64+
return np.matmul(X.transpose(), theta)
8665

87-
def featureNormalize(X,**kwargs):
88-
'''
89-
normalize X by subtracting the mean and dividing the results by the standard deviation
90-
'''
9166

92-
mean = kwargs.pop('mean', X.mean(axis=1))
93-
sigma = kwargs.pop('sigma', np.std(X,axis=1))
67+
def trainLinearRegressionModel(theta, X, y, lambdaVal):
68+
'''
69+
train the Linear Regression model
70+
'''
71+
return op.minimize(
72+
fun=linearRegressionCost,
73+
x0=theta,
74+
args=(
75+
X,
76+
y,
77+
lambdaVal),
78+
method='CG',
79+
jac=linearRegressionGradient)
80+
81+
82+
def learningCurve(X, y, Xval, yval, lambdaVal):
83+
'''
84+
Iterate throught each number of possible learning sample sizes and
85+
calculate the error for the training and validation sets
86+
'''
87+
88+
m = X.shape[1]
89+
theta = np.ones(X.shape[0]+1)
90+
91+
errorTrain = np.array([])
92+
errorValidation = np.array([])
93+
for i in range(m):
94+
results = trainLinearRegressionModel(
95+
theta,
96+
X[:, :i+1],
97+
y[:i+1],
98+
lambdaVal)
99+
100+
theta = results.x
101+
102+
errorTrain = np.append(
103+
errorTrain,
104+
linearRegressionCost(
105+
theta,
106+
X[:, :i+1],
107+
y[:i+1],
108+
lambdaVal))
109+
110+
errorValidation = np.append(
111+
errorValidation,
112+
linearRegressionCost(
113+
theta,
114+
Xval,
115+
yval,
116+
lambdaVal))
117+
118+
return [errorTrain, errorValidation]
119+
120+
121+
def polyFeatures(X, p):
122+
'''
123+
map the features of X to polynomial features for nonlinear solutions
124+
'''
125+
results = X
126+
127+
for i in range(2, p+1):
128+
results = np.append(
129+
results,
130+
np.power(X, i),
131+
axis=0)
132+
133+
return results
134+
135+
136+
def featureNormalize(X, **kwargs):
137+
'''normalize X by subtracting the mean and dividing the results by the standard deviation'''
138+
139+
mean = kwargs.pop(
140+
'mean',
141+
X.mean(axis=1))
142+
143+
sigma = kwargs.pop(
144+
'sigma',
145+
np.std(X, axis=1))
146+
147+
Xnormalized = (X.transpose()-mean)/sigma
148+
return Xnormalized.transpose()
94149

95-
Xnormalized = (X.transpose()-mean)/sigma
96-
return Xnormalized.transpose()
97150

98151
def plotFit(minX, maxX, X, theta, p):
99-
'''
100-
plot the linear regression line values
101-
'''
102-
valueMap = np.arange(minX-15, maxX+25, .05)
103-
valueMap = valueMap[np.newaxis,:]
104-
105-
valueMapPoly = polyFeatures(valueMap, p)
106-
107-
#calculating mean and standard deviation for normalizing valueMap
108-
mean = valueMapPoly.mean(axis=1)
109-
sigma = np.std(valueMapPoly,axis=1)
110-
111-
valueMapPoly = featureNormalize(valueMapPoly,mean=mean,sigma=sigma)
112-
113-
projection = linearRegressionPredict(valueMapPoly,theta,addBias=True)
114-
115-
plt.plot(valueMap[0,:],projection, label = "Regression Line", color='red', linestyle='--')
116-
117-
def validationCurve(X,y,Xval,yval,lambdaVector):
118-
'''
119-
Iterate through lamdba values and calculate training error
120-
and validation error to choose appropriate value for lambda
121-
'''
122-
theta = np.ones(X.shape[0]+1)
123-
124-
errorTrain = np.array([])
125-
errorValidation = np.array([])
126-
127-
128-
for lambdaVal in lambdaVector:
129-
130-
results = trainLinearRegressionModel(theta, X, y, lambdaVal)
131-
theta = results.x
132-
errorTrain = np.append(errorTrain,linearRegressionCost(theta, X, y, lambdaVal))
133-
errorValidation = np.append(errorValidation,linearRegressionCost(theta, Xval, yval, lambdaVal))
134-
135-
return [errorTrain, errorValidation]
136-
137-
152+
'''
153+
plot the linear regression line values
154+
'''
155+
valueMap = np.arange(minX-15, maxX+25, .05)
156+
valueMap = valueMap[np.newaxis, :]
157+
158+
valueMapPoly = polyFeatures(valueMap, p)
159+
160+
# calculating mean and standard deviation for normalizing valueMap
161+
mean = valueMapPoly.mean(axis=1)
162+
sigma = np.std(valueMapPoly, axis=1)
163+
164+
valueMapPoly = featureNormalize(
165+
valueMapPoly,
166+
mean=mean,
167+
sigma=sigma)
168+
169+
projection = linearRegressionPredict(
170+
valueMapPoly,
171+
theta,
172+
addBias=True)
173+
174+
plt.plot(
175+
valueMap[0, :],
176+
projection,
177+
label="Regression Line",
178+
color='red',
179+
linestyle='--')
180+
181+
182+
def validationCurve(X, y, Xval, yval, lambdaVector):
183+
'''
184+
Iterate through lamdba values and calculate training error
185+
and validation error to choose appropriate value for lambda
186+
'''
187+
theta = np.ones(X.shape[0]+1)
188+
189+
errorTrain = np.array([])
190+
errorValidation = np.array([])
191+
192+
for lambdaVal in lambdaVector:
193+
results = trainLinearRegressionModel(theta, X, y, lambdaVal)
194+
theta = results.x
195+
196+
errorTrain = np.append(
197+
errorTrain,
198+
linearRegressionCost(
199+
theta,
200+
X,
201+
y,
202+
lambdaVal))
203+
204+
errorValidation = np.append(
205+
errorValidation,
206+
linearRegressionCost(
207+
theta,
208+
Xval,
209+
yval,
210+
lambdaVal))
211+
212+
return [errorTrain, errorValidation]

‎ex6.py

+113-108
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
## Machine Learning Online Class
2-
# Exercise 6 | Support Vector Machines
3-
#
4-
# Instructions
5-
# ------------
6-
#
7-
# This file contains code that helps you get started on the
8-
# exercise. You will need to complete the following functions:
9-
#
10-
# gaussianKernel.m - complete
11-
# dataset3Params.m
12-
# processEmail.m
13-
# emailFeatures.m
14-
#
15-
# For this exercise, you will not need to change any code in this file,
16-
# or any other files other than those mentioned above.
17-
#
1+
"""Machine Learning Online Class
2+
Exercise 6 | Support Vector Machines
3+
Instructions
4+
------------
5+
This file contains code that helps you get started on the
6+
exercise. You will need to complete the following functions:
7+
gaussianKernel
8+
dataset3Params
9+
processEmail
10+
emailFeatures
11+
For this exercise, you will not need to change any code in this file,
12+
or any other files other than those mentioned above.
13+
"""
1814

1915
# Imports:
2016
import numpy as np
@@ -23,137 +19,146 @@
2319
from sklearn import svm
2420
import ex6helper as helper
2521

26-
## =============== Part 1: Loading and Visualizing Data ================
27-
# We start the exercise by first loading and visualizing the dataset.
28-
# The following code will load the dataset into your environment and plot
29-
# the data.
30-
#
31-
print('Loading and Visualizing Data ...')
3222

33-
# Load from ex6data1:
34-
# You will have X, y in your environment
23+
def main():
24+
# =============== Part 1: Loading and Visualizing Data ================
25+
# We start the exercise by first loading and visualizing the dataset.
26+
# The following code will load the dataset into your environment and plot
27+
# the data.
28+
print('Loading and Visualizing Data ...')
3529

36-
mat = io.loadmat('./data/ex6data1.mat')
30+
# Load from ex6data1:
31+
# You will have X, y in your environment
3732

38-
X = mat['X']
33+
mat = io.loadmat('./data/ex6data1.mat')
3934

40-
y = mat['y'].astype(int).ravel()
35+
X = mat['X']
4136

42-
helper.plotData(X,y, addBias=True)
43-
plt.show()
37+
y = mat['y'].astype(int).ravel()
4438

45-
input('\nPart 1 completed. Program paused. Press enter to continue: ')
39+
helper.plotData(X, y, addBias=True)
40+
plt.show()
4641

47-
## ==================== Part 2: Training Linear SVM ====================
48-
# The following code will train a linear SVM on the dataset and plot the
49-
# decision boundary learned.
50-
#
42+
input('\nPart 1 completed. Program paused. Press enter to continue: ')
5143

52-
print('\nTraining Linear SVM ...')
44+
# ==================== Part 2: Training Linear SVM ====================
45+
# The following code will train a linear SVM on the dataset and plot the
46+
# decision boundary learned.
47+
print('\nTraining Linear SVM ...')
5348

54-
# You should try to change the C value below and see how the decision
55-
# boundary varies (e.g., try C = 1000)
56-
C = 1
49+
# You should try to change the C value below and see how the decision
50+
# boundary varies (e.g., try C = 1000)
51+
C = 1
5752

58-
model = svm.SVC(C=1, max_iter=100, tol=.01, kernel='linear')
59-
model.fit(X,y)
53+
model = svm.SVC(
54+
C=1,
55+
max_iter=100,
56+
tol=.01,
57+
kernel='linear')
6058

59+
model.fit(X, y)
60+
helper.visualizeBoundary(X, y, model)
61+
plt.show()
6162

62-
helper.visualizeBoundary(X,y,model)
63-
plt.show()
63+
input('\nPart 2 completed. Program paused. Press enter to continue: ')
6464

65-
input('\nPart 2 completed. Program paused. Press enter to continue: ')
65+
# =============== Part 3: Implementing Gaussian Kernel ===============
66+
# You will now implement the Gaussian kernel to use
67+
# with the SVM. You should complete the code in gaussianKernel
6668

67-
#% =============== Part 3: Implementing Gaussian Kernel ===============
68-
# You will now implement the Gaussian kernel to use
69-
# with the SVM. You should complete the code in gaussianKernel.m
70-
#
71-
print('\nEvaluating the Gaussian Kernel ...')
69+
print('\nEvaluating the Gaussian Kernel ...')
7270

73-
X1 = np.array([1, 2, 1])
74-
X2 = np.array([0, 4, -1])
75-
sim = helper.gaussianKernel(X1, X2, sigma=2)
71+
X1 = np.array([1, 2, 1])
72+
X2 = np.array([0, 4, -1])
73+
sim = helper.gaussianKernel(X1, X2, sigma=2)
7674

77-
print('Gaussian Kernel between x1 = [1; 2; 1], x2 = [0; 4; -1], sigma = {:.6f}'.format(sim))
78-
print('(for sigma = 2, this value should be about 0.324652)')
75+
print('Gaussian Kernel between x1 = [1; 2; 1], x2 = [0; 4; -1], sigma = {:.6f}'.format(sim))
76+
print('(for sigma = 2, this value should be about 0.324652)')
7977

80-
input('\nPart 3 completed. Program paused. Press enter to continue: ')
78+
input('\nPart 3 completed. Program paused. Press enter to continue: ')
8179

82-
## =============== Part 4: Visualizing Dataset 2 ================
83-
# The following code will load the next dataset into your environment and
84-
# plot the data.
85-
#
80+
# =============== Part 4: Visualizing Dataset 2 ================
81+
# The following code will load the next dataset into your environment and
82+
# plot the data.
8683

87-
print('\nLoading and Visualizing Data ...')
84+
print('\nLoading and Visualizing Data ...')
8885

89-
mat = io.loadmat('./data/ex6data2.mat')
86+
mat = io.loadmat('./data/ex6data2.mat')
9087

91-
X = mat['X']
92-
y = mat['y'].astype(int).ravel()
88+
X = mat['X']
89+
y = mat['y'].astype(int).ravel()
9390

94-
helper.plotData(X,y, addBias=True)
95-
plt.show()
91+
helper.plotData(X, y, addBias=True)
92+
plt.show()
9693

97-
input('\nPart 4 completed. Program paused. Press enter to continue: ')
94+
input('\nPart 4 completed. Program paused. Press enter to continue: ')
9895

99-
## ========== Part 5: Training SVM with RBF Kernel (Dataset 2) ==========
100-
# After you have implemented the kernel, we can now use it to train the
101-
# SVM classifier.
102-
#
103-
print('\nTraining SVM with RBF Kernel (this may take 1 to 2 minutes) ...')
96+
# ========== Part 5: Training SVM with RBF Kernel (Dataset 2) ==========
97+
# After you have implemented the kernel, we can now use it to train the
98+
# SVM classifier.
99+
print('\nTraining SVM with RBF Kernel (this may take 1 to 2 minutes) ...')
104100

105-
# Train the SVM with the Gaussian kernel on this dataset.
106-
sigma = 0.1
107-
gamma = np.power(sigma,-2.)
101+
# Train the SVM with the Gaussian kernel on this dataset.
102+
sigma = 0.1
103+
gamma = np.power(sigma, -2.)
108104

109-
model = svm.SVC(C=1, kernel='rbf', gamma=gamma)
110-
model.fit(X, y.flatten())
105+
model = svm.SVC(
106+
C=1,
107+
kernel='rbf',
108+
gamma=gamma)
111109

112-
helper.visualizeBoundary(X,y,model)
113-
plt.show()
110+
model.fit(X, y.flatten())
111+
helper.visualizeBoundary(X, y, model)
112+
plt.show()
114113

115-
input('\nPart 5 completed. Program paused. Press enter to continue: ')
114+
input('\nPart 5 completed. Program paused. Press enter to continue: ')
116115

117-
## =============== Part 6: Visualizing Dataset 3 ================
118-
# The following code will load the next dataset into your environment and
119-
# plot the data.
120-
#
116+
# =============== Part 6: Visualizing Dataset 3 ================
117+
# The following code will load the next dataset into your environment and
118+
# plot the data.
121119

122-
print('\nLoading and Visualizing Data ...')
120+
print('\nLoading and Visualizing Data ...')
123121

124-
mat = io.loadmat('./data/ex6data3.mat')
122+
mat = io.loadmat('./data/ex6data3.mat')
125123

126-
X = mat['X']
127-
y = mat['y'].astype(int).ravel()
124+
X = mat['X']
125+
y = mat['y'].astype(int).ravel()
128126

129-
helper.plotData(X,y, addBias=True)
130-
plt.show()
127+
helper.plotData(X, y, addBias=True)
128+
plt.show()
131129

132-
input('\nPart 6 completed. Program paused. Press enter to continue: ')
130+
input('\nPart 6 completed. Program paused. Press enter to continue: ')
133131

134-
## ========== Part 7: Training SVM with RBF Kernel (Dataset 3) ==========
135-
# This is a different dataset that you can use to experiment with. Try
136-
# different values of C and sigma here.
137-
#
132+
# ========== Part 7: Training SVM with RBF Kernel (Dataset 3) ==========
133+
# This is a different dataset that you can use to experiment with. Try
134+
# different values of C and sigma here.
135+
#
138136

139-
Xval = mat['Xval']
140-
yval = mat['yval'].astype(int).ravel()
137+
Xval = mat['Xval']
138+
yval = mat['yval'].astype(int).ravel()
141139

140+
# get optimal parameters
141+
[C, sigma] = helper.dataset3Params(
142+
X,
143+
y,
144+
Xval,
145+
yval)
142146

143-
#get optimal parameters
144-
[C, sigma] = helper.dataset3Params(X,y,Xval,yval)
145-
gamma = np.power(sigma,-2.)
147+
gamma = np.power(sigma, -2.)
146148

147-
print('\nFound C & Sigma: {} & {}'.format(C, sigma))
149+
print('\nFound C & Sigma: {} & {}'.format(C, sigma))
148150

149-
#train the model
150-
model = svm.SVC(C=C, kernel='rbf', gamma=gamma)
151-
model.fit(X, y.flatten())
151+
# train the model
152+
model = svm.SVC(
153+
C=C,
154+
kernel='rbf',
155+
gamma=gamma)
152156

157+
model.fit(X, y.flatten())
158+
helper.visualizeBoundary(X, y, model)
159+
plt.show()
153160

154-
#visualize data
155-
helper.visualizeBoundary(X,y,model)
156-
plt.show()
161+
input('\nPart 7 completed. Program completed. Press enter to exit: ')
157162

158-
159-
input('\nPart 7 completed. Program completed. Press enter to exit: ')
163+
if __name__ == '__main__':
164+
main()

0 commit comments

Comments
 (0)
Please sign in to comment.