a04.Andrew-ML03-分类、逻辑回归

2018-08-06  本文已影响11人  Xylia_Yang

Classification and Representation


01.Classification

要尝试分类,一种方法是是使用线性回归,将所有<0.5的映射为0;>0.5的映射为1。
但由于分类不是一个线性函数,所以这种方法不能很好工作。

02.Hypothesis Representation

s函数表达式
s函数

hθ(x)=P(y=1|x;θ)=1−P(y=0|x;θ)
P(y=0|x;θ)+P(y=1|x;θ)=1

03.Decision Boundary

hθ(x)≥0.5→y=1
hθ(x)<0.5→y=0

z=0,e0=1⇒g(z)=1/2
z→∞,e−∞→0⇒g(z)=1
z→−∞,e∞→∞⇒g(z)=0

线性决策边界.png 非线性决策边界.png

Logistic Regression Model


01.Cost Function

Cost(hθ(x),y)=−log⁡(1−hθ(x))

Cost(hθ(x),y)=0 if hθ(x)=y

02.Simplified Cost Function and Gradient Descent

Cost(hθ​(x),y)=−ylog(hθ​(x))−(1−y)log(1−hθ​(x))

向量表示形式
  1. 梯度下降算法的表达式如下(在逻辑回归模式中和线性回归是一致的):


    一般而言
改写导数部分

03.Advanced Optimization

Multiclass Classification:One-vs-all


一般式

Solving the Problem of Overfitting


01.The Problem of Overfitting

  1. 减少特征值得数量
    手动筛选或者使用一个选择模型算法
  2. Regularization(正规化)
    保持所有的特征,但是降低 θj的级数?

02.Cost Function

03.Regularized Linear Regression

一般化规则

1−αλ/m将一直小于1,所以每次迭代之后都会减少theta的值。

04.Regularized Logistic Regression

编程作业


01.Q

假设你是一个学校校长,根据申请人两次考试成绩,来预测他们被录取的概率

02. plotdata.m

function plotData(X, y)
%按照y的不同取值对X进行分类,不同的画成不同形状显示在图中
figure; hold on;

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

03.sigmoid.m

function g = sigmoid(z)
%用S函数将数值映射在0-1之间
g = zeros(size(z));

g= 1./(1+exp(-z))

% =============================================================

end

04.costFunction.m

function [J, grad] = costFunction(theta, X, y)
%计算代价函数和梯度下降函数

% 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));

J=((-y' * log (sigmoid (Xtheta)))-(1-y)'log(1-sigmoid(Xtheta)))/m;
grad =(X'
(sigmoid(X*theta)-y))./m;

% =============================================================

end

05.predict

function p = predict(theta, X)
%对给定数据进行预测,看是否达到准确率
m = size(X, 1); % Number of training examples

% You need to return the following variables correctly
p = zeros(m, 1);

p = floor(sigmoid(X * theta) .* 2)

% =========================================================================

end

06.costFunction

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
J = ((-y' * log(sigmoid(X * theta))) - (1 - y)' * log(1 - sigmoid(X * theta))) / m + (sum(theta .^ 2) - theta(1) ^ 2) * lambda / (2 * m);

grad(1) = (X(:, 1)' * (sigmoid(X * theta) - y)) ./ m;
for i = 2 : size(theta)
grad(i) = (X(:, i)' * (sigmoid(X * theta) - y)) ./ m + lambda * theta(i) / m

% =============================================================

end

上一篇 下一篇

猜你喜欢

热点阅读