Skip to content

Instantly share code, notes, and snippets.

@prakhar1989
Created September 17, 2012 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prakhar1989/3736661 to your computer and use it in GitHub Desktop.
Save prakhar1989/3736661 to your computer and use it in GitHub Desktop.
regularization
function [J, grad] = costFunctionReg(theta, X, y, lambda)
m = length(y); % number of training examples
J = 0;
grad = zeros(size(theta));
[J, grad] = costFunction(theta, X,y);
reg = ( (sum(theta.^2) - theta(1,1)^2) * (lambda/(2*m)) )
J = J + reg;
for iter = 1:size(grad),
if (iter) > 1,
grad(iter) = grad(iter) + theta(iter, 1) * lambda / m;
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment