[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. shell编程/字库裁剪(2)——编程过程

    版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址 http://www.cnblogs.com/Colin-Cai/p/7704085.html 作者:窗户 Q ...

  2. angular-utils-ui-breadcrumbs使用心得

    angular-utils-ui-breadcrumbs是一个用来自动生成面包屑导航栏的一个插件,需要依赖angular.UIRouter和bootstrap3.css.生成的界面截图如下,点击相应的 ...

  3. Android Weekly Notes Issue #281

    October 29th, 2017 Android Weekly Issue #281 本期内容不多,包含了小众DI库牙签帮助测试的文章,Kotlin中Delegate的强大之介绍,以及基于Goog ...

  4. HDU 6092 Rikka with Subset

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  5. KMP (next数组的性质及证明)

    性质:如果len%(len-next[len-1])==0,则字符串中必存在最小循环节,且循环次数即为len/(len-next[len-1]); 证明:在前len个字符组成的字符串,存在最小循环节k ...

  6. Android 开发笔记___图像视图

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  7. grunt concat针对有依赖文件的js脚本的合并

    grunt concat针对有依赖文件的js脚本的合并: 在一个入口文件index.js里,有很多依赖文件,主要分两类,一类是和主文件同目录,另一类是其他目录下的js(cmd.非cmd的js文件,一般 ...

  8. sql select case when 语句

    有道笔试题: 服务器监控表server_status中,当服务器状态发生server_status变化时数据表中将被插入一条记录,状态0表示停机  1表示正常,用SQL查询Server A 的停机开始 ...

  9. 10.javaweb核心标签库详解

    一.JSTL简介及在项目中安装配置 1,  简介 使用JSTL标签的目的就是不希望jsp中出现java逻辑代码 分类 2,  JSTL的安装配置 首先将jar包中的各个标签库配置文件拷贝到项目WEB- ...

  10. GCC 中的编译器堆栈保护技术

    GCC 中的编译器堆栈保护技术 前几天看到的觉得不错得博客于是转发了,但这里我补充一下一些点. GCC通过栈保护选项-fstack-protector-all编译时额外添加两个符号,__stack_c ...