模型

假定有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]学习笔记-线性回归的更多相关文章

  1. [Machine Learning]学习笔记-Logistic Regression

    [Machine Learning]学习笔记-Logistic Regression 模型-二分类任务 Logistic regression,亦称logtic regression,翻译为" ...

  2. Machine Learning 学习笔记

    点击标题可转到相关博客. 博客专栏:机器学习 PDF 文档下载地址:Machine Learning 学习笔记 机器学习 scikit-learn 图谱 人脸表情识别常用的几个数据库 机器学习 F1- ...

  3. Coursera 机器学习 第6章(上) Advice for Applying Machine Learning 学习笔记

    这章的内容对于设计分析假设性能有很大的帮助,如果运用的好,将会节省实验者大量时间. Machine Learning System Design6.1 Evaluating a Learning Al ...

  4. [Python & Machine Learning] 学习笔记之scikit-learn机器学习库

    1. scikit-learn介绍 scikit-learn是Python的一个开源机器学习模块,它建立在NumPy,SciPy和matplotlib模块之上.值得一提的是,scikit-learn最 ...

  5. Machine Learning 学习笔记1 - 基本概念以及各分类

    What is machine learning? 并没有广泛认可的定义来准确定义机器学习.以下定义均为译文,若以后有时间,将补充原英文...... 定义1.来自Arthur Samuel(上世纪50 ...

  6. Machine Learning 学习笔记2 - linear regression with one variable(单变量线性回归)

    一.Model representation(模型表示) 1.1 训练集 由训练样例(training example)组成的集合就是训练集(training set), 如下图所示, 其中(x,y) ...

  7. 吴恩达Machine Learning学习笔记(二)--多变量线性回归

    回归任务 多变量线性回归 公式 h为假设,theta为模型参数(代表了特征的权重),x为特征的值 参数更新 梯度下降算法 影响梯度下降算法的因素 (1)加速梯度下降:通过让每一个输入值大致在相同的范围 ...

  8. Machine Learning 学习笔记 (1) —— 线性回归与逻辑回归

    本系列文章允许转载,转载请保留全文! [请先阅读][说明&总目录]http://www.cnblogs.com/tbcaaa8/p/4415055.html 1. 梯度下降法 (Gradien ...

  9. machine learning学习笔记

    看到Max Welling教授主页上有不少学习notes,收藏一下吧,其最近出版了一本书呢还,还没看过. http://www.ics.uci.edu/~welling/classnotes/clas ...

随机推荐

  1. IDEA-最简单的struts2项目 关于lib项目的默认位置

    文件结构 struts.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts ...

  2. 2017年11月Dyn365/CRM用户社区活动报名

    UG是全球最大Dynamics的用户组织,由最终用户自发组织,由行业有经验的专家自愿贡献知识和经验的非营利机构,与会人员本着务实中立的态度,不进行推介产品,服务以及其他营销行为.在美国,微软Dynam ...

  3. LeetCode 243. Shortest Word Distance (最短单词距离)$

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  4. jerasure 2.0译文

    原文地址: 本文译者水平有限,如发现问题请批评指正 Jerasure 2.0:为方便存储相关应用开发的一个基于C开发的纠删码库 版本2.0 James S.Plank Kevin M.Greenan ...

  5. mysql密码更改

    1.用户修改密码: 方法一:mysqladmin -u用户 -p密码 password '新密码' mysqladmin -uroot -pdefault password 'zhouli.cn' 方 ...

  6. jsoup从表单中取数据

    表单的格式如下 <td>user</td> <td>cc</td> </tr> <tr> <td>pass</ ...

  7. 简单Spring+Struts2+Hibernate框架搭建

    使用Maven+Spring+Struts2+Hibernate整合 pom文件 <project xmlns="http://maven.apache.org/POM/4.0.0&q ...

  8. AngularJS学习篇(二十三)

    AngularJS 路由 AngularJS 路由允许我们通过不同的 URL 访问不同的内容. 通过 AngularJS 可以实现多视图的单页Web应用(single page web applica ...

  9. Luogu P1541 乌龟棋(NOIP2010TG)

    自己的第一篇博文 祭一下祭一下 题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点, ...

  10. Oracle-3 - :超级适合初学者的入门级笔记--用户权限,set运算符,高级子查询

    上一篇的内容在这里第二篇内容, 用户权限:创建用户,创建角色,使用grant  和 revoke 语句赋予和回收权限,创建数据库联接 创建用户:create user xxx identified b ...