functiong = sigmoid(z)
%SIGMOIDComputesigmoidfunctoon
% J = SIGMOID(z)computesthesigmoidofz.
% Youneedtoreturnthefollowingvariablescorrectlyg = zeros(size(z));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the sigmoid ofeach value of z (z can be a matrix,
% vector or scalar).
g = 1 ./ (1 + exp(-z)); % Ff operator'./'is replaced with'/', this expression will calculate the
% inverse matrix of (1 + exp(-z)).
% =============================================================
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
下面是 costFunction.m 的代码:
function [J, grad] = costFunction(theta, X, y)
%COSTFUNCTION Compute cost and gradient for logistic regression
% J = COSTFUNCTION(theta, X, y) computes the cost of using theta asthe
% parameter for logistic regression andthe gradient ofthe cost
% w.r.t. tothe parameters.
% Initialize some useful values
m = length(y); % numberof training examples
% You need toreturnthe 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 tothe cost.
% Compute the partial derivatives andset grad tothe partial
% derivatives ofthe cost w.r.t. each parameter in theta
%
% Note: grad should have the same dimensions as theta
%
J = 1 / m * sum(((-y) .* log(sigmoid(X * theta)) - (1 - y) .* log(1 - sigmoid(X * theta))));
grad = 1 / m * X' * (sigmoid(X * theta) - y);
% =============================================================
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
下面是 predict.m 的代码:
function p = predict(theta, X)
%PREDICT Predict whether the label is0or1 using learned logistic
%regression parameters theta
% p = PREDICT(theta, X) computes the predictions for X using a
% threshold at0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1)
m = size(X, 1); % Number of training examples
% You need toreturnthe following variables correctly
p = zeros(m, 1);
% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
% your learned logistic regression parameters.
% You should set p to a vector of0's and1's
%
temp = sigmoid(X * theta);
p = temp > 0.5; % If a element is positive, make it1, or keep it0
% =========================================================================
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
下面是 costFunctionReg.m 的代码:
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 asthe parameter for regularized logistic regression andthe
% gradient ofthe cost w.r.t. tothe parameters.
% Initialize some useful values
m = length(y); % numberof training examples
% You need toreturnthe 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 tothe cost.
% Compute the partial derivatives andset grad tothe partial
% derivatives ofthe cost w.r.t. each parameter in theta
[J, grad] = costFunction(theta,X,y);
J = J + lambda / (2*m) * (sum(theta.^2) - theta(1)^2); % no need to regularize theta 1
grad = grad + lambda / m * theta;
grad(1) = grad(1) - lambda / m * theta(1); % no need to regularize theta 1
% =============================================================
end