[Machine Learning]学习笔记-Logistic Regression

时间:2022-09-21 21:51:36

[Machine Learning]学习笔记-Logistic Regression

模型-二分类任务

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

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

[Machine Learning]学习笔记-Logistic Regression

可以采用对数几率函数(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. 在CentOS 6.6下安装与配置mysql

    1.使用yum安装mysql yum list | grep mysql   //查看mysql信息 yum install mysql-server.x86_64 //安装mysql sudo ap ...

  2. hdu 5545 The Battle of Guandu spfa最短路

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5545 题意:有N个村庄, M 个战场: $ 1 <=N,M <= 10^5 $; 其中曹 ...

  3. 第二百五十二天 how can I 坚持

    明天就要去旅游了...还不知道去哪呢,只知道要滑雪,要泡温泉,还要去西柏坡..哈哈. 其他没什么了吧.只是昨晚刷的鞋还没干,不知道明天会不会干,明天还得早走会,九点之前就得到. 还不知道坐车坐多长时间 ...

  4. NDK编译应用程序

    Android源码目录下的build/envsetup.sh文件,描述编译的命令 - m:       Makes from the top of the tree. - mm:      Build ...

  5. 一文快速了解MaxCompute

    很多刚初次接触MaxCompute的用户,面对繁多的产品文档内容以及社区文章,往往很难快速.全面了解MaxCompute产品全貌.同时,很多拥有大数据开发经验的开发者,也希望能够结合自身的背景知识,将 ...

  6. Java【第三篇】基本语法之--选择结构

    Java分支语句分类 分支语句根据一定的条件有选择地执行或跳过特定的语句,分为两类: if-else 语句 switch 语句 if-else语句语法格式 if(布尔表达式){ 语句或语句块; } i ...

  7. &lbrack;转载&rsqb;表单校验之datatype

    凡要验证格式的元素均需绑定datatype属性,datatype可选值内置有10类,用来指定不同的验证格式. 如果还不能满足您的验证需求,可以传入自定义datatype,自定义datatype是一个非 ...

  8. 2018焦作网络赛Mathematical Curse

    题意:开始有个数k,有个数组和几个运算符.遍历数组的过程中花费一个运算符和数组当前元素运算.运算符必须按顺序花费,并且最后要花费完.问得到最大结果. 用maxv[x][y]记录到第x个元素,用完了第y ...

  9. beautiful number 数位DP codeforces 55D

    题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...

  10. jQuery学习- 子选择器与可见性选择器

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...