def gradientDescent(X, y, theta, alpha, num_iter): m, n = len(X), len(X[0]) J_history = np.zeros(num_iter) J_history[0] = cost(X, y, theta) for i in range(0, num_iter): theta = theta - np.sum((X @ theta - y) * X, axis=0).reshape( (n, 1)) * alpha / m # suan sum de fang shi shi bu dui de J_history[i] = cost(X, y, theta) return theta, J_history
def gradientDescent(x, y, theta, alpha, num_iters): m = np.size(y) J_history = np.zeros((num_iters, 1)) for iters in range(num_iters): theta = theta - alpha / m * np.dot(x.T, (np.dot(x, theta) - y)) J_history[iters] = cost(x, y, theta) return theta, J_history
def visual_J(X, y): theta0 = np.linspace(-10, 10, 100) theta1 = np.linspace(-1, 4, 100) J = np.zeros((len(theta0), len(theta1))) for i in range(len(theta0)): for j in range(len(theta1)): J[i, j] = cost(X, y, [[theta0[i]], [theta1[j]]]) fig = plt.figure() ax = fig.gca(projection='3d') ax.plot_surface(theta1, theta0, J) ax.set_xlabel(r'$\theta0$') ax.set_ylabel(r'$\theta1$')
plt.figure(0) plot_data(X, y) #=================== Part 3: Cost and Gradient descent =================== m = np.size(y) X = np.column_stack((np.ones(m), X)) # % Add a column of ones to x theta = np.zeros((2, 1)) # % initialize fitting parameters y = y.reshape(m, 1) #% Some gradient descent settings iterations = 1500 alpha = 0.01 print('Testing the cost function ...') #% compute and display initial cost J = cost(X, y, theta) print('With theta = [0 ; 0], Cost computed = {:.2f}'.format( J)) #(approx) 32.07\n #% further testing of the cost function J = cost(X, y, [[-1], [2]]) print('\nWith theta = [-1 ; 2], Cost computed = {:.2f}'.format(J)) #(approx) 54.24\n'); print('Running Gradient Descent ...') #% run gradient descent theta = gradientDescent(X, y, theta, alpha, iterations)[0] #% print theta to screen print('Theta found by gradient descent:', theta) #(approx):-3.6303 1.1664 # Plot the linear fit plt.plot(X[:, 1], np.dot(X, theta), '-')