Path: blob/master/Week 3/Programming Assignment - 2/machine-learning-ex2/ex2/costFunction.m
863 views
function [J, grad] = costFunction(theta, X, y)1%COSTFUNCTION Compute cost and gradient for logistic regression2% J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the3% parameter for logistic regression and the gradient of the cost4% w.r.t. to the parameters.56% Initialize some useful values7m = length(y); % number of training examples89% You need to return the following variables correctly10J = 0;11grad = zeros(size(theta));1213% ====================== YOUR CODE HERE ======================14% Instructions: Compute the cost of a particular choice of theta.15% You should set J to the cost.16% Compute the partial derivatives and set grad to the partial17% derivatives of the cost w.r.t. each parameter in theta18%19% Note: grad should have the same dimensions as theta20%2122J = (-1/m) * sum ( (y.*log (sigmoid(X * theta))) + ((1-y).*log (1-sigmoid(X * theta))));23grad = (1/m) * (X'*((sigmoid(X * theta)) - y));24% =============================================================2526end272829