function [error_train, error_val] = ... learningCurve(X, y, Xval, yval, lambda) % LEARNINGCURVE Generates the train and cross validation set errors needed to plot a learning curve % 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 = size(X, 1); m_Xval = size(Xval,1); % You need to return these values correctly error_train = zeros(m, 1); error_val = zeros(m, 1); % your code here for i = 1:m X_train = X(1:i,:); y_train = y(1:i); [theta_curve] = trainLinearReg([ones(i, 1) X_train],y_train,lambda); [J, grad] = linearRegCostFunction([ones(i, 1) X_train],y_train,theta_curve,0); error_train(i) = J; [J, grad] = linearRegCostFunction([ones(m_Xval, 1) Xval],yval,theta_curve,0); error_val(i) = J; end % code end
