Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mstampfer committed Oct 28, 2015
1 parent 4fb3086 commit b99de55
Show file tree
Hide file tree
Showing 121 changed files with 9,114 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.pyc
.idea
iterate.dat
.DS_Store
*~
token.txt
.git_
98 changes: 98 additions & 0 deletions Submission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from urllib import urlencode
from urllib2 import urlopen
from json import loads, dumps
from collections import OrderedDict
import numpy as np
import os

from getpass import getpass


class Submission():

def __init__(self, homework, part_names, srcs, output):
self.__homework = homework
self.__part_names = part_names
self.__srcs = srcs
self.__output = output
self.__submit_url = 'https://www-origin.coursera.org/api/onDemandProgrammingImmediateFormSubmissions.v1'
self.__login = None
self.__password = None

def submit(self):
print '==\n== Submitting Solutions | Programming Exercise %s\n==' % self.__homework
self.login_prompt()

parts = OrderedDict()
for part_id, _ in enumerate(self.__srcs,1):
parts[str(part_id)] = {'output': self.__output(part_id)}

result, response = self.request(parts)

response = loads(response)
print '=='
print '== %43s | %9s | %-s' % ('Part Name', 'Score', 'Feedback')
print '== %43s | %9s | %-s' % ('---------', '-----', '--------')
for part in parts:
partFeedback = response['partFeedbacks'][part]
partEvaluation = response['partEvaluations'][part]
score = '%d / %3d' % (partEvaluation['score'], partEvaluation['maxScore'])
print '== %43s | %9s | %-s' % (self.__part_names[int(part)-1], score, partFeedback)

evaluation = response['evaluation']
totalScore = '%d / %d' % (evaluation['score'], evaluation['maxScore'])
print '== --------------------------------'
print '== %43s | %9s | %-s\n' % (' ', totalScore, ' ')
print '=='

if not os.path.isfile('token.txt'):
with open('token.txt', 'w') as f:
f.write(self.__login + '\n')
f.writelines(self.__password)


def login_prompt(self):
try:
with open('token.txt', 'r') as f:
self.__login = f.readline().strip()
self.__password = f.readline().strip()
except IOError:
pass

if self.__login is not None and self.__password is not None:
reenter = raw_input('Use token from last successful submission (%s)? (Y/n): ' % self.__login)

if reenter == '' or reenter[0] == 'Y' or reenter[0] == 'y':
return

if os.path.isfile('token.txt'):
os.remove('token.txt')
self.__login = raw_input('login (Email address): ')
self.__password = getpass('Password: ')

def request(self, parts):

params = {
'assignmentSlug': self.__homework,
'secret': self.__password,
'parts': parts,
'submitterEmail': self.__login}

params = urlencode({'jsonBody': dumps(params)})
f = urlopen(self.__submit_url, params)
try:
return 0, f.read()
finally:
f.close()

def sprintf(fmt, arg):
"emulates (part of) Octave sprintf function"
if isinstance(arg, tuple):
# for multiple return values, only use the first one
arg = arg[0]

if isinstance(arg, (np.ndarray, list)):
# concatenates all elements, column by column
return ' '.join(fmt % e for e in np.asarray(arg).ravel('F'))
else:
return fmt % arg
Empty file added __init__.py
Empty file.
Empty file added ex1/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions ex1/computeCost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import numpy as np

def computeCost(X, y, theta):
"""
computes the cost of using theta as the parameter for linear
regression to fit the data points in X and y
"""
m = y.size
J = 0

# ====================== YOUR CODE HERE ======================
# Instructions: Compute the cost of a particular choice of theta
# You should set J to the cost.


# =========================================================================

return J


16 changes: 16 additions & 0 deletions ex1/computeCostMulti.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def computeCostMulti(X, y, theta):
"""
Compute cost for linear regression with multiple variables
J = computeCost(X, y, theta) computes the cost of using theta as the
parameter for linear regression to fit the data points in X and y
"""
m = y.size
J = 0
# ====================== YOUR CODE HERE ======================
# Instructions: Compute the cost of a particular choice of theta
# You should set J to the cost.


# =========================================================================

return J
161 changes: 161 additions & 0 deletions ex1/ex1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
from matplotlib import use, cm
use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d
from sklearn import linear_model

from gradientDescent import gradientDescent
from computeCost import computeCost
from warmUpExercise import warmUpExercise
from plotData import plotData
from show import show

## Machine Learning Online Class - Exercise 1: Linear Regression

# Instructions
# ------------
#
# This file contains code that helps you get started on the
# linear exercise. You will need to complete the following modules
# in this exericse:
#
# warmUpExercise.py
# plotData.py
# gradientDescent.py
# computeCost.py
# gradientDescentMulti.py
# computeCostMulti.py
# featureNormalize.py
# normalEqn.py
#
# For this exercise, you will not need to change any code in this file,
# or any other files other than those mentioned above.
#
# x refers to the population size in 10,000s
# y refers to the profit in $10,000s

# ==================== Part 1: Basic Function ====================
# Complete warmUpExercise.py
print 'Running warmUpExercise ...'
print '5x5 Identity Matrix:'
warmup = warmUpExercise()
print warmup
raw_input("Program paused. Press Enter to continue...")

# ======================= Part 2: Plotting =======================
data = np.loadtxt('ex1data1.txt', delimiter=',')
m = data.shape[0]
X = np.vstack(zip(np.ones(m),data[:,0]))
y = data[:, 1]

# Plot Data
# Note: You have to complete the code in plotData.py
print 'Plotting Data ...'
plotData(data)
show()

raw_input("Program paused. Press Enter to continue...")

# =================== Part 3: Gradient descent ===================
print 'Running Gradient Descent ...'
theta = np.zeros(2)

# compute and display initial cost
J = computeCost(X, y, theta)
print 'cost: %0.4f ' % J

# Some gradient descent settings
iterations = 1500
alpha = 0.01

# run gradient descent
theta, J_history = gradientDescent(X, y, theta, alpha, iterations)

# print theta to screen
print 'Theta found by gradient descent: '
print '%s %s \n' % (theta[0], theta[1])

# Plot the linear fit
plt.figure()
plotData(data)
plt.plot(X[:, 1], X.dot(theta), '-', label='Linear regression')
plt.legend(loc='upper right', shadow=True, fontsize='x-large', numpoints=1)
show()

raw_input("Program paused. Press Enter to continue...")

# Predict values for population sizes of 35,000 and 70,000
predict1 = np.array([1, 3.5]).dot(theta)
predict2 = np.array([1, 7]).dot(theta)
print 'For population = 35,000, we predict a profit of {:.4f}'.format(predict1*10000)
print 'For population = 70,000, we predict a profit of {:.4f}'.format(predict2*10000)

# ============= Part 4: Visualizing J(theta_0, theta_1) =============
print 'Visualizing J(theta_0, theta_1) ...'

# Grid over which we will calculate J
theta0_vals = np.linspace(-10, 10, X.shape[0])
theta1_vals = np.linspace(-1, 4, X.shape[0])

# initialize J_vals to a matrix of 0's
J_vals=np.array(np.zeros(X.shape[0]).T)

for i in range(theta0_vals.size):
col = []
for j in range(theta1_vals.size):
t = np.array([theta0_vals[i],theta1_vals[j]])
col.append(computeCost(X, y, t.T))
J_vals=np.column_stack((J_vals,col))

# Because of the way meshgrids work in the surf command, we need to
# transpose J_vals before calling surf, or else the axes will be flipped
J_vals = J_vals[:,1:].T
theta0_vals, theta1_vals = np.meshgrid(theta0_vals, theta1_vals)

# Surface plot
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(theta0_vals, theta1_vals, J_vals, rstride=8, cstride=8, alpha=0.3,
cmap=cm.coolwarm, linewidth=0, antialiased=False)
ax.set_xlabel(r'$\theta_0$')
ax.set_ylabel(r'$\theta_1$')
ax.set_zlabel(r'J($\theta$)')
show()

raw_input("Program paused. Press Enter to continue...")

# Contour plot
plt.figure()

# Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100
ax = plt.contour(theta0_vals, theta1_vals, J_vals, np.logspace(-2, 3, 20))
plt.clabel(ax, inline=1, fontsize=10)
plt.xlabel(r'$\theta_0$')
plt.ylabel(r'$\theta_1$')
plt.plot(0.0, 0.0, 'rx', linewidth=2, markersize=10)
show()

raw_input("Program paused. Press Enter to continue...")

# =============Use Scikit-learn =============
regr = linear_model.LinearRegression(fit_intercept=False, normalize=True)
regr.fit(X, y)

print 'Theta found by scikit: '
print '%s %s \n' % (regr.coef_[0], regr.coef_[1])

predict1 = np.array([1, 3.5]).dot(regr.coef_)
predict2 = np.array([1, 7]).dot(regr.coef_)
print 'For population = 35,000, we predict a profit of {:.4f}'.format(predict1*10000)
print 'For population = 70,000, we predict a profit of {:.4f}'.format(predict2*10000)

plt.figure()
plotData(data)
plt.plot(X[:, 1], X.dot(regr.coef_), '-', color='black', label='Linear regression wit scikit')
plt.legend(loc='upper right', shadow=True, fontsize='x-large', numpoints=1)
show()

raw_input("Program paused. Press Enter to continue...")


Loading

0 comments on commit b99de55

Please sign in to comment.