注意事项:
1. X = data[:,0]得到的是一个shape为(97,)的array,要reshape之后才能加入一列单位列向量
2. plt.xlabel()是一个函数,画图时加标签操作绝对不能写成plt.xlabel = ‘Population’
3. matplotlib库画三维图像,主要使用函数np.meshgrid()
4. 由于meshgrids的工作方式,在调用ax.plot_surface()和plt.contour()时需将J_vals转置
5. 创建等比数列,使用函数np.logspace(start, end, number),开始点和结束点都是10的幂
-linearReg.py
import numpy as np
import matplotlib.pyplot as plt
def warmUpExercise():
'An example function that returns the 5x5 identity matrix'
A = np.eye(5)
print(A)
def plotData(X,y):
'''
Plots the data points x and y into a new figure
PLOTDATA(x,y) plots the data points and gives the figure axes labels of
population and profit.
'''
plt.scatter(X,y,s=10,c='r',marker='x')
plt.xlabel('Population')
plt.ylabel('Profit')
plt.show()
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 = len(y) #number of training examples
predictions = np.dot(X, theta)
sqrErrors = (predictions - y )**2
J = 1 / (2*m) * sqrErrors.sum()
return J
def gradientDescent(X,y,theta,alpha,num_iters):
'''
GRADIENTDESCENT Performs gradient descent to learn theta
GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by
taking num_iters gradient steps with learning rate alpha
'''
m = len(y)
J_history = []
for i in range(num_iters):
predictions = np.dot(X, theta)
temp = (1 / m) * np.transpose(X).dot(predictions - y)
theta = theta - alpha * temp
# Save the cost J in every iteration
J_history.append(computeCost(X, y, theta))
return theta, J_history
-ex1.py
# Machine Learning Online Class - Exercise 1: Linear Regression
#
#
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from linearReg import warmUpExercise, plotData, computeCost, gradientDescent
#%%==================== Part 1: Basic Function ====================
print('Running warmUpExercise ... \n')
print('5x5 Identity Matrix: \n')
warmUpExercise()
print('Program paused. Press enter to continue.\n')
input()
#%%======================= Part 2: Plotting =======================
print('Plotting Data ...\n')
data = []
with open('ex1data1.txt','r') as infile:
for line in infile:
line = line.split(',')
data.append([float(line[0]),float(line[1])])
data = np.array(data)
X = data[:,0]
y = data[:,1]
m = len(y) # number of training examples
#Plot Data
plotData(X,y)
print('Program paused. Press enter to continue.\n')
input()
#%%=================== Part 3: Cost and Gradient descent ===================
X = X.reshape(m,1)
y = y.reshape(m,1)
X = np.hstack((np.ones((m,1)),X)) #Add a column of ones to x
theta = np.zeros((2, 1)) # initialize fitting parameters
#Some gradient descent settings
iterations = 1500
alpha = 0.01
print('\nTesting the cost function ...\n')
#compute and display initial cost
J = computeCost(X, y, theta)
print('With theta = [0 ; 0]\nCost computed = %f\n' % J)
print('Expected cost value (approx) 32.07\n')
#further testing of the cost function
J = computeCost(X, y, np.array([[-1],[2]]))
print('With theta = [-1 ; 2]\nCost computed = %f\n' % J)
print('Expected cost value (approx) 54.24\n')
print('Program paused. Press enter to continue.\n')
input()
# run gradient descent
theta,J_history = gradientDescent(X, y, theta, alpha, iterations)
# print theta to screen
print('Theta found by gradient descent:\n')
print('{}\n'.format(theta))
print('Expected theta values (approx)\n')
print(' -3.6303\n 1.1664\n\n')
# Plot the linear fit
plt.scatter(X[:,1],y,s=10,c='r',marker='x',label='Training data')
plt.plot(X[:,1], np.dot(X,theta),'-',label='Linear regression')
plt.xlabel('Population')
plt.ylabel('Profit')
plt.legend()
# 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 %f\n'% \
(predict1*10000))
print('For population = 70,000, we predict a profit of %f\n'% \
(predict2*10000))
print('Program paused. Press enter to continue.\n')
input()
#%% ============= Part 4: Visualizing J(theta_0, theta_1) =============
print('Visualizing J(theta_0, theta_1) ...\n')
#Grid over which we will calculate J
theta0_vals = np.linspace(-10, 10, 100,endpoint=False)
theta1_vals = np.linspace(-1, 4, 100,endpoint=False)
#initialize J_vals to a matrix of 0's
J_vals = np.zeros((len(theta0_vals), len(theta1_vals)))
# Fill out J_vals
for i in range(len(theta0_vals)):
for j in range(len(theta1_vals)):
t = np.array([[theta0_vals[i]], [theta1_vals[j]]])
J_vals[i,j] = computeCost(X, y, t)
# Surface plot
theta0_vals,theta1_vals = np.meshgrid(theta0_vals,theta1_vals)
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(theta0_vals, theta1_vals, J_vals.T, rstride=1, cstride=1, cmap=cm.viridis)
ax.set_xlabel('theta_0')
ax.set_ylabel('theta_1')
plt.show()
# Contour plot
# Plot J_vals as contours spaced logarithmically between 0.01 and 100
plt.plot(theta[0], theta[1], color='r',marker='x',linewidth=2, markersize=10)
plt.contour(theta0_vals, theta1_vals, J_vals.T, levels=np.logspace(-2,3,20),linewidths=0.5)
plt.xlabel('theta_0')
plt.ylabel('theta_1')