You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

costFunction.m 1.0 kB

8 years ago
1234567891011121314151617181920212223242526272829303132
  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. J = 0;
  10. grad = zeros(size(theta));
  11. % ====================== YOUR CODE HERE ======================
  12. % Instructions: Compute the cost of a particular choice of theta.
  13. % You should set J to the cost.
  14. % Compute the partial derivatives and set grad to the partial
  15. % derivatives of the cost w.r.t. each parameter in theta
  16. %
  17. % Note: grad should have the same dimensions as theta
  18. %
  19. hx = sigmoid(X * theta); %hypothesis, m * 1
  20. J = 1 / m * sum(-y' * log(hx) - (1 .- y)' * log(1 -hx));
  21. grad = 1 / m * X' *(hx - y);
  22. % =============================================================
  23. end

机器学习

Contributors (1)