From 9e9b9990bb7fd4741ce3cefc4db0d289c7201da9 Mon Sep 17 00:00:00 2001 From: neingeist Date: Wed, 15 Oct 2014 19:54:07 +0200 Subject: [PATCH] Compute the gradient for regularized logistic regression --- ex2/costFunctionReg.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ex2/costFunctionReg.m b/ex2/costFunctionReg.m index deaae32..572bebf 100644 --- a/ex2/costFunctionReg.m +++ b/ex2/costFunctionReg.m @@ -20,6 +20,10 @@ grad = zeros(size(theta)); J = 1/m * (-y'*log(sigmoid(X*theta)) - (1-y)'*log(1-sigmoid(X*theta))) ... + lambda/(2*m) * theta(2:end)' * theta(2:end); +regularization_term = ... + lambda/m * (theta .* prepad(ones(length(theta)-1, 1), length(theta), 0)); +grad = 1/m * X' * (sigmoid(X*theta) - y) + regularization_term; + % ============================================================= end