Skip to content

Instantly share code, notes, and snippets.

@parachvte
Last active August 25, 2017 06:52
Show Gist options
  • Select an option

  • Save parachvte/6ba6c3cdf5c13f16c898bd74efa273d5 to your computer and use it in GitHub Desktop.

Select an option

Save parachvte/6ba6c3cdf5c13f16c898bd74efa273d5 to your computer and use it in GitHub Desktop.
Logistic Regression Cost Function (Regularized)
function [J, grad] = costFunctionReg(theta, X, y, lambda)
m = length(y); % number of training examples
h = sigmoid(X * theta);
J = mean(-y .* log(h) - (1 - y) .* log(1 - h)) ...
+ lambda / 2.0 / m * (sum(theta .^ 2) - theta(1) ^ 2);
grad = X' * (h - y) / m + lambda / m * theta;
grad(1) -= lambda / m * theta(1);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment