[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. wordpress 显示数学公式 (MathJax-LaTeX)

    blog 不放一堆数学公式怎么能显得高大上,所以 MathJax-LaTeX 也是必装的插件之一了. 一.安装 MathJax-LaTex 插件 直接在 wordpress 插件中,搜索并安装 Mat ...

  2. Numpy的小总结

    1.Numpy是什么? numpy是Python的一个科学计算库,提供矩阵运算的功能. 1.1Numpy的导入 import numpy as np #一般都是用numpy的别名来进行操作 1.2Nu ...

  3. Python Nose框架编写测试用例方法

    1. 关于Nose nose项目是于2005年发布的,也就是 py.test改名后的一年.它是由 Jason Pellerin 编写的,支持与 py.test 相同的测试习惯做法,但是这个包更容易安装 ...

  4. Java基础总结--方法(函数)

    ---函数的作用---实现特定功能的代码--是一种代码重用的方式---函数的格式---访问修饰符 返回值类型 函数名(参数列表){语句:} 参数列表包含参数的类型和参数名(参数列表要注意顺序)---关 ...

  5. LeetCode 11. Container With Most Water (装最多水的容器)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  6. javascript的一些算法的实用小技巧

    一.交换两个数字的值 我们交换两个数字的值想到的方法一般就是用一个新的变变量,让他把一个数存起来,然后在交换两个数字的值,看下面这种. var a = 1, b = 2; //交换两个数字的值 var ...

  7. 快速部署MongoDB

    MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案.本文安装的版本为3.0,其他版本可对照. 设置mongodb repo vi /e ...

  8. 【Telerik控件学习】-制作3D效果的柱状图(ChartView)

    首先,定义柱状图,并设置自定义的DataTemplate <telerik:RadCartesianChart > <telerik:RadCartesianChart.Horizo ...

  9. 读书笔记-你不知道的JS上-this

    关于this 与静态词法作用域不用,this的指向动态绑定,在函数执行期间才能确定.感觉有点像C++的多态? var a = 1; var obj = { a: 2, fn: function() { ...

  10. 平衡二叉树(AVL树)

    参考资料 http://www.cnblogs.com/Cmpl/archive/2011/06/05/2073217.html http://www.cnblogs.com/yc_sunniwell ...