Machine Learning - WEEK 1 2 3- 线性回归 、逻辑回归、梯度下降法及其优化算法、传统方法、 Octave 入门

本文为个人笔记,只记了重要内容,不适合新手入手

线性回归

  • 样本(x(i),y(i))i1,2,,m(x(i),y(i)),i∈1,2,…,m
  • x(i)=(x(i)1,x(i)2,,x(i)n)x(i)=(x1(i),x2(i),…,xn(i)) ,假设 x(i)x(i) 具有 nn 个特征
  • 假想函数(目标函数): 
     
    hθ(x(i))=θ0+θ1x(i)1+θ2x(i)2++θnx(i)nhθ(x(i))=θ0+θ1x1(i)+θ2x2(i)+…+θnxn(i)
  • hθ(x(i))hθ(x(i)) 表达式视具体情况而定
  • 线性回归 中 线性 的含义是参数 θjθj 都是一次的,而非 hθhθ 的自变量,回归 应该是代价函数 JJ 的自变量的回归
  • θ=(θ0,θ1,θ2,,θn)θ=(θ0,θ1,θ2,…,θn) 称为参数,Machine Learning 的目的就是求出参数的合适取值使得 hθ(x(i))hθ(x(i)) 更能体现 x(i)y(i)x(i)→y(i) 的映射关系
  • 为此我们提出了一个衡量参数取值好坏的函数——代价函数: 
     
    J(θ)=12mi=1m(hθ(x(i))y(i))2J(θ)=12m∑i=1m(hθ(x(i))−y(i))2
  • 现在问题转变为了求使得代价函数 J(θ)J(θ) 最小时的参数 θθ,下面给出两种方法:

梯度下降法

时间复杂度 O(kn2)O(kn2) ,适合当 n > 10000 时 (k 为学习步数) 

 
θj:=θjαJ(θ)θjθj:=θj−α∂J(θ)∂θj

 

  • αα 称学习速率(或步长),取值视情况而定,取值过大会导致不收敛;若当取值适当,在趋近极值时 J(θ)θj∂J(θ)∂θj 也会变小,所以此时梯度下降法是收敛的,所以没必要担心 αα 在趋近极值点的时候不改变导致无法收敛。
  • 注意要先求出所有的 tempj=θjαJ(θ)θjtempj=θj−α∂J(θ)∂θj ,再对所有的 θj=tempjθj=tempj

若取 x(i)0=1x0(i)=1 ,求偏导后可以写成: 

 
θj:=θjαmi=1m[(hθ(x(i))y(i))x(i)j]θj:=θj−αm∑i=1m[(hθ(x(i))−y(i))⋅xj(i)]

 

用矩阵来表述的话: 

 
θ:=θαm[XT(Xθy)]θ:=θ−αm[XT⋅(X⋅θ−y)]

 

  • θR(n+1)×1,XRm×(n+1),yRm×1θ∈R(n+1)×1,X∈Rm×(n+1),y∈Rm×1

特征优化:

尽量让 1<xi<1−1<xi<1 

 
xi:=xiμisixi:=xi−μisi

 

  • μiμi 为第 ii 个特征的平均取值,sisi 为第 ii 个特征的极差 (maxmin)(max−min)

传统方法

时间复杂度 O(n3)O(n3) ,适合当 n < 10000 时

直接令J(θ)θi=0∂J(θ)∂θi=0,解得: 

 
θ=(XTX)1XTyθ=(XTX)−1XTy


θR(n+1)×1,XRm×(n+1),yRm×1θ∈R(n+1)×1,X∈Rm×(n+1),y∈Rm×1

 

code

featureNormalize

function [X_norm, mu, sigma] = featureNormalize(X)
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
for iter = 1:size(X, 2)
    mu(iter) = mean(X(:, iter));
    sigma(iter) = std(X(:, iter));
    X_norm(:, iter) = (X(:, iter) - mu(iter)) / sigma(iter);
end
end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

computeCostMulti

function J = computeCost(X, y, theta)
m = length(y); % number of training examples
J = 1 / (2 * m) * sum((X * theta - y) .^ 2);
end
  • 1
  • 2
  • 3
  • 4

gradientDescentMulti

function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
    theta -= alpha / m * X' * (X * theta - y);   
    J_history(iter) = computeCostMulti(X, y, theta);
end
end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

normalEqn

function [theta] = normalEqn(X, y)
theta = zeros(size(X, 2), 1);
theta = pinv(X' * X) * X' * y;
end
  • 1
  • 2
  • 3
  • 4

Octave

https://www.gnu.org/software/octave/

Ubuntu Install:

sudo apt-add-repository ppa:octave/stable
sudo apt-get update
sudo apt-get install octave
  • 1
  • 2
  • 3
  • 4

Octave 入门:

内容比较多,推荐两篇文章

http://blog.csdn.net/weixin_36106941/article/details/64443944 
https://www.cnblogs.com/leezx/p/5635056.html

逻辑回归

线性回归是用来预测某个点的取值,逻辑回归是预测某个点具有某种特征的概率

为了达到我们的目的,重现建立模型: 

 
hθ(x)g(z)J(θ)=g(θTx)=11+ez=1mi=1mCost(hθ(x(i),y(i)))hθ(x)=g(θTx)g(z)=11+e−zJ(θ)=1m∑i=1mCost(hθ(x(i),y(i)))

 

 

 
Cost(hθ(x,y))=⎧⎩⎨⎪⎪log(hθ(x))log(1hθ(x))y=1y=0Cost(hθ(x,y))={−log(hθ(x))y=1−log(1−hθ(x))y=0


Note: y=0y=0 or 11 always , and log is ln

 

可以写成: 

 
Cost(hθ(x,y))=[ylog(hθ(x))+(1y)log(1hθ(x)]Cost(hθ(x,y))=−[y⋅log(hθ(x))+(1−y)⋅log(1−hθ(x)]

 

对代价函数求偏倒后发现和线性回归代价函数求偏倒的结果形式上是完全一样的: 

 
J(θ)θj=1mi=1m[(hθ(x(i))y(i))x(i)j]∂J(θ)∂θj=1m∑i=1m[(hθ(x(i))−y(i))⋅xj(i)]

 

cost function code

function [J, grad] = costFunction(theta, X, y)
m = length(y); % number of training examples
tmp = sigmoid(X*theta);
J = -1 / m * (y'*log(tmp)+(1-y)'*log(1-tmp));
grad = 1 / m * X' * (sigmoid(X * theta) - y);
end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

高级优化算法

Optimization algorithms: 
- Gradient descent 
- Conjugate gradient 
- BFGS 
- L-BFGS

Advantages: 
- No need to manually pick αα 
- Often faster than grdicent descent.

disadvantages: 
- More complex

调用 Octave 优化算法 example :

[first] 定义 cost function:

costFunction.m

function [jval, gradient] = costFunction(theta, X, y)
    % jval := J(theta)
    % gradient := grad J(theta)
  • 1
  • 2
  • 3

[then] 键入命令

options = optimset('GrandObj', 'on', 'MaxIter', '100');
initialTheta = zeros(n + 1, 1)
[optTheta, functionVal, exitFlag] = fminunc(@(t)costFunction(t, X, y), initialTheta, options);
  • 1
  • 2
  • 3
  • 4

正则化项

线性回归

为了防止 overfitting(过度拟合),对 cost function 引入了正则化项 λ2mni=1θ2iλ2m∑i=1nθi2 

 
J(θ)=12m[i=1m(hθ(x(i))y(i))2+λi=1nθ2i]J(θ)=12m[∑i=1m(hθ(x(i))−y(i))2+λ∑i=1nθi2]


求偏导: 

 
J(θ)θj=⎧⎩⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪1mi=1m(hθ(x(i))y(i))x(i)01m[i=1m(hθ(x(i))y(i))x(i)j+λθj]j=0j>0∂J(θ)∂θj={1m∑i=1m(hθ(x(i))−y(i))x0(i)j=01m[∑i=1m(hθ(x(i))−y(i))xj(i)+λ⋅θj]j>0


注意!!! θ0θ0 不需要惩罚

 

梯度下降法

Repeat { 

 
θ0θj:=θ0α1mi=1m(hθ(x(i))y(i))x(i)0:=θjα1m[i=1m(hθ(x(i))y(i))x(i)j+λθj]θ0:=θ0−α1m∑i=1m(hθ(x(i))−y(i))x0(i)θj:=θj−α1m[∑i=1m(hθ(x(i))−y(i))xj(i)+λ⋅θj]


}

 

对上整理后:

 

 
θj:=θj(1αλm)α1mi=1m(hθ(x(i))y(i))x(i)jθj:=θj(1−αλm)−α1m∑i=1m(hθ(x(i))−y(i))xj(i)

 

由于 1αλm<11−αλm<1 ,可以将 θj(1αλm)θj(1−αλm) 写成 θj0.99θj⋅0.99

常规方法

直接令J(θ)θi=0∂J(θ)∂θi=0,解得: 

 
θ=(XTX+λ⎡⎣⎢⎢⎢⎢⎢⎢⎢0000010000100001⎤⎦⎥⎥⎥⎥⎥⎥⎥(n+1)×(n+1))1XTyθ=(XTX+λ⋅[000⋯0010⋯0001⋯0⋮⋮⋮⋱⋮000⋯1]⏟(n+1)×(n+1))−1XTy

 

  • Suppose mnm≤n (m: examples , n : features)
  • 在 λ>0λ>0 时,括号内的矩阵总是可逆的
  • θR(n+1)×1,XRm×(n+1),yRm×1θ∈R(n+1)×1,X∈Rm×(n+1),y∈Rm×1

逻辑回归

对 cost function 引入了正则化项 λ2mni=1θ2iλ2m∑i=1nθi2 

 
J(θ)=1mi=1mCost(hθ(x(i),y(i)))+λ2mi=1nθ2i=1mi=1m[y(i)log(hθ(x(i)))+(1y(i))log(1hθ(x(i))]+λ2mi=1nθ2iJ(θ)=1m∑i=1mCost(hθ(x(i),y(i)))+λ2m∑i=1nθi2=−1m∑i=1m[y(i)⋅log(hθ(x(i)))+(1−y(i))⋅log(1−hθ(x(i))]+λ2m∑i=1nθi2


求偏导数后结果形式和线性回归是一样的: 

 
J(θ)θj=⎧⎩⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪1mi=1m(hθ(x(i))y(i))x(i)01m[i=1m(hθ(x(i))y(i))x(i)j+λθj]j=0j>0∂J(θ)∂θj={1m∑i=1m(hθ(x(i))−y(i))x0(i)j=01m[∑i=1m(hθ(x(i))−y(i))xj(i)+λ⋅θj]j>0

 

注意!!! θ0θ0 不需要惩罚

cost function code

function [J, grad] = costFunctionReg(theta, X, y, lambda)
tmp = sigmoid(X*theta);
J = -1 / m * (y'*log(tmp)+(1-y)'*log(1-tmp)) + lambda / (2 * m) * sum(theta(2:size(theta, 1),1) .^ 2);
theta(1) = 0;
grad = 1 / m * (X' * (tmp - y) + lambda * theta);
end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

https://www.coursera.org/learn/machine-learning 
教学方: Andrew Ng, Co-founder, Coursera; Adjunct Professor, Stanford University; formerly head of Baidu AI Group/Google Brain

posted @ 2018-05-09 15:31  jude_python  阅读(306)  评论(0编辑  收藏  举报