def validationCurve(X, y, Xval, yval): #VALIDATIONCURVE Generate the train and validation errors needed to #plot a validation curve that we can use to select lambda # [lambda_vec, error_train, error_val] = ... # VALIDATIONCURVE(X, y, Xval, yval) returns the train # and validation errors (in error_train, error_val) # for different values of lambda. You are given the training set (X, # y) and validation set (Xval, yval). # # Selected values of lambda (you should not change this) lambda_vec = np.array([0, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1, 3, 10]) # You need to return these variables correctly. error_train = np.zeros((len(lambda_vec), 1)) error_val = np.zeros((len(lambda_vec), 1)) # ====================== YOUR CODE HERE ====================== # Instructions: Fill in this function to return training errors in # error_train and the validation errors in error_val. The # vector lambda_vec contains the different lambda parameters # to use for each calculation of the errors, i.e, # error_train(i), and error_val(i) should give # you the errors obtained after training with # lambda = lambda_vec(i) # # Note: You can loop over lambda_vec with the following: # # for i = 1:length(lambda_vec) # lambda = lambda_vec(i); # # Compute train / val errors when training linear # # regression with regularization parameter lambda # # You should store the result in error_train(i) # # and error_val(i) # .... # # end # # for i in range(len(lambda_vec)): lambda_val = lambda_vec[i] # learn theta parameters with current lambda value theta = trainLinearReg(X, y, lambda_val) # fill in error_train[i] and error_val[i] # note that for error computation, we set lambda = 0 in the last argument error_train[i] = linearRegCostFunction(X, y, theta, 0) error_val[i] = linearRegCostFunction(Xval, yval, theta, 0) # ========================================================================= return lambda_vec, error_train, error_val
def costFunc(theta): return linearRegCostFunction(X, y, theta, lambda_val, True)
# Plot training data plt.scatter(X, y, marker='x', s=60, color='r', lw=1.5) plt.ylabel('Water flowing out of the dam (y)') # Set the y-axis label plt.xlabel('Change in water level (x)') # Set the x-axis label plt.show() input('Program paused. Press <Enter> to continue...') ## =========== Part 2: Regularized Linear Regression Cost ============= # You should now implement the cost function for regularized linear # regression. # theta = np.array([1, 1]) J = linearRegCostFunction(np.column_stack((np.ones(m), X)), y, theta, 1) print( 'Cost at theta = [1 1]: %f \n(this value should be about 303.993192)\n' % J) input('Program paused. Press <Enter> to continue...') ## =========== Part 3: Regularized Linear Regression Gradient ============= # You should now implement the gradient for regularized linear # regression. # theta = np.array([1, 1]) J, grad = linearRegCostFunction(np.column_stack((np.ones(m), X)), y, theta, 1, True)
def learningCurve(X, y, Xval, yval, Lambda): """returns the train and cross validation set errors for a learning curve. In particular, it returns two vectors of the same length - error_train and error_val. Then, error_train(i) contains the training error for i examples (and similarly for error_val(i)). In this function, you will compute the train and test errors for dataset sizes from 1 up to m. In practice, when working with larger datasets, you might want to do this in larger intervals. """ # Number of training examples m, _ = X.shape # You need to return these values correctly error_train = np.zeros(m) error_val = np.zeros(m) # ====================== YOUR CODE HERE ====================== # Instructions: Fill in this function to return training errors in # error_train and the cross validation errors in error_val. # i.e., error_train(i) and # error_val(i) should give you the errors # obtained after training on i examples. # # Note: You should evaluate the training error on the first i training # examples (i.e., X(1:i, :) and y(1:i)). # # For the cross-validation error, you should instead evaluate on # the _entire_ cross validation set (Xval and yval). # # Note: If you are using your cost function (linearRegCostFunction) # to compute the training and cross validation error, you should # call the function with the lambda argument set to 0. # Do note that you will still need to use lambda when running # the training to obtain the theta parameters. # # Hint: You can loop over the examples with the following: # # for i = 1:m # # Compute train/cross validation errors using training examples # # X(1:i, :) and y(1:i), storing the result in # # error_train(i) and error_val(i) # .... # # # ---------------------- Sample Solution ---------------------- for i in range(1, m + 1): # define training variables for this loop X_train = X[:i] y_train = y[:i] # learn theta parameters with current X_train and y_train theta = trainLinearReg(X_train, y_train, Lambda) # fill in error_train(i) and error_val(i) # note that for error computation, we set lambda_val = 0 in the last argument error_train[i - 1] = linearRegCostFunction(X_train, y_train, theta, 0) error_val[i - 1] = linearRegCostFunction(Xval, yval, theta, 0) # ------------------------------------------------------------------------- # ========================================================================= return error_train, error_val