编程作业: Logistic Regression
plotData.m - Function to plot 2D classi cation data
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.
%
% 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);
% =========================================================================
hold off;
end
自己写的话就是
function plotData(X, y) % 定义一个函数plotData,形参是X,y
data = load('ex2data1.txt'); % 读取数据
X = data(:, [1, 2]); y = data(:, 3); % X为data的1,2列;y为data的3列
pos = find(y==1); neg = find(y==0); % pos是返回y==1的位置;neg...
figure; hold on; %创建数字窗口,保持
plot(X(pos, 1), X(pos, 2), 'k+', 'LineWidth', 2, 'MarkerSize', 7); % 绘制 , 用+标记,线宽为2,大小为7
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', 'MarkerSize', 7); %...
hold off;
end
sigmoid.m - Sigmoid Function
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 ./ (1 + exp(-z)); % 没啥好说的,S型函数
% =============================================================
end
costFunction.m - Logistic Regression Cost Function
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.
%(J = COSTFUNCTION(theta,X,y)计算使用theta作为逻辑回归的参数和成本相对于参数的梯度。)
% 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
%(说明:计算特定选择的theta的成本。你应该将J设置为cost。计算偏导数并将grad设置为相对于theta中每个参数的成本的偏导数)
% Note: grad should have the same dimensions as theta
%注意:grad应该与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
定义:
代价函数:
梯度:
predict.m - Logistic Regression Prediction Function
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)
% (使用已知的逻辑回归参数theta p = PREDICT(theta, X)预测标签是0还是1,
% 使用阈值0.5计算x的预测值(即如果sigmoid(theta'*x) >= 0.5,则预测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设为0和1的向量)
h = sigmoid(X * theta);
for i = 1:m
if (h(i))>0.5
p(i) = 1;
end
end
% =========================================================================
end
costFunctionReg.m - Regularized Logistic Regression Cost
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.
% (COSTFUNCTIONREG使用正则化进行逻辑回归的代价计算和使用
% 梯度J = COSTFUNCTIONREG(theta,X,y,lambda)
% 计算用theta作为正则逻辑回归的参数和相对于参数的梯度的代价。)
% 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
%(说明:计算特定选择的θ的代价。
您应该将J设置为代价。 计算偏导数并将grad设置为关于θ中每个参数的代价的偏导数)
% =============================================================
end