Created
October 22, 2021 17:23
-
-
Save fuwiak/bd7f7cb186fe75989a614bf4d14d082a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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