Diff of /costFunction.m [000000] .. [0e6e15]

Switch to unified view

a b/costFunction.m
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
7
% Initialize some useful values
8
m = length(y); % number of training examples
9
10
grad = zeros(size(theta));
11
12
h = sigmoid(X * theta);
13
J = -(1 / m) * sum( (y .* log(h)) + ((1 - y) .* log(1 - h)) );
14
15
for i = 1 : size(theta, 1)
16
    grad(i) = (1 / m) * sum( (h - y) .* X(:, i) );
17
end
18
19
end