costFunction.m 978 B

123456789101112131415161718192021222324252627282930313233
  1. function [J, grad] = costFunction(theta, X, y)
  2. %COSTFUNCTION Compute cost and gradient for logistic regression
  3. % J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the
  4. % parameter for logistic regression and the gradient of the cost
  5. % w.r.t. to the parameters.
  6. % Initialize some useful values
  7. %m = length(y); % number of training examples
  8. % You need to return the following variables correctly
  9. z = hypothesis(theta, X);
  10. J = mean(- y .* log(z) + (y - 1) .* log(1 - z));
  11. grad = mean((z - y) .* X);
  12. % ====================== YOUR CODE HERE ======================
  13. % Instructions: Compute the cost of a particular choice of theta.
  14. % You should set J to the cost.
  15. % Compute the partial derivatives and set grad to the partial
  16. % derivatives of the cost w.r.t. each parameter in theta
  17. %
  18. % Note: grad should have the same dimensions as theta
  19. %
  20. % =============================================================
  21. end