[Machine Learning]学习笔记-线性回归
模型
假定有i组输入输出数据。输入变量可以用\(x^i\)表示,输出变量可以用\(y^i\)表示,一对\(\{x^i,y^i\}\)名为训练样本(training example),它们的集合则名为训练集(training set)。
假定\(X\)有j个特征,则可以用集合\({x^i_1,x^i_2,\dots ,x^i_j}\)表示。
为了描述模型,要建立假设方程(hypothesis function) :
$ h:X\to Y$。
\(h_\theta (x) = \theta_0 + \theta_1 x_1 + \theta_2 x_2 + \theta_3 x_3 + \cdots + \theta_n x_n\)
也可以写成矩阵形式:
\(\begin{align*}h_\theta(x) =\begin{bmatrix}\theta_0 \hspace{2em} \theta_1 \hspace{2em} ... \hspace{2em} \theta_n\end{bmatrix}\begin{bmatrix}x_0 \newline x_1 \newline \vdots \newline x_n\end{bmatrix}= \theta^T x\end{align*}\)
(备注:一般一维向量都写成列向量)
评价假设方程的准确性,可以用代价函数(cost function)。
代价函数
代价函数可以表示为遍历每个样本,求预测值和实际值的残差平方和的均值。
\(J(\theta) = \dfrac {1}{2m} \displaystyle \sum _{i=1}^m \left ( \hat{y}_{i}- y_{i} \right)^2 = \dfrac {1}{2m} \displaystyle \sum _{i=1}^m \left (h_\theta (x_{i}) - y_{i} \right)^2\)
显然,代价函数值越小,假设方程越准确。
由此可引入两种方法-梯度下降(Gradient Descent)和正规方程(Normal Equation)来调整参数\(\theta\)使\(J\)的值最小。
梯度下降
The gradient descent algorithm is:
repeat until convergence:
\(\theta_j := \theta_j - \alpha \frac{\partial}{\partial \theta_j} J(\theta)\)
求偏导(舍去m):
\[\begin{equation*}
\begin{split}
\frac{\partial}{\partial \theta_j} J(\theta) & = \frac{\partial}{\partial \theta_j}\frac{1}{2m}( h_\theta(\boldsymbol{x})-y)^2 \\
& =2\cdot\frac{1}{2m}\cdot( h_\theta(\boldsymbol{x})-y)\cdot\frac{\partial}{\partial \theta_j}( h_\theta(\boldsymbol{x})-y) \\
& = \frac{1}{m}(h_\theta(\boldsymbol{x})-y)\cdot \frac{\partial}{\partial \theta_j}(\sum_{i=0}^{n}\theta_i x_i-y) \\
& =\frac{1}{m} (h_\theta(\boldsymbol{x})-y)x_j \\
\end{split}
\end{equation*}\]
\(\alpha\)为学习速率(learning rate),对应上图的步长。
对于一条样本,可得:
\(\theta_j := \theta_j - \alpha \frac{1}{m} (h_\theta(x^i)-y^i)x_{j}^{i}\)
这就是有名的LMS更新原则,也叫Widrow-Hoff学习准则,参数 θ 更新的幅度取决于误差项的大小。从一对样本的情况,我们推导出参数θ
如何更新使得函数可以收敛。事实上,对于含有多个训练样本的情况,有两个方法可以对参数θ 进行更新,一个是 batch model, 另外一个是stochastic model。
(PS:这篇博客介绍的很详细,但最后两个公式的正负号错了。)
batch mode:
每次更新都遍历所有样本
\[
\begin{align*} & \text{repeat until convergence:} \; \lbrace \newline \; & \theta_0 := \theta_0 - \alpha \frac{1}{m} \sum\limits_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)}) \cdot x_0^{(i)}\newline \; & \theta_1 := \theta_1 - \alpha \frac{1}{m} \sum\limits_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)}) \cdot x_1^{(i)} \newline \; & \theta_2 := \theta_2 - \alpha \frac{1}{m} \sum\limits_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)}) \cdot x_2^{(i)} \newline & \cdots \newline \rbrace \end{align*}
\]
特征缩放(Feature Scaling)
在使用梯度下降算法前,最好对每个特征进行归一化操作。
归一化公式:
\[x_j := \dfrac{x_j - \mu_j}{s_j}\]
\(\mu_j-样本均值\)
\(s_j -样本方差\)
正规方程
公式
推导过程
\(\theta = (X^T X)^{-1}X^T y\)
与梯度下降的对比
Gradient Descent | Normal Equation |
Need to choose alpha | No need to choose alpha |
Needs many iterations | No need to iterate |
O (kn2) | O (n3), need to calculate inverse of XTX |
Works well when n is large | Slow if n is very large |
[Machine Learning]学习笔记-线性回归的更多相关文章
- [Machine Learning]学习笔记-Logistic Regression
[Machine Learning]学习笔记-Logistic Regression 模型-二分类任务 Logistic regression,亦称logtic regression,翻译为" ...
- Machine Learning 学习笔记
点击标题可转到相关博客. 博客专栏:机器学习 PDF 文档下载地址:Machine Learning 学习笔记 机器学习 scikit-learn 图谱 人脸表情识别常用的几个数据库 机器学习 F1- ...
- Coursera 机器学习 第6章(上) Advice for Applying Machine Learning 学习笔记
这章的内容对于设计分析假设性能有很大的帮助,如果运用的好,将会节省实验者大量时间. Machine Learning System Design6.1 Evaluating a Learning Al ...
- [Python & Machine Learning] 学习笔记之scikit-learn机器学习库
1. scikit-learn介绍 scikit-learn是Python的一个开源机器学习模块,它建立在NumPy,SciPy和matplotlib模块之上.值得一提的是,scikit-learn最 ...
- Machine Learning 学习笔记1 - 基本概念以及各分类
What is machine learning? 并没有广泛认可的定义来准确定义机器学习.以下定义均为译文,若以后有时间,将补充原英文...... 定义1.来自Arthur Samuel(上世纪50 ...
- Machine Learning 学习笔记2 - linear regression with one variable(单变量线性回归)
一.Model representation(模型表示) 1.1 训练集 由训练样例(training example)组成的集合就是训练集(training set), 如下图所示, 其中(x,y) ...
- 吴恩达Machine Learning学习笔记(二)--多变量线性回归
回归任务 多变量线性回归 公式 h为假设,theta为模型参数(代表了特征的权重),x为特征的值 参数更新 梯度下降算法 影响梯度下降算法的因素 (1)加速梯度下降:通过让每一个输入值大致在相同的范围 ...
- Machine Learning 学习笔记 (1) —— 线性回归与逻辑回归
本系列文章允许转载,转载请保留全文! [请先阅读][说明&总目录]http://www.cnblogs.com/tbcaaa8/p/4415055.html 1. 梯度下降法 (Gradien ...
- machine learning学习笔记
看到Max Welling教授主页上有不少学习notes,收藏一下吧,其最近出版了一本书呢还,还没看过. http://www.ics.uci.edu/~welling/classnotes/clas ...
随机推荐
- LDAP服务部署
1.安装基本环境 # yum -y install openldap openldap-devel openldap-servers openldap-clients 2.配置LDAP服务端 (1)拷 ...
- 读Zepto源码之Form模块
Form 模块处理的是表单提交.表单提交包含两部分,一部分是格式化表单数据,另一部分是触发 submit 事件,提交表单. 读 Zepto 源码系列文章已经放到了github上,欢迎star: rea ...
- HTTP 错误 500.19 Internal Server Error的解决方法
第一种可能,能解决一部分问题 http://wenku.baidu.com/view/c5cb4a08bb68a98271fefa3f.html 第二种可能,解决另外一部分问题 经过检查发现是由于先安 ...
- SpringMVC + Spring + Mybatis+ Redis +shiro以及MyBatis学习
SpringMVC + Spring + Mybatis+ Redis +shiro http://www.sojson.com/shiro MyBatis简介与配置MyBatis+Spring+My ...
- LeetCode 120. Triangle (三角形)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 《从零玩转JavaWeb+项目实战》-系列课堂录制计划
点击试听课程 前言 很多自学编程的同学经常和我说想学一门语言自己到网上找一些教程看到一半就像背单词背到ambulance一样坚持不下去了....究其原因基本上都是:内容太多,太枯燥,专业术语听不懂,学 ...
- C++ 多态与虚函数
1.多态的概念 由虚函数实现的动态多态性就是:同一类族中不同类的对象,对同一函数调用作出不同的响应. 先看下面这个简单的例子: #include<iostream> using std:: ...
- TCP协议的滑动窗口协议以及流量控制
参考资料 http://blog.chinaunix.net/uid-26275986-id-4109679.html http://network.51cto.com/art/201501/4640 ...
- 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest J. Bottles
J. Bottles time limit per test 2 seconds memory limit per test 512 megabytes input standard input ou ...
- AngularJS学习篇(十)
AngularJS Select(选择框) 使用 ng-options 创建选择框 在 AngularJS 中我们可以使用 ng-option 指令来创建一个下拉列表,列表项通过对象和数组循环输出,如 ...