Machine Learning--week3 逻辑回归函数(分类)、决策边界、逻辑回归代价函数、多分类与(逻辑回归和线性回归的)正则化
Classification
It's not a good idea to use linear regression for classification problem.
We can use logistic regression algorism, which is a classification algorism
想要\(0\le h_{\theta}(x) \le 1\), 只需要使用sigmoid function (又称为logistic function)
\]
\(h_\theta(x)\)的意义在于: \(h_\theta(x)\) = estimated probability that \(y = 1\) on input \(x\)
注意:\(x=0\)时,\(g(z)\)刚好等于0.5
Decision Boundary
\(h_\theta{(x)} == P\{y=1|x;0 \}\) (\(P\)指预测的概率)
在课上的例子中,\(h_\theta(x) \ge 0.5,则y=1, else\; y=0\)
不妨设\(\theta = \begin{bmatrix}-3\\ 1\\ 1 \end{bmatrix} ,则 h_\theta(x)=g(-3+x_1+x_2)\)
由于"\(y=1\)" == "\(h_\theta(x) \ge 0.5\)" == "\(\theta^Tx \ge 0\)" == "\(-3+x_1+x_2 \ge 0\)"
这样的到了 "\(y=1\)" == "\(x_1+x_2 \ge 3\)"
\(x_1+x_2\) 与 \(3\) 的关系决定了 \(y\) 的值,这就是Decision boundary(决策边界)
拓展到 Non-linear decision boundary:
还可以有:Predict "\(y=1\)" if \(-1+x_1^2+x_2^2 \ge 0\) (\(\theta = \begin{bmatrix}-1\\ 0\\ 0 \\ 1\\ 1 \end{bmatrix},\;x = \begin{bmatrix}x_0\\ x_1\\ x_2\\ x_3 \\ x_4 \end{bmatrix} = \begin{bmatrix}1\\ x_1\\ x_2\\ x_1^2 \\ x_2^2 \end{bmatrix}\))
通过\(\theta\)的不同选择与\(x\)的不同构造可以得到各种形状的决策边界
而Decision Boundary 取决于参数 \(\theta\) 的选择,并非由训练集决定
我们需要用训练集来拟合参数 \(\theta\)
Cost Function
\]
在之前的 linear regression 中,用的Cost函数是:$Cost(h_\theta(x,y)) = \frac{1}{2}(h_\theta(x,y))^2 $
但那不是通用的,在hypothesis function \(h_\theta(x)\)不再是线性方程的情况下,若再采用$Cost(h_\theta(x,y)) = \frac{1}{2}(h_\theta(x,y))^2 \(会导致\)J(\theta)$ 有着众多的local optima,而不是我们想要的convex function
Logistic Regression Cost Function
\begin{align}
{-log(h_\theta(x))} &\quad\text{ if $y$ = 1} \\
{-log(1-h_\theta(x))} &\quad \text{ if $y$ = 0}
\end{align}
\end{cases}
\]
当 \(h_\theta(x)=y\) 时,\(Cost(h_\theta(x,y))=0\),
当 \(y=1,h_\theta(x)\rightarrow0\) 时 \(Cost \rightarrow \infty\),此时:\(\theta^Tx \rightarrow -\infty\)
当 \(y=0,h_\theta(x)\rightarrow1\) 时 \(Cost \rightarrow \infty\),此时:\(\theta^Tx \rightarrow \infty\)
这样就保证了\(\theta\)的调整能使得\(h_\theta(x)\) 向 \(y\) 靠近,也就是预测效果与实际更加符合
上面的\(Cost\) function 也可以写成:
\]
这与之前的cases形式是等价的
所以:
&= -\frac{1}{m}[\sum_{i=1}^{m}y^{(i)}\cdot log(h_\theta(x^{(i)}))+(1-y^{(i)})\cdot log(1-h_\theta(x^{(i)}))]
\end{align}
\]
Gradient Descent Algorithm的通用形式还是跟linear regression的一样(当然把\(h_\theta(x)\)展开后就不一样了):
\]
Other Optimization Algorism
- Conjugate Algorism(共轭梯度法)
- BFGS(Broyden–Fletcher–Goldfarb–Shanno algorithm)
- L-BFGS( Limited-memory BFGS)
advantage:
- no need to manually pick \(\alpha\)
- Often faster than gradient descent
disadvantage:
- More complex
不建议自己写,但是...可以直接调库啊
%{
%a function's definition, return the costFunction in 'jVal' and the Partial derivative in 'gradient'
function [jVal, gradient] = costFunction(theta)
jVal = [code to compute J(theta)]
gradient = zeros(n+1,1)
gradient(1) = [code to compute ∂[J(theta)]/∂[theta(0)]]
gradient(2) = [code to compute ∂[J(theta)]/∂[theta(1)]]
...
gradient(n+1) [code to compute ∂[J(theta)]/∂[theta(n)]] %the matrix in Octave starts from 1
%}
options = optimset('GradObj', 'on', 'MaxIter', '100');
initialTheta = zeros(2,1);
[optTheta, functional, exitFlag] = fminunc(@costFunction, initialTheta, options);
Multiclass Classification:
用one-vs-all(一对多/一对余)的思想
对每一类都分成"这一类" 与 "剩下的所有类的集合" 两类,然后用之前的课程中讲得分类方法拟合出这一类的分类器(classifier)
(classifier 就是hypothesis)
最后得出\(n\)个classifiers, 其中\(n\)是类别的总数量, \(y\)是类别:
\]
也就是说,给定\(x\)和\(\theta\), \(h_\theta^{(i)}(x)\) 能算出来类别是\(i\)类的概率
然后输入一个新的input \(x\)时,作出预测的行为是:\(\underbrace{max}_i(h_\theta^{(i)}(x))\)
Regularization (正则化)
解决overfitting(过拟合)的问题,另一个描述这个问题的词语是high variance(高方差)
这是 过多变量(feature)+ 过少训练数据 造成的
If we have too many features, the learned hypothesis may fit the training set very well(\(J(\theta) \approx 0\))
generalize: how well a hypothesis applies even to new examples
Option to address overfitting:
- Reduce number of features:
- Manually select which features to keep
- Model selection algorism
- Regularization:
- Keep all features, but reduce magnitude(大小)/values of parameters \(\theta_j\)
- Works well when having a lot of features , each of which contributes a bit to predicting \(y\)
regularized Linear Regression
Regularization 的思路:
Small values for parameters \(\theta_0, \theta_1,\dots,\theta_n\):
- "Simpler" hypothesis
- Less prone to overfitting
也就是将某些影响过大的\(\theta_j\)设得很小,比如: \(\theta_0 + \theta_1x + \theta_2x^2 + \theta_3x^3 + \theta_4x^4 \approx \theta_0 + \theta_1x + \theta_2x^2\)
Gradient Descent
但是这个regularization 的过程不是在 \(h_\theta(x)\) 里进行的,而是在Cost Function \(J(\theta)\)里进行的:
\]
注意后面加上的那一项(称之为正则化项)是从1开始的,它收缩了除了\(\theta_0\)外的每一个参数。 \(\lambda\) 称为regularization parameter(正则化参数),用于控制两个不同目标之间的平衡关系。
在这个cost functions 里两个\(\sum\)项代表了两个不同的目标:
- 使假设更好地拟合数据(fit the training data well)
- 保持参数值较小(keep the parameters small)
较小的参数值能得到简单的hypothesis,从而避免overfitting
注意:\(\lambda\)不能过大,否则会使得 \(\theta_1,\dots ,\theta_n \approx 0\), 从而fail to fit even the training set ——too high bias——underfitting(欠拟合)
&\text{repeat until convergence}\{\qquad\qquad\qquad\qquad\qquad\\
&\qquad \theta_{0}\; \text{:= } \theta_{0} - \alpha\frac{1}{m} \sum_{i=1}^{m} (h_{\theta}(x^{(i)})-y^{(i)})x_0^{(i)} \\
&\qquad \theta_{j}\; \text{:= } \theta_{j} - \alpha[\frac{1}{m} \sum_{i=1}^{m} (h_{\theta}(x^{(i)})-y^{(i)})x_j^{(i)} + \frac{\lambda}{m}\theta_j] \qquad (j = 1,2...,n)\\
&\}
\end{align}
\]
亦即:
&\text{repeat until convergence}\{\qquad\qquad\qquad\qquad\qquad\\
&\qquad \theta_{0}\; \text{:= } \theta_{0} - \alpha\frac{1}{m} \sum_{i=1}^{m} (h_{\theta}(x^{(i)})-y^{(i)})x_0^{(i)} \\
&\qquad \theta_{j}\; \text{:= } \theta_{j}(1-\alpha\frac{\lambda}{m}) - \alpha\frac{1}{m} \sum_{i=1}^{m} (h_{\theta}(x^{(i)})-y^{(i)})x_j^{(i)}\qquad (j = 1,2...,n)\\
&\}
\end{align}
\]
Normal Equation
review: 之前的Normal Equation是 \(\theta = (X^TX)^{-1}X^Ty\)
改成\(\theta = (X^TX+\lambda \small{\begin{bmatrix}0 \\&1 \\ &&1\\&&&\ddots\\&&&&1 \end{bmatrix}})^{-1}X^Ty,\quad \large\text{if }\lambda \gt 0\)
关于不可逆/退化矩阵 的问题,还是用Octave中的pinv()可以取伪逆矩阵
但是只要确保\(\lambda\)严格大于0,就能证明括号里的两个矩阵的和是可逆的.....
Regularized Logistic Regression
review: $ J(\theta) = -\frac{1}{m}[\sum_{i=1}{m}y{(i)}, log,h_\theta(x{(i)})+(1-y{(i)}), log,(1-h_\theta(x^{(i)}))]$
处理方法与Linear Regression 的一样,都是在式子最后面加上一个正则化项 \(\frac{\lambda}{2m}\sum_{j=1}^m\theta_j^2\)
\]
Gradient Descent(general 形式跟Linear Regression的一样,区别还是只有\(h_\theta(x^{(i)})\)不同):
&\text{repeat until convergence}\{\qquad\qquad\qquad\qquad\qquad\\
&\qquad \theta_{0}\; \text{:= } \theta_{0} - \alpha\frac{1}{m} \sum_{i=1}^{m} (h_{\theta}(x^{(i)})-y^{(i)})x_0^{(i)} \\
&\qquad \theta_{j}\; \text{:= } \theta_{j} - \alpha[\frac{1}{m} \sum_{i=1}^{m} (h_{\theta}(x^{(i)})-y^{(i)})x_j^{(i)} + \frac{\lambda}{m}\theta_j] \qquad (j = 1,2...,n)\\
&\}
\end{align}
\]
在Octave中还是用之前的代码模版就行,注意在算\(\frac{\partial J(\theta)}{\partial \theta_j}\;(\small j=1,2,\dots,n)\)时需要注意把正则化项的偏微分加上
%{
%a function's definition, return the costFunction in 'jVal' and the Partial derivative in 'gradient'
function [jVal, gradient] = costFunction(theta)
jVal = [code to compute J(theta)]
gradient = zeros(n+1,1)
gradient(1) = [code to compute ∂[J(theta)]/∂[theta(0)]]
gradient(2) = [code to compute ∂[J(theta)]/∂[theta(1)]]
...
gradient(n+1) [code to compute ∂[J(theta)]/∂[theta(n)]] %the matrix in Octave starts from 1
%}
options = optimset('GradObj', 'on', 'MaxIter', '100');
initialTheta = zeros(2,1);
[optTheta, functional, exitFlag] = fminunc(@costFunction, initialTheta, options);
Machine Learning--week3 逻辑回归函数(分类)、决策边界、逻辑回归代价函数、多分类与(逻辑回归和线性回归的)正则化的更多相关文章
- 【机器学习Machine Learning】资料大全
昨天总结了深度学习的资料,今天把机器学习的资料也总结一下(友情提示:有些网站需要"科学上网"^_^) 推荐几本好书: 1.Pattern Recognition and Machi ...
- Machine Learning Algorithms Study Notes(2)--Supervised Learning
Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 本系列文章是Andrew Ng 在斯坦福的机器学习课程 CS 22 ...
- Machine Learning With Go 第4章:回归
4 回归 之前有转载过一篇文章:容量推荐引擎:基于吞吐量和利用率的预测缩放,里面用到了基本的线性回归来预测容器的资源利用情况.后面打算学一下相关的知识,译自:Machine Learning With ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料【转】
转自:机器学习(Machine Learning)&深度学习(Deep Learning)资料 <Brief History of Machine Learning> 介绍:这是一 ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料汇总 (上)
转载:http://dataunion.org/8463.html?utm_source=tuicool&utm_medium=referral <Brief History of Ma ...
- 机器学习(Machine Learning)与深度学习(Deep Learning)资料汇总
<Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost到随机森林.D ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)
##机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)---#####注:机器学习资料[篇目一](https://github.co ...
- 学习笔记之机器学习实战 (Machine Learning in Action)
机器学习实战 (豆瓣) https://book.douban.com/subject/24703171/ 机器学习是人工智能研究领域中一个极其重要的研究方向,在现今的大数据时代背景下,捕获数据并从中 ...
- 逻辑回归,多分类推广算法softmax回归中
转自http://ufldl.stanford.edu/wiki/index.php/Softmax%E5%9B%9E%E5%BD%92 简介 在本节中,我们介绍Softmax回归模型,该模型是log ...
随机推荐
- python 接口自动化测试(四)cookie&session
掌握了前面几节的的内容,就可以做一些简单的http协议接口的请求发送了,但是这些还不够.HTTP协议是一个无状态的应用层协议,也就是说前后两次请求是没有任何关系的,那如果我们测试的接口之前有相互依赖关 ...
- PHP(层叠样式表,写法分类),选择器的种类)
表单元素的取值怎么取 对应的属性值都有哪些? <span> 标签被用来组合文档中的行内元素. 注释:span 没有固定的格式表现.当对它应用样式时,它才会产生视觉上的变化. style ...
- Dubbo 分布式服务框架入门
要想了解 Dubbo 是什么,我们不防先了解它有什么用.使用场景:比如我想开发一个网上商城项目,这个网上商城呢,比较复杂,分为 pc 端 web 管理后台,微信端销售公众号,那么我们分成四个项目,pc ...
- tomcat8.5.11安装教程
备份,省得以后自己又忘了. 1.下载tomcat 2.环境变量设置 打开环境变量设置 注意,要点击下面红框处的新建按钮,而不要点击上面的.新建系统变量: 注意,变量名一定得设置为CATALINA_HO ...
- 谁考了第k名
题目描述: 在一次考试中,每个学生的成绩都不相同,现知道了每个学生的学号和成绩,求考第k名学生的学号和成绩. 输入: 第一行有两个整数,分别是学生的人数n(1≤n≤100),和求第k名学生的k(1≤k ...
- day20:序列化模块,模块的导入
1,什么是序列化--将原本的字典,列表等内容转换成一个字符串的过程就叫做序列化,字符串是有顺序的,序列化转向一个字符串的过程,我们平时说的序列,指的就是字符串. 2,为何要序列化?本来字符串是可以强转 ...
- [java] 在linux+chrome/firefox上使用java applet
我现在的浏览器用不了java applet,我的环境是archlinux + chrome or firefox. java网站的指引是这样说的 按照这些说明操作,以便在 Ubuntu Linux 上 ...
- 删除64位ODBC数据源DNS
1.按照打开管理工具-打开数据源(ODBC),进入如下界面,选择用户DSN删除,发现报错一直删除不了. 2.成功删除:进入如下图路径,打开ODBC数据源管理工具,选择要删除的DSN就可以成功删除啦.
- array_merge() 用法
array_merge() 函数把两个或多个数组合并为一个数组. 如果键名有重复,该键的键值为最后一个键名对应的值(后面的覆盖前面的).如果数组是数字索引的,则键名会以连续方式重新索引. 注释:如果仅 ...
- Linux 两台服务器之间传输文件
一.scp命令的使用 1.传输文件(不包括目录) 命令格式:scp 源文件路径目录/需要传输的文件 目标主机的用户名@目标主机IP/主机别名:目标主机存储目录 举个例子:scp /root/ceshi ...