Commit 624d4444 authored by Hernandez Francois's avatar Hernandez Francois

ex2 done

parent c2d3f934
......@@ -20,12 +20,11 @@ grad = zeros(size(theta));
% Note: grad should have the same dimensions as theta
%
H = sigmoid(theta' * X');
J = 1/size(X,1) * ((-y' .* log(H)) - ((1-y') .* log(1-H))) * ones(size(X,1),1);
grad = 1/size(X,1) * ((H - y')*X)';
% =============================================================
......
......@@ -17,7 +17,11 @@ grad = zeros(size(theta));
% Compute the partial derivatives and set grad to the partial
% derivatives of the cost w.r.t. each parameter in theta
H = sigmoid(theta' * X');
J = 1/size(X,1) * ((-y' .* log(H)) - ((1-y') .* log(1-H))) * ones(size(X,1),1) + lambda/(2*size(X,1))*(theta.^2)'*[0;ones(size(theta,1)-1,1)];
grad = (1/size(X,1) * ((H - y')*X)') + (lambda/m * theta .* ([0;ones(size(theta,1)-1,1)]));
......
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
% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
% You should set J to the cost.
% Compute the partial derivatives and set grad to the partial
% derivatives of the cost w.r.t. each parameter in theta
H = sigmoid(theta' * X');
J = 1/size(X,1) * ((-y' .* log(H)) - ((1-y') .* log(1-H))) * ones(size(X,1),1) + lambda/(2*size(X,1))*(theta.^2)'*ones(size(theta,1),1);
grad = (1/size(X,1) * ((H - y')*X)') + lambda/m * theta .* ([0;ones(size(theta,1)-1,1)]));
% =============================================================
end
......@@ -12,12 +12,15 @@ figure; hold on;
% examples and 'ko' for the negative examples.
%
% Find Indices of Positive and Negative Examples
pos = find(y==1);
neg = find(y==0);
% Plot Examples
plot(X(pos,1), X(pos,2), 'k+', 'LineWidth', 2, 'MarkerSize', 7);
plot(X(neg,1), X(neg,2), 'ko', 'MarkerFaceColor', 'y', 'MarkerSize', 7);
% =========================================================================
......
......@@ -15,10 +15,9 @@ p = zeros(m, 1);
% You should set p to a vector of 0's and 1's
%
pos = find(sigmoid(theta'*X')>=0.5);
p(pos') = ones(size(pos,1),1);
% =========================================================================
......
......@@ -9,8 +9,7 @@ g = zeros(size(z));
% Instructions: Compute the sigmoid of each value of z (z can be a matrix,
% vector or scalar).
g = 1./(1.+exp(-z));
% =============================================================
......
File added
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment