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 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))
# Add intercept term to X X_padded = np.column_stack((np.ones( (m, 1)), X_norm)) # Add a column of ones to x ## ================ Part 2: Gradient Descent ================ print('Running gradient descent...') # Choose some alpha value alpha = 0.01 num_iters = 400 # Init Theta and Run Gradient Descent theta = np.zeros((3, 1)) theta, J_history = gdm.gradientDescentMulti(X_padded, y, theta, alpha, num_iters) # Plot the convergence graph plt.plot(range(J_history.size), J_history, "-b", linewidth=2) plt.xlabel('Number of iterations') plt.ylabel('Cost J') plt.show(block=False) # Display gradient descent's result print('Theta computed from gradient descent: ') print("{:f}, {:f}, {:f}".format(theta[0, 0], theta[1, 0], theta[2, 0])) print("") # Estimate the price of a 1650 sq-ft, 3 br house # ====================== YOUR CODE HERE ====================== # Recall that the first column of X is all-ones. Thus, it does
ones = np.ones((len(x), 1)) X = np.hstack((ones, X)) #print(np.hstack((ones, X))) # Gradient Descent # 1) Try different values of alpha # 2) prediction (With feature normalisation) alpha = 0.009 #0.009, try 0.01, 0.009. num_iters = 350 # Init Theta and Run Gradient Descent theta = np.zeros((3, 1)) [theta, J_History] = gdm.gradientDescentMulti(X, y, theta, alpha, num_iters) print('Values of theta:') print(theta) plot.plot(J_History) plot.title('Convergence Graph') plot.xlabel('No Of Iterations') plot.ylabel('Cost J') ''' iteration = np.zeros((num_iters, 1)) for i in range(num_iters): iteration[i, :] = i plot.plot(iteration, J_History) ''' # Prediction
from featureNormalize import featureNormalize from computeCostMulti import computeCostMulti, computeCostMulti2 from gradientDescentMulti import gradientDescentMulti #3 data = pd.read_csv('ex1data2.txt', header=None) #read from dataset X = data.iloc[:, 0:2] # read first 2column y = data.iloc[:, 2] # read third column m = len(y) # number of training example data_top = data.head() # view first few rows of the data print(data_top) #3.1 X = featureNormalize(X) #3.2 ones = np.ones((m, 1)) y = y[:, np.newaxis] X = np.hstack((ones, X)) # adding the intercept term theta = np.zeros([3, 1]) iterations = 400 alpha = 0.01 J = computeCostMulti(X, y, theta) print('CostFunction: ', J) J1 = computeCostMulti2(X, y, theta) print('CostFunction2: ', J1) theta = gradientDescentMulti(X, y, theta, alpha, iterations) print('Theta from gradientDescent: ', theta)
def testGradientDescentMulti1(): X = array([[1.,0.]]) y = array([0.]) theta = array([0.,0.]) th = gradientDescentMulti(X, y, theta, 0, 0)[0] assert_array_equal(th, theta)
def testGradientDescentMulti8(): 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.]) th = gradientDescentMulti(X, y, theta, 0.05, 100)[0] assert_array_almost_equal(th, array([1.6225, 0.39764, -0.39422, 5.7765]), decimal=3)
def ex1_multi(): # Initialization # ================ Part 1: Feature Normalization ================ # Clear and Close Figures #clear ; close all; clc print('Loading data ...') # Load Data data = np.loadtxt('ex1data2.txt', delimiter=',') X = np.reshape(data[:, 0:2], (data.shape[0], 2)) y = np.reshape(data[:, 2], (data.shape[0], 1)) m = y.shape[0] # Print out some data points print('First 10 examples from the dataset: ') print(np.c_[X[0:10, :], y[0:10, :]].T) print('Program paused. Press enter to continue.') #input() # Scale features and set them to zero mean print('Normalizing Features ...') X, mu, sigma = featureNormalize(X) # Add intercept term to X X = np.c_[np.ones((m, 1)), X] # ================ Part 2: Gradient Descent ================ # ====================== YOUR CODE HERE ====================== # Instructions: We have provided you with the following starter # code that runs gradient descent with a particular # learning rate (alpha). # # Your task is to first make sure that your functions - # computeCost and gradientDescent already work with # this starter code and support multiple variables. # # After that, try running gradient descent with # different values of alpha and see which one gives # you the best result. # # Finally, you should complete the code at the end # to predict the price of a 1650 sq-ft, 3 br house. # # Hint: By using the 'hold on' command, you can plot multiple # graphs on the same figure. # Hint: At prediction, make sure you do the same feature normalization. # Begin: My code plotting for different learning rates alphas = [0.3, 0.1, 0.03, 0.01] colors = ['r', 'g', 'b', 'k'] short_iters = 50 fig = plt.figure() #hold on; plt.xlabel('Number of iterations') plt.ylabel('Cost J') for i in range(len(alphas)): _, J = gradientDescentMulti(X, y, np.reshape(np.zeros((3, 1)), (3, 1)), alphas[i], short_iters) plt.plot(range(len(J)), J, colors[i], markersize=2) plt.savefig('figure1.multi.png') # End: My code plotting for different learning rates print('Running gradient descent ...') # Choose some alpha value alpha = 0.01 num_iters = 400 # Init Theta and Run Gradient Descent theta = np.reshape(np.zeros((3, 1)), (3, 1)) theta, J_history = gradientDescentMulti(X, y, theta, alpha, num_iters) # Plot the convergence graph fig = plt.figure() plt.plot(range(len(J_history)), J_history, '-b', markersize=2) plt.xlabel('Number of iterations') plt.ylabel('Cost J') plt.savefig('figure2.multi.png') # Display gradient descent's result print('Theta computed from gradient descent: ') print(theta) print() # Estimate the price of a 1650 sq-ft, 3 br house # ====================== YOUR CODE HERE ====================== # Recall that the first column of X is all-ones. Thus, it does # not need to be normalized. #price = 0; % You should change this price = np.dot(np.r_[1, np.divide(np.subtract([1650, 3], mu), sigma)], theta) # ============================================================ print('Predicted price of a 1650 sq-ft, 3 br house (using gradient descent):\n $%f' % price) print('Program paused. Press enter to continue.') #input() # ================ Part 3: Normal Equations ================ print('Solving with normal equations...') # ====================== YOUR CODE HERE ====================== # Instructions: The following code computes the closed form # solution for linear regression using the normal # equations. You should complete the code in # normalEqn.m # # After doing so, you should complete this code # to predict the price of a 1650 sq-ft, 3 br house. # # Load Data data = np.loadtxt('ex1data2.txt', delimiter=',') X = np.reshape(data[:, 0:2], (data.shape[0], 2)) y = np.reshape(data[:, 2], (data.shape[0], 1)) m = y.shape[0] # Add intercept term to X X = np.c_[np.ones((m, 1)), X] # Calculate the parameters from the normal equation theta = normalEqn(X, y) # Display normal equation's result print('Theta computed from the normal equations: ') print(theta) print('') # Estimate the price of a 1650 sq-ft, 3 br house # ====================== YOUR CODE HERE ====================== price = np.dot([1, 1650, 3], theta) # You should change this # ============================================================ print('Predicted price of a 1650 sq-ft, 3 br house (using normal equations):\n $%f' % price) # http://scikit-learn.org/stable/auto_examples/linear_model/plot_ridge_coeffs.html # Using sklearn X = np.reshape(data[:, 0:2], (data.shape[0], 2)) y = np.reshape(data[:, 2], (data.shape[0], 1)) model = linear_model.Ridge(max_iter=num_iters, solver='lsqr') count = 200 alphas = np.logspace(-3, 1, count) coefs = np.zeros((count, 2)) errors = np.zeros((count, 1)) for i, alpha in enumerate(alphas): model.set_params(alpha=alpha) model.fit(X, y) coefs[i, :] = model.coef_ errors[i, 0] = metrics.mean_squared_error(model.predict(X), y) results = [(r'$\theta_1$', coefs[:, 0]), (r'$\theta_2$', coefs[:, 1]), ('MSE', errors)] for i, result in enumerate(results): label, values = result plt.figure() ax = plt.gca() ax.set_xscale('log') ax.plot(alphas, values) plt.xlabel(r'$\alpha$') plt.ylabel(label) plt.savefig('figure%d.multi.sklearn.png' % (i + 1)) #model = linear_model.LinearRegression() model = linear_model.Ridge(alpha=alpha, max_iter=num_iters, solver='lsqr') model.fit(X, y) print('Theta found: ') print('%f %f %f' % (model.intercept_[0], model.coef_[0, 0], model.coef_[0, 1])) print('Predicted price of a 1650 sq-ft, 3 br house (using sklearn):\n $%f' % model.predict([[1650, 3]]))
def testGradientDescentMulti5(): X = column_stack((ones(101), linspace(0,10,101))) y = sin(linspace(0,10,101)) theta = array([1.,-1.]) th = gradientDescentMulti(X, y, theta, 0.05, 100)[0] assert_array_almost_equal(th, array([0.5132, -0.0545]), decimal=3)
def testGradientDescentMulti4(): X = column_stack((ones(10), arange(10))) y = arange(10)*2 theta = array([1.,2.]) th = gradientDescentMulti(X, y, theta, 0.05, 100)[0] assert_array_almost_equal(th, array([0.2353, 1.9625]), decimal=3)
X, mu, sigma = featureNormalize(X) X = np.array(X) X = np.insert(X, 0, 1, axis=1) y = np.array(y).reshape(m, 1) ##================ Part 2: Gradient Descent ================## print('Running Gradient Descent...') alpha = 0.01 iterations = 400 theta = np.zeros([3, 1]) theta, J_history = gradientDescentMulti(X, y, theta, alpha, iterations) #Plot the convergence plot plt.plot(J_history) plt.xlabel('Number of iterations') plt.ylabel('Cost J') plt.show() print('Theta computed from gradient descent: \n', theta) X_pred = np.array([1650, 3]) X_predn = (X_pred - mu) / sigma X_predn = np.insert(X_predn, 0, 1) y_pred = X_predn.dot(theta) print(
# # Hint: By using the 'hold on' command, you can plot multiple # graphs on the same figure. # # Hint: At prediction, make sure you do the same feature normalization. # print 'Running gradient descent ...' # Choose some alpha value alpha = 0.01 num_iters = 400 # Init Theta and Run Gradient Descent theta = np.zeros(3) theta, theta_history, J_history = gradientDescentMulti(Xnorm, y, theta, alpha, num_iters) # Plot the convergence graph plotConvergence(J_history, num_iters) #dummy = plt.ylim([4,7]) plt.show(block=False) # Display gradient descent's result print 'Theta computed from gradient descent: ' print theta # Estimate the price of a 1650 sq-ft, 3 br house #We did: Xnorm, mu, sigma = featureNormalize(X) test = np.array([1650, 3]) scaled = (test - mu[1:]) / sigma[1:]
# Add intercept term to X X_padded = np.column_stack((np.ones((m,1)), X_norm)) # Add a column of ones to x ## ================ Part 2: Gradient Descent ================ print('Running gradient descent...') # Choose some alpha value alpha = 0.01 num_iters = 400 # Init Theta and Run Gradient Descent theta = np.zeros((3, 1)) theta, J_history = gdm.gradientDescentMulti(X_padded, y, theta, alpha, num_iters) # Plot the convergence graph plt.plot(xrange(J_history.size), J_history, "-b", linewidth=2 ) plt.xlabel('Number of iterations') plt.ylabel('Cost J') plt.show(block=False) # Display gradient descent's result print('Theta computed from gradient descent: ') print("{:f}, {:f}, {:f}".format(theta[0,0], theta[1,0], theta[2,0])) print("") # Estimate the price of a 1650 sq-ft, 3 br house # ====================== YOUR CODE HERE ====================== # Recall that the first column of X is all-ones. Thus, it does
# Add intercept term to X X = np.hstack((np.ones((m, 1)), X)) # ================ Part 2: Gradient Descent ================ # ====================== YOUR CODE HERE ==================== print('Running gradient descent ...') # Choose some alpha value alpha = 0.01 num_iters = 400 # Init Theta and Run Gradient Descent theta = np.zeros((3, 1)) theta, J_history = gradientDescentMulti(X, y, theta, alpha, num_iters) # Plot the convergence graph plt.plot(range(len(J_history)), J_history, '-b') plt.xlabel('Number of iterations') plt.ylabel('Cost J') plt.show() # Display gradient descent's result print('Theta computed from gradient descent:') print(theta) # Estimate the price of a 1650 sq-ft, 3 br house # ====================== YOUR CODE HERE ====================== price = 0 # You should change this
def testGradientDescentMulti7(): X = column_stack((ones(10), arange(10), arange(10))) y = arange(10)*2 theta = array([0.,0.,0.]) th = gradientDescentMulti(X, y, theta, 1, 1)[0] assert_array_almost_equal(th, array([9.,57.,57.]))
X, mu, sigma = featureNormalize(X) # Add intercept term to X X = np.vstack((np.ones(m), X.T)).T ## ================ Part 2: Gradient Descent ================ print('Running gradient descent ...\n') # Choose some alpha value alpha = 0.01 num_iters = 400 # Init Theta and Run Gradient Descent theta = np.zeros((3, 1)) theta, J_history = gradientDescentMulti(X, y, theta, alpha, num_iters) # Plot the convergence graph plt.ion() plt.figure() plt.plot(np.arange(0, J_history.size, 1), J_history, '-g') plt.xlabel('Number of iterations') plt.ylabel('Cost J') # Display gradient descent's result print('Theta computed from gradient descent: \n') print(theta) print('\n') temp = np.array([[1.0, 1650.0, 3.0]]) temp[0, 1:3] = (temp[0, 1:3] - mu) / sigma
## ================ Part 2: Gradient Descent ================ print('Running gradient descent ...') # perform linear regression on the data set alpha1 = 0.001 alpha2 = 0.01 alpha3 = 0.05 num_iters = 400 # Initialize Theta and run Gradient Descent theta = np.zeros((3, 1)) theta_1 = np.zeros((2, 1)) theta1, J_history1 = gradientDescentMulti(X_padded, y, theta, alpha1, num_iters) theta2, J_history2 = gradientDescentMulti(X_padded, y, theta, alpha2, num_iters) theta3, J_history3 = gradientDescentMulti(X_padded, y, theta, alpha3, num_iters) theta4, J_history4 = gradientDescentMulti(X_norm, y, theta_1, alpha2, num_iters) theta5, J_history5 = gradientDescentMulti(X, y, theta_1, alpha2, num_iters) print 'Theta 5 is:' print theta5 #get the cost (error) of the model #computeCostMulti(X, y, theta)