感觉自己做的好慢,不懂的也太多,读英文文档障碍太多了……不过,这个必须慢慢克服,感觉自己在逐渐步入正轨。
1.1 plotData.m
完成这个函数,依照给定的数据进行描点。这个函数文档中已经给出了正确答案……所以这里是不用提交的。
function plotData(X, y)
%PLOTDATA Plots the data points X and y into a new figure
% PLOTDATA(x,y) plots the data points with + for the positive examples
% and o for the negative examples. X is assumed to be a Mx2 matrix.
% Create New Figure
figure; hold on;
% ====================== YOUR CODE HERE ======================
% Instructions: Plot the positive and negative examples on a
% 2D plot, using the option 'k+' for the positive
% examples and 'ko' for the negative examples.
%
% 根据 y 进行分组
y1 = find(y == 1);
y0 = find(y == 0);
% 描点
plot(X(y1, 1), X(y1, 2), 'k+', 'LineWidth', 2, 'MarkerSize', 7);
plot(X(y0, 1), X(y0, 2), 'ko', 'MarkerFaceColor', 'y', 'MarkerSize', 7);
% =========================================================================
hold off;
end
1.2.1 sigmoid.m
实现函数 hθ(x)=g(θTx) h θ ( x ) = g ( θ T x ) 中的 g(z) g ( z ) ,公式如下:
g(z)=11+e−z g ( z ) = 1 1 + e − z
,这里强调 z z 有可能是矩阵,所以需要考虑到矩阵的运算。 function g = sigmoid(z)
%SIGMOID Compute sigmoid function
% g = SIGMOID(z) computes the sigmoid of z.
% You need to return the following variables correctly
g = zeros(size(z));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the sigmoid of each value of z (z can be a matrix,
% vector or scalar).
g = 1 ./ (ones(size(z)) + e .^ (-z));
% =============================================================
end
1.2.2 costFunction.m
根据代价和梯度公式分别求代价和梯度,代价公式:
∂J(θ)∂θj=1m∑i=1m(hθ(x(i))−y(i))x(i)j ∂ J ( θ ) ∂ θ j = 1 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) x j ( i )
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 as the
% parameter for 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
%
% Note: grad should have the same dimensions as theta
%
h_theta = sigmoid(X * theta); % X: m * n theta: n * 1 h_theta: m * 1
J = (-y' * log(h_theta) - (1 - y)' * log(1 - h_theta)) / m; % y: m * 1
grad = X' * (h_theta - y) / m;
% =============================================================
end
1.2.4 predict.m
预测函数,返回判断结果为 0 or 1 0 &nbs***bsp; 1 。
function p = predict(theta, X)
%PREDICT Predict whether the label is 0 or 1 using learned logistic
%regression parameters theta
% p = PREDICT(theta, X) computes the predictions for X using a
% threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1)
m = size(X, 1); % Number of training examples
% You need to return the 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 of 0's and 1's
%
p = sigmoid(X * theta) > 0.5; % X: m * n theta: n * 1
% =========================================================================
end
2.3 costFunctionReg.m
和 costFunction.m c o s t F u n c t i o n . m 相似,不过加入了正则化处理,公式有所变化。代价公式:
J(θ)=1m∑i=1m[−y(i)log(hθ(x(i))−(1−y(i))log(1−hθ(x(i)))]+12m∑j=1nθ2j J ( θ ) = 1 m ∑ i = 1 m [ − y ( i ) l o g ( h θ ( x ( i ) ) − ( 1 − y ( i ) ) l o g ( 1 − h θ ( x ( i ) ) ) ] + 1 2 m ∑ j = 1 n θ j 2
梯度公式: ∂J(θ)∂θj=1m∑i=1m(hθ(x(i))−y(i))x(i)j, for j=0∂J(θ)∂θj=(1m∑i=1m(hθ(x(i))−y(i))x(i)j)+λmθj, for j≥1 ∂ J ( θ ) ∂ θ j = 1 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) x j ( i ) , f o r j = 0 ∂ J ( θ ) ∂ θ j = ( 1 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) x j ( i ) ) + λ m θ j , f o r j ≥ 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_theta = sigmoid(X * theta); % X: m * n theta: n * 1 h_theta: m * 1
J = (-y' * log(h_theta) - (1 - y)' * log(1 - h_theta)) / m + ...
lambda * (theta' * theta - (theta(1, 1))^2) / (2 * m); % y: m * 1
theta(1) = 0;
grad = (X' * (h_theta - y) + theta * lambda) / m;
% =============================================================
end
结果
最后应该有的实验结果在文档中都有给出,所以这里就不截图了。这次编程作业一共是两个部分,前者是逻辑回归算法,后者是在此基础上进行了正则化处理,不过两部分的数据是不一样的,刚好适应两种算法。
代码亲测,都是可以通过测试的。