def gradientDescentMulti(X, y, theta, alpha, num_iters): """ Performs gradient descent to learn theta theta = gradientDescent(x, y, theta, alpha, num_iters) updates theta by taking num_iters gradient steps with learning rate alpha """ # Initialize some useful values J_history = [] m = y.size # number of training examples for i in range(num_iters): # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCost) and gradient here. # h = np.dot(X, theta) theta = theta - ((alpha / m) * (np.dot(X.T, (h - y)))) # Save the cost J in every iteration J_history.append(computeCostMulti(X, y, theta)) # ============================================================ # Save the cost J in every iteration J_history.append(computeCostMulti(X, y, theta)) # ============================================================ return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, num_iters): """ Performs gradient descent to learn theta theta = gradientDescent(x, y, theta, alpha, num_iters) updates theta by taking num_iters gradient steps with learning rate alpha """ # Initialize some useful values] J_history = np.zeros((num_iters, )) m = np.size(y, 0) # number of training examples for i in range(num_iters): # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCost) and gradient here. # # ============================================================ theta = theta - alpha * (X.T.dot(X.dot(theta) - y) / m) J_history = computeCostMulti(X, y, theta) return theta, J_history
def output(partId): # Random Test Cases X1 = np.column_stack( (np.ones(20), np.exp(1) + np.exp(2) * np.linspace(0.1, 2, 20))) Y1 = X1[:, 1] + np.sin(X1[:, 0]) + np.cos(X1[:, 1]) X2 = np.column_stack((X1, X1[:, 1]**0.5, X1[:, 1]**0.25)) Y2 = np.power(Y1, 0.5) + Y1 if partId == '1': out = formatter('%0.5f ', warmUpExercise()) elif partId == '2': out = formatter('%0.5f ', computeCost(X1, Y1, np.array([0.5, -0.5]))) elif partId == '3': out = formatter( '%0.5f ', gradientDescent(X1, Y1, np.array([0.5, -0.5]), 0.01, 10)) elif partId == '4': out = formatter('%0.5f ', featureNormalize(X2[:, 1:4])) elif partId == '5': out = formatter( '%0.5f ', computeCostMulti(X2, Y2, np.array([0.1, 0.2, 0.3, 0.4]))) elif partId == '6': out = formatter( '%0.5f ', gradientDescentMulti(X2, Y2, np.array([-0.1, -0.2, -0.3, -0.4]), 0.01, 10)) elif partId == '7': out = formatter('%0.5f ', normalEqn(X2, Y2)) return out
def gradientDescentMulti(X, y, theta, alpha, num_iters): """ Performs gradient descent to learn theta theta = gradientDescent(x, y, theta, alpha, num_iters) updates theta by taking num_iters gradient steps with learning rate alpha """ # Initialize some useful values J_history = [] m = y.size # number of training examples for i in range(num_iters): # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCost) and gradient here. # #print computeCostMulti(X, y, theta) theta = theta - alpha * ( 1.0 / m) * np.transpose(X).dot(X.dot(theta) - np.transpose(y)) #origin_theta = theta #h = X.dot(origin_theta) #for index in xrange(len(theta)): # theta[index] -= alpha / m * sum((h - y) * X[:, index]) # ============================================================ # Save the cost J in every iteration J_history.append(computeCostMulti(X, y, theta)) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, num_iters): #GRADIENTDESCENTMULTI Performs gradient descent to learn theta # theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by # taking num_iters gradient steps with learning rate alpha # Initialize some useful values m = y.shape[0] # number of training examples J_history = np.reshape(np.zeros((num_iters, 1)), (num_iters, 1)) for i in range(num_iters): # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCostMulti) and gradient here. # theta = np.subtract(theta, (alpha / m) * np.dot(np.subtract(np.dot(X, theta), y).T, X).T) # ============================================================ # Save the cost J in every iteration J_history[i, 0] = computeCostMulti(X, y, theta) return (theta, J_history)
def gradientDescentMulti(X, y, theta, alpha, num_iters): """ Performs gradient descent to learn theta theta = gradientDescent(x, y, theta, alpha, num_iters) updates theta by taking num_iters gradient steps with learning rate alpha """ theta_history = [] J_history = np.zeros(num_iters) m = y.size # number of training examples for i in range(num_iters): # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCost) and gradient here. # ============================================================ #J = np.sum (1.0/(2*m) * (np.square(y_hat - y))) y_hat = np.dot(X, theta) theta = theta - alpha * (1.0 / m) * np.dot(X.T, y_hat - y) # Save the cost J in every iteration theta_history.append(theta) J_history[i] = computeCostMulti(X, y, theta) if i % 100 == 0: print J_history[i] return theta, theta_history, J_history
def gradientDescentMulti(X, y, theta, alpha, iters): m = y.shape[0] J_history = list(range(iters)) for i in range(iters): theta = theta - (alpha / m) * X.T.dot(X.dot(theta) - y) J_history[i] = computeCostMulti(X, y, theta) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, num_iters): #GRADIENTDESCENTMULTI Performs gradient descent to learn theta # theta = GRADIENTDESCENTMULTI(X, y, theta, alpha, num_iters) updates theta by # taking num_iters gradient steps with learning rate alpha # Initialize some useful values m = len(y) # number of training examples J_history = zeros(num_iters) for iteration in range(num_iters): # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCostMulti) and gradient here. # # ============================================================ # Save the cost J in every iteration J_history[iteration] = computeCostMulti(X, y, theta) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, num_iters): """Performs gradient descent to learn theta """ # Initialize some useful values m = len(y) # number of training examples J_history = np.zeros(num_iters) for iter_ in range(num_iters): # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCostMulti) and gradient here. # h = X @ theta theta -= alpha * (h - y) @ X / m # ============================================================ # Save the cost J in every iteration J_history[iter_] = computeCostMulti(X, y, theta) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, num_iters): """ Performs gradient descent to learn theta theta = gradientDescent(x, y, theta, alpha, num_iters) updates theta by taking num_iters gradient steps with learning rate alpha """ # Initialize some useful values J_history = [] m = y.size # number of training examples for i in range(num_iters): # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCost) and gradient here. # predictions = X.dot(theta).flatten() errors_x1 = (predictions - y) * X[:, 0] errors_x2 = (predictions - y) * X[:, 1] theta[0] = theta[0] - alpha * (1.0 / m) * errors_x1.sum() theta[1] = theta[0] - alpha * (1.0 / m) * errors_x2.sum() # ============================================================ # Save the cost J in every iteration J_history.append(computeCostMulti(X, y, theta)) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, num_iters): """ Performs gradient descent to learn theta theta = gradientDescent(x, y, theta, alpha, num_iters) updates theta by taking num_iters gradient steps with learning rate alpha """ # Initialize some useful values J_history = [] m = y.size # number of training examples for i in range(num_iters): # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCost) and gradient here. # # ============================================================ # Save the cost J in every iteration J_history.append(computeCostMulti(X, y, theta)) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, num_iters): """ Performs gradient descent to learn theta theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by taking num_iters gradient steps with learning rate alpha args: X: numpy array, Input y: numpy array, Output theta: numpy array, weight matrix alpha: learning rate num_iters: number of epochs return: theta: the last theta after updates J_history: values of cost function """ m = len(y) J_history = np.zeros((num_iters, 1)) y = y.reshape(-1, 1) for i in range(num_iters): h = X.dot(theta) error = h - y theta += -(alpha / m) * (X.T.dot(error)) J_history[i] = computeCostMulti(X, y, theta) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, num_iters): #GRADIENTDESCENTMULTI Performs gradient descent to learn theta # theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by # taking num_iters gradient steps with learning rate alpha # Initialize some useful values m = y.size # number of training examples J_history = np.zeros(num_iters) for iter in range(0, num_iters): # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCost) and gradient here. # # Vectorized theta = theta - alpha / m * np.dot(X.transpose(), (np.dot(X, theta) - y)) # ============================================================ # Save the cost J in every iteration J_history[iter] = computeCostMulti.computeCostMulti(X, y, theta) return theta
def gradientDescentMulti(X, y, theta, alpha, num_iters): """ Performs gradient descent to learn theta theta = gradientDescent(x, y, theta, alpha, num_iters) updates theta by taking num_iters gradient steps with learning rate alpha """ # Initialize some useful values J_history = [] m = y.size # number of training examples for i in range(num_iters): # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCost) and gradient here. # #work through hypothesis and subtract y h_gd = np.dot(X,theta) e_gd = (h_gd-y) #finish with dot product of xT and error grad = np.dot(np.transpose(X),e_gd) #solve for theta theta = theta-(alpha*grad/m) #print(computeCost(X,y,theta)) # ============================================================ # Save the cost J in every iteration J_history.append(computeCostMulti(X, y, theta)) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, iterations): import numpy as np from computeCostMulti import computeCostMulti m = X.shape[0] J_history = np.zeros((iterations, 1)) for i in range(iterations): theta = theta - alpha * np.transpose(X) @ (X.dot(theta) - y) / m J_history[i] = computeCostMulti(X, y, theta) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, num_iters): m = len(y) # number of training examples J_history = np.zeros((num_iters, 1)) l, o, n = featureNormalize.featureNormalize(y) y = (y - o) / n for iter in range(num_iters): h = np.dot(X, theta) divergence = h - y theta = theta - (alpha * (np.dot(X.T, divergence))) / m J_history[iter] = computeCostMulti.computeCostMulti(X, y, theta) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, iterations): m = len(y) J_history = np.zeros([iterations, 1]) for i in range(iterations): err = np.dot(X, theta) - y err = np.dot(X.T, err) theta = theta - (alpha / m) * err J_history[i] = computeCostMulti(X, y, theta) print(J_history) return theta
def gradientDescentMulti(X, y, theta, alpha, num_iters): """ X: [m,2] y: [m,1] theta: [2,1] """ m = y.shape[0] J_history = np.zeros((num_iters, 1)) for iter in range(0, num_iters): H = np.dot(X, theta) # [m,1] Errors = H - y # [m,1] theta = theta - alpha / m * np.dot(np.transpose(X), Errors) J_history[iter] = computeCostMulti(X, y, theta) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, num_iters): # Initialize some useful values m = len(y) # number of training examples J_history = np.zeros((num_iters, 1)) for iter in range(num_iters): error = np.dot(X,theta) - y theta = theta - alpha * np.dot(X.T,error) / m J_history[iter][0] = computeCostMulti(X, y, theta) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, num_iters): # Initialize some useful values m = y.shape[0] # number of training examples J_history = np.zeros((num_iters, 1)) for i in range(num_iters): # ====================== YOUR CODE HERE ====================== # ============================================================ # Save the cost J in every iteration J_history[i] = computeCostMulti(X, y, theta) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, iterations): J_history = [] for iters in range(iterations): predictions = X.dot(theta) se = (predictions - y) m = len(X) temp = [0, 0, 0] for i in range(len(theta)): temp[i] = alpha / m * sum(se * X[:, i].reshape(m, 1)) theta = theta - temp J = computeCostMulti(X, y, theta) J_history.append(J) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, num_iters): m = len(y) # number of training examples J_history = np.zeros( (num_iters, 1)) # Using visualize gradient of cost function # Perform a single gradient step on the parameter vector theta for i in range(num_iters): # Compute gradient of cost function h = X.dot(theta) err_vec = h - y grad_J = (1 / m) * X.T.dot(err_vec) theta = theta - alpha * grad_J # update theta # Save the cost J in every iteration J_history[i] = computeCostMulti(X, y, theta) return [theta, J_history]
def gradientDescentMulti(X, y, theta, alpha, num_iters): """ Performs gradient descent to learn theta theta = gradientDescent(x, y, theta, alpha, num_iters) updates theta by taking num_iters gradient steps with learning rate alpha """ # Initialize some useful values J_history = [] m = y.size # number of training examples for i in range(num_iters): hypothesis = np.dot(X, theta) theta = theta - (alpha / m) * np.dot(X.T, (hypothesis - y)) J_history.append(computeCostMulti(X, y, theta)) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, num_iters): '''updates theta by taking num_iters gradient steps with learning rate alpha''' # Initialize some useful values m = y.size # number of training examples J_history = np.zeros(num_iters) for iter in range(num_iters): # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCostMulti) and gradient here. # theta = theta - alpha * (X.dot(theta) - y).dot(X) / m # ============================================================ # Save the cost J in every iteration J_history[iter] = computeCostMulti(X, y, theta) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, num_iters): """Performs gradient descent to learn theta theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by taking num_iters gradient steps with learning rate alpha """ m = y.size #number of training examples J_history = np.zeros((num_iters, 1)) for iter in range(1, num_iters): #do linear regression with identity (f(x) = x) as an activation function prediction = np.dot(X, theta) errors = prediction - y delta = (1.0/m) * np.dot(X.T, errors) #update weight theta = theta - alpha * delta #save the cost J in every iteration J_history[iter] = costMultiModule.computeCostMulti(X, y, theta) return J_history, theta
def gradientDescentMulti(X, y, theta, alpha, num_iters): #gradientDescentMulti(X, y, theta, alpha, num_iters) performs gradient descent to learn theta # Initialize some useful values m = y.size # number of training examples J_history = np.zeros(num_iters) for iter in np.arange(num_iters): hypothesis = X.dot(theta) error_vector = hypothesis - y theta = theta - alpha * (1 / m) * (X.T.dot(error_vector)) # Save the cost in every iteration J_history[iter] = computeCostMulti(X, y, theta) return (theta, J_history)
def gradientDescentMulti(X, y, theta, alpha, num_iters): """ Performs gradient descent to learn theta theta = gradientDescent(x, y, theta, alpha, num_iters) updates theta by taking num_iters gradient steps with learning rate alpha """ # Initialize some useful values m = y.size # number of training examples n = theta.size # number of parameters cost_history = np.zeros(num_iters) theta_history = np.zeros((n, num_iters)) for i in range(num_iters): for j in range(m): theta -= alpha / m * ((X[j] @ theta - y[j]) * X[j]).reshape(n, 1) cost_history[i] = computeCostMulti(X, y, theta) theta_history[:, i] = theta.reshape((n, )) return theta, cost_history, theta_history
def output(partId): # Random Test Cases X1 = column_stack((ones(20), exp(1) + dot(exp(2), arange(0.1, 2.1, 0.1)))) Y1 = X1[:,1] + sin(X1[:,0]) + cos(X1[:,1]) X2 = column_stack((X1, X1[:,1]**0.5, X1[:,1]**0.25)) Y2 = Y1**0.5 + Y1 if partId == '1': return sprintf('%0.5f ', warmUpExercise()) elif partId == '2': return sprintf('%0.5f ', computeCost(X1, Y1, array([0.5, -0.5]))) elif partId == '3': return sprintf('%0.5f ', gradientDescent(X1, Y1, array([0.5, -0.5]), 0.01, 10)) elif partId == '4': return sprintf('%0.5f ', featureNormalize(X2[:,1:3])); elif partId == '5': return sprintf('%0.5f ', computeCostMulti(X2, Y2, array([0.1, 0.2, 0.3, 0.4]))) elif partId == '6': return sprintf('%0.5f ', gradientDescentMulti(X2, Y2, array([-0.1, -0.2, -0.3, -0.4]), 0.01, 10)) elif partId == '7': return sprintf('%0.5f ', normalEqn(X2, Y2))
def gradientDescentMulti(X,y,theta,alpha,num_iters): #import numpy as np for linear algebra manipulations import numpy as np #import compute cost function for mutiple variables import computeCostMulti as cCM J_history=np.zeros((num_iters,1),dtype=float) #number of training examples m=len(y) #the transpose of X.its not done in the loop since it would be very computationally expensive to do it every time Xtranspose=np.transpose(X) #for every iteration update theta for iter in range(0,num_iters): Xtheta=np.dot(X,theta) thetaminusy=Xtheta-y #update theta with every iteration theta-=np.dot((alpha/m),np.dot(Xtranspose,thetaminusy)) #theta = theta + alpha * (1/m) * np.dot((y - np.dot(X,theta)),X) J_history[iter,0]=cCM.computeCostMulti(X,y,theta) #return theta and cost after the loop has executed return [theta,J_history]
def gradientDescentMulti(X, y, theta, alpha, num_iters): '''%GRADIENTDESCENTMULTI Performs gradient descent to learn theta % theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by % taking num_iters gradient steps with learning rate alpha ''' # Initialize some useful values m = y.shape[0] # number of training examples J_history = []; for iter in range(num_iters): # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCostMulti) and gradient here. # #h = X * theta; #error = h - y; #theta_change = alpha * ((X' * error)/m); #theta = theta - theta_change; h = X.dot(theta) error = h - y; theta_change = alpha * (X.T.dot(error)/m) theta = theta - theta_change; # ============================================================ # Save the cost J in every iteration J_history.append(computeCostMulti(X, y, theta)); theta=theta.flatten() return theta,J_history
def gradientDescentMulti(X, y, theta, alpha, num_iters): """ Performs gradient descent to learn theta theta = gradientDescent(x, y, theta, alpha, num_iters) updates theta by taking num_iters gradient steps with learning rate alpha """ # Initialize some useful values J_history = [] m = y.size # number of training examples for i in range(num_iters): s0 = 0 s1 = 0 for r in range(X.shape[0]): Xr = X[r,] yr = y[r] s0 = s0 + (theta[0]+theta[1]*Xr[1]-yr)*Xr[0] s1 = s1 + (theta[0]+theta[1]*Xr[1]-yr)*Xr[1] s = np.array([s0,s1]) theta= theta- alpha * (s)/m J_history.append(computeCost(X, y, theta)) return theta, J_history # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCost) and gradient here. # # ============================================================ # Save the cost J in every iteration J_history.append(computeCostMulti(X, y, theta))
def gradientDescentMulti(X, y, theta, alpha, num_iters): m = len(y) J_history = np.zeros((num_iters, 1)) for i in range(num_iters): #h = theta[0] + (theta[1] * X[1]) #summation = (h[1] - y).sum() #temp1 = theta[0] - (alpha * (summation / m)) #temp2 = theta[1] - (alpha * ((summation * X[1]) / m)) #theta[0] = temp1 #theta[1] = temp2 h = X.dot(theta) error = h - y mul = X.T.dot(error) temp = theta - (alpha * (mul / m)) theta = temp #theta = temp # To display how much cost is reducing at every iteration. J_history[i] = ccm.computeCostMulti(X, y, theta) #print(J_history[i]) return (theta, J_history)
def gradientDescentMulti(X, y, theta, alpha, num_iters): # Initialize some useful values C_history = [] m = y.size # number of training examples for i in range(0, num_iters): # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCost) and gradient here. # theta = theta - alpha * (X.T.dot(X.dot(theta) - y) / m) # ============================================================ # Save the cost J in every iteration C_history.append(computeCostMulti(X, y, theta)) return theta, C_history
def gradientDescentMulti(X_norm, y, theta, alpha, num_iters): """ Performs gradient descent to learn theta theta = gradientDescent(x, y, theta, alpha, num_iters) updates theta by taking num_iters gradient steps with learning rate alpha """ # Initialize some useful values J_history = [] m = y.size # number of training examples for i in range(num_iters): # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCost) and gradient here. # # Gradient step for theta knot: xy_diff = (X_norm.dot(theta) - y) g_step = (alpha/m) * ( np.transpose(X_norm).dot(xy_diff) ) #update theta theta = (theta - g_step) #print([theta , g_step1, g_step0]) # ============================================================ # Save the cost J in every iteration J_history.append(computeCostMulti(X_norm, y, theta)) return theta, J_history
def gradientDescentMulti(X, y, theta, alpha, num_iters): #GRADIENTDESCENTMULTI Performs gradient descent to learn theta # theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by # taking num_iters gradient steps with learning rate alpha # Initialize some useful values m = len(y) # number of training examples J_history = np.zeros((num_iters, 1)) for iter in range(num_iters): # ====================== YOUR CODE HERE ====================== # Instructions: Perform a single gradient step on the parameter vector # theta. # # Hint: While debugging, it can be useful to print out the values # of the cost function (computeCostMulti) and gradient here. S = (np.dot(X, theta).transpose() - y) for j in range(X.shape[1]): theta[j] = theta[j] - (alpha / m) * np.sum(S * X[:, j]) # ============================================================ # Save the cost J in every iteration J_history[iter] = ccm.computeCostMulti(X, y, theta) return theta, J_history
y = fileIn[:, n-1:] print 'Normalizing features...' X, mu, sigma = dn.meanNormalization(X, 1) #add column of ones for intercept X = np.append(np.ones((m,1)), X, axis = 1) print 'Running gradient descent...' #set learning rate and number of iterations alpha = .01 num_iters = 400 #initialize theta with zeros theta = np.zeros((n,1)) J, theta, J_history = cm.computeCostMulti(X, y, theta, alpha, num_iters) #plot J at each iteration #should be decreasing every time x_axis = [q for q in range(num_iters)] plt.plot(x_axis, J_history) plt.title('Cost vs. Number of iterations') plt.xlabel('Number of Iterations') plt.ylabel('Cost') plt.show() print 'Final J {}'.format(J) print 'Final theta: {}'.format(theta) #print 'J history: {}'.format(J_history)
def testComputeCostMulti1(): X = array([[1., 0.]]) y = array([0.]) theta = array([0., 0.]) assert_equal(computeCostMulti(X, y, theta), 0)
def testComputeCostMulti4(): X = column_stack((ones(10), sin(arange(10)), cos(arange(10)), linspace(0.3,0.7,10))) y = arange(10) theta = array([1., 2., 3., 4.]) assert_almost_equal(computeCostMulti(X, y, theta), 7.3698431965)
def testComputeCostMulti3(): X = column_stack((ones(101), linspace(0,10,101))) y = sin(linspace(0,10,101)) theta = array([0., 0.]) assert_almost_equal(computeCostMulti(X, y, theta), 0.23699618)
def testComputeCostMulti2(): X = column_stack((ones(10), arange(10))) y = arange(10)*2 theta = array([1., 2.]) assert_almost_equal(computeCostMulti(X, y, theta), 0.5)