1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| import numpy as np import matplotlib.pyplot as plt import pandas as pd from mpl_toolkits.mplot3d import Axes3D
data = pd.read_table("ex1data1.txt", header=None, delimiter=",", encoding="gb2312") m = len(data) data_x = np.array(data)[:, 0].reshape(m, 1) data_y = np.array(data)[:, 1].reshape(m, 1)
print(np.eye(5))
def computeCost(X, y, theta): inner = np.power(((X * theta) - y), 2) return np.sum(inner / (2 * len(X)))
def gradientDescent(X,y,theta,alpha,epoch): cost = np.zeros(epoch)
m = X.shape[0] for i in range(epoch): temp = theta - (alpha / m) * ((X * theta - y).T * X).T theta = temp cost[i] = computeCost(X,y,theta) return theta,cost
def normal_equations(X,y): theta = np.linalg.inv(X.T*X)*X.T*y return theta
X = np.matrix(np.concatenate((np.ones((m, 1)), data_x), axis=1)) y = np.matrix(data_y) theta = np.matrix(np.zeros([2, 1])) alpha = 0.01 epoch = 1000 final_theta,cost = gradientDescent(X,y,theta,alpha,epoch) print(final_theta) final_theta = normal_equations(X,y) print(final_theta) x = np.array(list(data_x[:,0])) f = final_theta[0,0] + final_theta[1,0] * x
theta0_vals = np.linspace(-10,10,100) theta1_vals = np.linspace(-1,4,100) J_vals = np.zeros((len(theta0_vals),len(theta1_vals))) for i in range(len(theta0_vals)): for j in range(len(theta1_vals)): t = np.matrix(np.array([theta0_vals[i],theta1_vals[j]]).reshape((2,1))) J_vals[i,j] = computeCost(X,y,t)
data2 = pd.read_table("ex1data2.txt", header=None, delimiter=",", encoding="gb2312")
data2 = (data2 - data2.mean()) / data2.std() m = len(data2) data_x = np.array(data2)[:, 0:2].reshape(m, 2) data_y = np.array(data2)[:, 2].reshape(m, 1) data_x = np.concatenate((np.zeros((m,1)),data_x),axis=1) X = np.matrix(data_x) y = np.matrix(data_y) theta = np.matrix(np.zeros((3,1))) final_theta,cost = gradientDescent(X,y,theta,alpha,epoch)
plt.plot(np.arange(epoch),cost,'r') plt.show()
|