[Coursera机器学习]Logistic Regression WEEK3编程作业

1.1 Visualizing the data

To help you get more familiar with plotting, we have left plotData.m empty so you can try to implement it yourself. However, this is an optional(ungraded) exercise. We also provide our implementation below so you can copy it or refer to it.

% 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);
1.2.1 Warmup exercise: sigmoid function

Your rst step is to implement this function in sigmoid.m so it can be called by the rest of your program. When you are nished, try testing a few values by calling sigmoid(x) at the Octave/MATLAB command line.

g(z)=11+ez

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the sigmoid of each value of z (z can be a matrix,
%               vector or scalar).
g = 1 ./ (1 + exp(-z));
1.2.2 Cost function and gradient

Now you will implement the cost function and gradient for logistic regression.
Complete the code in costFunction.m to return the cost and gradient.
Recall that the cost function in logistic regression is:

J(θ)=1mmi=1[y(i)log(hθ(x(i)))(1y(i))log(1hθ(x(i)))]

J(θ)θj=1mmi=1(hθ(x(i))y(i))x(i)j

J = (1 / m) * (-y' * log(sigmoid(X * theta)) - (1-y)' * log(1 - sigmoid(X*theta)));
grad = (1 / m) * X' * (sigmoid(X * theta) - y);
1.2.4 Evaluating logistic regression

Another way to evaluate the quality of the parameters we have found is to see how well the learned model predicts on our training set. In this part, your task is to complete the code in predict.m. The predict functionwill produce “1” or “0” predictions given a dataset and a learned parameter vector θ .

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
%               your learned logistic regression parameters. 
%               You should set p to a vector of 0's and 1's
%
p = round( sigmoid(X * theta) )
2.3 Cost function and gradient

Now you will implement code to compute the cost function and gradient for regularized logistic regression. Complete the code in costFunctionReg.m to return the cost and gradient.
Recall that the regularized cost function in logistic regression is:

J(θ)=1mmi=1[y(i)log(hθ(x(i)))(1y(i))log(1hθ(x(i)))]+λ2mnj=1θ2j

J(θ)θ0=1mmi=1(hθ(x(i))y(i))x(i)j           for j = 0

J(θ)θj=1mmi=1(hθ(x(i))y(i))x(i)j+λmθj           for j 1

% ====================== 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

n = size(theta, 1);
% You should not regularize theta(1)
theta_reg = [0; theta(2:n)];
J = (1 / m) * (-y' * log(sigmoid(X * theta)) - (1-y)' * log(1 - sigmoid(X*theta))) + lambda * (1 / (2 * m)) * sum(theta_reg .^ 2);
grad = (1 / m) * X' * (sigmoid(X * theta) - y) + lambda * (1 / m) * theta_reg;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值