view costFunctionReg.m @ 4:4fb05328d3cf

Implement costFunctionReg
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Sat, 29 Oct 2011 22:29:53 -0500
parents 5664e0047b3e
children 141d81a2acf5
line wrap: on
line source

function [J, grad] = costFunctionReg(theta, X, y, lambda)
  ##COSTFUNCTIONREG Compute cost and gradient for logistic regression
  ##with regularization
  ##   J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using
  ##   theta as the parameter for regularized 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

  ## h_theta(x)
  ht = sigmoid (X*theta); 

  J = -sum (y.*log (ht) + (1 - y).*log (1 - ht))/m \
      + lambda*sum (theta(2:end).^2)/(2*m);

  grad = (X'*(ht - y) + [0; lambda*theta(2:end)])/m ;

endfunction