learningCurve.m 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. function [error_train, error_val] = ...
  2. learningCurve(X, y, Xval, yval, lambda)
  3. %LEARNINGCURVE Generates the train and cross validation set errors needed
  4. %to plot a learning curve
  5. % [error_train, error_val] = ...
  6. % LEARNINGCURVE(X, y, Xval, yval, lambda) returns the train and
  7. % cross validation set errors for a learning curve. In particular,
  8. % it returns two vectors of the same length - error_train and
  9. % error_val. Then, error_train(i) contains the training error for
  10. % i examples (and similarly for error_val(i)).
  11. %
  12. % In this function, you will compute the train and test errors for
  13. % dataset sizes from 1 up to m. In practice, when working with larger
  14. % datasets, you might want to do this in larger intervals.
  15. %
  16. % Number of training examples
  17. m = size(X, 1);
  18. % You need to return these values correctly
  19. error_train = zeros(m, 1);
  20. error_val = zeros(m, 1);
  21. % ====================== YOUR CODE HERE ======================
  22. % ?????
  23. for i = 1:m
  24. X_train = X(1:i,:);
  25. y_train = y(1:i,:);
  26. theta = trainLinearReg(X_train, y_train, lambda);
  27. [error_train(i,:), grad] = linearRegCostFunction(X_train, y_train, theta, 0);
  28. [error_val(i,:), grad] = linearRegCostFunction(Xval, yval, theta, 0);
  29. endfor
  30. % -------------------------------------------------------------
  31. % =========================================================================
  32. end