Path: blob/master/Week 6/Programming Assignment - 5/machine-learning-ex5/ex5/learningCurve.m
864 views
function [error_train, error_val] = ...1learningCurve(X, y, Xval, yval, lambda)2%LEARNINGCURVE Generates the train and cross validation set errors needed3%to plot a learning curve4% [error_train, error_val] = ...5% LEARNINGCURVE(X, y, Xval, yval, lambda) returns the train and6% cross validation set errors for a learning curve. In particular,7% it returns two vectors of the same length - error_train and8% error_val. Then, error_train(i) contains the training error for9% i examples (and similarly for error_val(i)).10%11% In this function, you will compute the train and test errors for12% dataset sizes from 1 up to m. In practice, when working with larger13% datasets, you might want to do this in larger intervals.14%1516% Number of training examples17m = size(X, 1);1819% You need to return these values correctly20error_train = zeros(m, 1);21error_val = zeros(m, 1);2223% ====================== YOUR CODE HERE ======================24% Instructions: Fill in this function to return training errors in25% error_train and the cross validation errors in error_val.26% i.e., error_train(i) and27% error_val(i) should give you the errors28% obtained after training on i examples.29%30% Note: You should evaluate the training error on the first i training31% examples (i.e., X(1:i, :) and y(1:i)).32%33% For the cross-validation error, you should instead evaluate on34% the _entire_ cross validation set (Xval and yval).35%36% Note: If you are using your cost function (linearRegCostFunction)37% to compute the training and cross validation error, you should38% call the function with the lambda argument set to 0.39% Do note that you will still need to use lambda when running40% the training to obtain the theta parameters.41%42% Hint: You can loop over the examples with the following:43%44% for i = 1:m45% % Compute train/cross validation errors using training examples46% % X(1:i, :) and y(1:i), storing the result in47% % error_train(i) and error_val(i)48% ....49%50% end51%52%theta = trainLinearReg(X, y, 0);53% Computing error_train and error_val54for i = 1:m55theta = trainLinearReg(X(1:i,:), y(1:i), lambda);56error_train(i) = linearRegCostFunction(X(1:i,:),y(1:i),theta,0);57error_val(i) = linearRegCostFunction(Xval, yval, theta, 0);58end;59%error_val = linearRegCostFunction(Xval, yval, theta, 0);60% ---------------------- Sample Solution ----------------------6162636465666768% -------------------------------------------------------------6970% =========================================================================7172end737475