[Machine Learning]学习笔记-Logistic Regression

模型-二分类任务

Logistic regression,亦称logtic regression,翻译为“对数几率回归”,是一种分类学习方法。和先前的线性回归模型不同的是,输出的y一般是离散量的集合,如输出\(y \in \{0,1\}\)的二分类任务。

考虑二分类任务,线性回归模型产生的\(Z=\theta ^TX\)是连续的实值,需要用一个函数\(g(\theta ^TX)\)将z转换为0/1值。



可以采用对数几率函数(Logistic Function,亦称Sigmoid Function):

\[g(z)=\frac{1}{1+e^{-z}}
\]

至此,可以确定假设方程\(h_\theta(x)\)的形式:

\[\begin{align*}& h_\theta (x) = g ( \theta^T x ) \newline \newline& z = \theta^T x \newline& g(z) = \dfrac{1}{1 + e^{-z}}\end{align*}
\]

令\(y=g(z)\),可得:

\[\ln \frac{y}{1-y}=\theta^T x
\]

若将y视为样本为正例的可能性,则1-y为反例可能性。

上式可重写为:

\[\ln \frac{p(y=1 | x ; \theta)}{p(y=0 | x ; \theta)}=\theta^T x
\]

显然有:

\[p(y=1 | x ; \theta)=\frac{e^{\theta^T x}}{1+e^{\theta^T x}}=h_\theta (x)
\\p(y=0 | x ; \theta)=\frac{1}{1+e^{\theta^T x}}=1-h_\theta (x)
\]

可以由极大似然法(maximum likelihood method)来估计\(\theta\),

最大化似然概率\(L(\theta)\),即令每个样本属于其真实标记的概率越大越好:

\[\begin{equation*}
\begin{split}
L(\boldsymbol{\theta}) & =p(\mathbf{y}|\mathbf{X}; \boldsymbol{\theta}) \\
& =\prod_{i=1}^{m}p(y_{i}|\mathbf{x}_{i}; \boldsymbol{\theta}) \\
& =\prod_{i=1}^{m} (h_{\boldsymbol{\theta}}(\mathbf{x}_{i}))^{y_{i}} (1-h_{\boldsymbol{\theta}}(\mathbf{x}_{i}))^{1-y_{i}}
\end{split}
\end{equation*}
\]

为了方便求导,对等式两边同时取对数,将\(L(\theta)\)转换为凸函数(convex function),可得:

\[\begin{equation*}
\begin{split}
l(\boldsymbol{\theta}) & =\text{log}L(\boldsymbol{\theta}) \\
& = \sum_{i=1}^{m} y_{i} \text{log} h_(\mathbf{x}_{i})+(1-y_{i})\text{log}(1-h_(\mathbf{x_i}))
\end{split}
\end{equation*}
\]

要使\(l(\theta)\)达到最大值,可以构造代价函数\(J(\theta)\):

\[J(\theta) = - \frac{1}{m} \displaystyle \sum_{i=1}^m [y^{(i)}\log (h_\theta (x^{(i)})) + (1 - y^{(i)})\log (1 - h_\theta(x^{(i)}))]
\]

接下来就可以用梯度下降法求得\(J(\theta)\)的最小值了。

\[\begin{align*}& Repeat \; \lbrace \newline & \; \theta_j := \theta_j - \alpha \dfrac{\partial}{\partial \theta_j}J(\theta) \newline & \rbrace\end{align*}
\]

求偏导:

\[\begin{equation*}
\begin{split}
\frac{\partial }{\partial \theta_{j}}l(\boldsymbol{\theta}) & = -\frac{1}{m}\left ( \frac{y}{g(\boldsymbol{\theta}^{T}\mathbf{x})}-\frac{1-y}{1-g(\boldsymbol{\theta}^{T}\mathbf{x})} \right) \frac{\partial }{\partial \theta_{j}} g(\boldsymbol{\theta}^{T}\mathbf{x}) \\
& =-\frac{1}{m}\left( \frac{y}{g(\boldsymbol{\theta}^{T}\mathbf{x})}-\frac{1-y}{1-g(\boldsymbol{\theta}^{T}\mathbf{x})} \right) g(\boldsymbol{\theta}^{T}\mathbf{x}) (1-g(\boldsymbol{\theta}^{T}\mathbf{x})) \frac{\partial }{\partial \theta_{j}} \boldsymbol{\theta}^{T}\mathbf{x} \\
& =-\frac{1}{m}\left( y(1-g(\boldsymbol{\theta}^{T}\mathbf{x})) -(1-y) g(\boldsymbol{\theta}^{T}\mathbf{x}) \right)x_{j} \\
& =-\frac{1}{m}(y-g(\boldsymbol{\theta}^{T}\mathbf{x}))x_{j} \\
& =\frac{1}{m}(h_{\boldsymbol{\theta}}(\mathbf{x})-y)x_{j} \\
\end{split}
\end{equation*}\]

化简后可得:

\[\begin{align*} & Repeat \; \lbrace \newline & \; \theta_j := \theta_j - \frac{\alpha}{m} \sum_{i=1}^m (h_\theta(x^{(i)}) - y^{(i)}) x_j^{(i)} \newline & \rbrace \end{align*}
\]

week 3的课中介绍了matlab中采用梯度下降法的优化函数:fminunc

只要写出如下形式的代价函数后:

function [J, grad] = costFunction(theta, X, y)
J = 0;
grad = zeros(size(theta));
rows=size(X,1);
cols=size(X,2);
hx=sigmoid(X*theta); %rows*1的h_theta(x^i)的值
for i=1:rows
J=J-1/m*(y(i)*log(hx(i))+(1-y(i))*log(1-hx(i)));
for j=1:cols
grad(j)=grad(j)+1/m*(hx(i)-y(i))*X(i,j);
end
end

就可以调用该函数计算出\(\theta\)和J:

options = optimset('GradObj', 'on', 'MaxIter', 400);

%  Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);

这篇博客中介绍了详细用法,先mark一下。

多分类任务

基本解决思路是将多分类任务拆解为若干个二分类任务求解。

最经典的拆分策略有三种:"一对一"(OvO),“一对其余”(OvR)和多对多(MvM)。

在这里介绍下OvR:对于N个类别,分别训练N个分类器,每个分类器仅将一个类作为正例,其余作为反例。最后将置信度最大的分类器的结果作为预测的结果。如下:

\[\begin{align*}& y \in \lbrace0, 1 ... n\rbrace \newline& h_\theta^{(0)}(x) = P(y = 0 | x ; \theta) \newline& h_\theta^{(1)}(x) = P(y = 1 | x ; \theta) \newline& \cdots \newline& h_\theta^{(n)}(x) = P(y = n | x ; \theta) \newline& \mathrm{prediction} = \max_i( h_\theta ^{(i)}(x) )\newline\end{align*}
\]

[Machine Learning]学习笔记-Logistic Regression的更多相关文章

  1. Machine Learning 学习笔记

    点击标题可转到相关博客. 博客专栏:机器学习 PDF 文档下载地址:Machine Learning 学习笔记 机器学习 scikit-learn 图谱 人脸表情识别常用的几个数据库 机器学习 F1- ...

  2. Machine Learning 学习笔记1 - 基本概念以及各分类

    What is machine learning? 并没有广泛认可的定义来准确定义机器学习.以下定义均为译文,若以后有时间,将补充原英文...... 定义1.来自Arthur Samuel(上世纪50 ...

  3. Andrew Ng Machine Learning 专题【Logistic Regression & Regularization】

    此文是斯坦福大学,机器学习界 superstar - Andrew Ng 所开设的 Coursera 课程:Machine Learning 的课程笔记. 力求简洁,仅代表本人观点,不足之处希望大家探 ...

  4. [Python & Machine Learning] 学习笔记之scikit-learn机器学习库

    1. scikit-learn介绍 scikit-learn是Python的一个开源机器学习模块,它建立在NumPy,SciPy和matplotlib模块之上.值得一提的是,scikit-learn最 ...

  5. Coursera 机器学习 第6章(上) Advice for Applying Machine Learning 学习笔记

    这章的内容对于设计分析假设性能有很大的帮助,如果运用的好,将会节省实验者大量时间. Machine Learning System Design6.1 Evaluating a Learning Al ...

  6. Machine Learning 学习笔记 (1) —— 线性回归与逻辑回归

    本系列文章允许转载,转载请保留全文! [请先阅读][说明&总目录]http://www.cnblogs.com/tbcaaa8/p/4415055.html 1. 梯度下降法 (Gradien ...

  7. [Machine Learning]学习笔记-Neural Networks

    引子 对于一个特征数比较大的非线性分类问题,如果采用先前的回归算法,需要很多相关量和高阶量作为输入,算法的时间复杂度就会很大,还有可能会产生过拟合问题,如下图: 这时就可以选择采用神经网络算法. 神经 ...

  8. CheeseZH: Stanford University: Machine Learning Ex3: Multiclass Logistic Regression and Neural Network Prediction

    Handwritten digits recognition (0-9) Multi-class Logistic Regression 1. Vectorizing Logistic Regress ...

  9. 机器学习---朴素贝叶斯与逻辑回归的区别(Machine Learning Naive Bayes Logistic Regression Difference)

    朴素贝叶斯与逻辑回归的区别: 朴素贝叶斯 逻辑回归 生成模型(Generative model) 判别模型(Discriminative model) 对特征x和目标y的联合分布P(x,y)建模,使用 ...

随机推荐

  1. BootStrap教程完整版

    http://www.runoob.com/bootstrap/bootstrap-navbar.html

  2. Java基础总结--Java编程环境变量配置

    1.jdk--bin--都是命令行程序(图形化是对命令行的封装)eg javac&java执行javac必须切换到其所在目录--太麻烦---想在任意目录下使用要执行一个命令--先在当前目录下找 ...

  3. 对象转字典 iOS

    最近在开发SDK,我开放给客户model类设置信息后,对象转字典,POST给后台. 思路:通过Runtime访问属性列表,快速转换成字典. FRObjectToDictionary.h类 p.p1 { ...

  4. url编码&&PHP大法

    URL编码 Url编码通常也被称为百分号编码(Url Encoding,also known as percent-encoding),是因为它的编码方式非常简单,使用%百分号加上两位的字符--012 ...

  5. Install a Redmine on Ubuntu system

    # How to install a Redmine on Ubuntu system Ref to: https://www.linode.com/docs/applications/project ...

  6. Again Stone Game

    Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains ...

  7. Best Coder #86 1002 NanoApe Loves Sequence

    NanoApe Loves Sequence Accepts: 531 Submissions: 2481 Time Limit: 2000/1000 MS (Java/Others) Memory ...

  8. Linux中常见问题(磁盘 定时任务)

    第1章 linux无法上网 1)     第一步,先ping域名. ping www.baidu.com 2)再ping一个公网ip , ping 223.5.5.5/223.6.6.6/114.11 ...

  9. 关于C++中vector和set使用sort方法进行排序

    C++中vector和set都是非常方便的容器, sort方法是algorithm头文件里的一个标准函数,能进行高效的排序,默认是按元素从小到大排序 将sort方法用到vector和set中能实现多种 ...

  10. Java多线程其他

    1.interrupt()方法 interrupt方法不会真正中断线程,它只会清楚线程的wait,sleep,join的受阻状态,时线程重新获得CPU的执行权. 此时如果再次调用线程的wait,sle ...