Skip to content

Instantly share code, notes, and snippets.

@fuwiak
Created October 22, 2021 17:23
Show Gist options
  • Save fuwiak/bd7f7cb186fe75989a614bf4d14d082a to your computer and use it in GitHub Desktop.
Save fuwiak/bd7f7cb186fe75989a614bf4d14d082a to your computer and use it in GitHub Desktop.
function [J, grad] = costFunction(theta, X, y)
%COSTFUNCTION Compute cost and gradient for logistic regression
% J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the
% parameter for logistic regression and the gradient of the cost
% w.r.t. to the parameters.
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));
h = sigmoid(X*theta);
J = ((-y)'*log(h)-(1-y)'*log(1-h))/m;
% calculate grads
grad = (X'*(h - y))/m;
% =============================================================
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment