learningCurve.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. for i = 1:m
  22. theta = trainLinearReg(X(1:i, :), y(1:i), lambda);
  23. error_train(i) = linearRegCostFunction(X(1:i, :), y(1:i), theta, 0);
  24. error_val(i) = linearRegCostFunction(Xval, yval, theta, 0);
  25. % Compute train/cross validation errors using training examples
  26. % X(1:i, :) and y(1:i), storing the result in
  27. % error_train(i) and error_val(i)
  28. ....
  29. end
  30. % ====================== YOUR CODE HERE ======================
  31. % Instructions: Fill in this function to return training errors in
  32. % error_train and the cross validation errors in error_val.
  33. % i.e., error_train(i) and
  34. % error_val(i) should give you the errors
  35. % obtained after training on i examples.
  36. %
  37. % Note: You should evaluate the training error on the first i training
  38. % examples (i.e., X(1:i, :) and y(1:i)).
  39. %
  40. % For the cross-validation error, you should instead evaluate on
  41. % the _entire_ cross validation set (Xval and yval).
  42. %
  43. % Note: If you are using your cost function (linearRegCostFunction)
  44. % to compute the training and cross validation error, you should
  45. % call the function with the lambda argument set to 0.
  46. % Do note that you will still need to use lambda when running
  47. % the training to obtain the theta parameters.
  48. %
  49. % Hint: You can loop over the examples with the following:
  50. %
  51. % for i = 1:m
  52. % % Compute train/cross validation errors using training examples
  53. % % X(1:i, :) and y(1:i), storing the result in
  54. % % error_train(i) and error_val(i)
  55. % ....
  56. %
  57. % end
  58. %
  59. % ---------------------- Sample Solution ----------------------
  60. % -------------------------------------------------------------
  61. % =========================================================================
  62. end