Lecture7 Regularization 正则化

7.1 过拟合问题 The Problem of Overfitting
7.2 代价函数 Cost Function
7.3 正则化线性回归  Regularized Linear Regression
7.4 正则化的逻辑回归模型 Regularized Logistic Regression

7.1 过拟合问题 The Problem of Overfitting

  参考视频: 7 - 1 - The Problem of Overfitting (10 min).mkv

  • 欠拟合/高偏差  underfitting 预测不准确
  • 刚好            just right
  • 过拟合/高方差  overfitting   泛化能力差

 回归问题:

  分类问题:

  解决方法:

1) 减少 feature 的个数:

  • Manually select which features to keep.
  • Use a model selection algorithm .

2) 正则化

  • Keep all the features, but reduce the magnitude of parameters θj
  • Regularization works well when we have a lot of slightly useful features.

7.2 代价函数 Cost Function

  参考视频: 7 - 2 - Cost Function (10 min).mkv

  如果线性回归出现过拟合,曲线方程如下:

  如果想消除高次幂项的影响,可以修改代价函数 ,在某些参数上设置一些惩罚,一定程度上减小这些参数的影响:

  要使代价函数趋于0,则需降低θ3和θ4的值,因为二次项≥0,所以令它们为0时代价函数最小,降低了他们在hypothesis function的影响,从而减少了过拟合。这就是正则化的思想。

  实际使用中,因为不知道具体应该惩罚那些参数。所以给所有参数都加一个系数 λ:

   λ or lambda 叫做 regularization parameter,加号后面这一项叫做 regularization term。
  1)如果 λ = 0或者特别小,起不到作用,仍然过拟合。

  1)如果 λ 选的太大,所有参数都遭到惩罚。最后假设方程可能变成 h(x) = θ0,导致欠拟合 underfitting。

7.3 正则化线性回归  Regularized Linear Regression

  参考视频: 7 - 3 - Regularized Linear Regression (11 min).mkv

  正则化线性回归的代价函数为:

  因为正则化不涉及到 θ0,梯度下降算法如下:

  对上面的算法第二个式子调整可得

( j ∈ 1,2 ... n)

  正则化线性回归的梯度下降算法的变化在于,每次都在原有算法更新规则的基础上令 θ减少了一个额外的值。

   如果使用正规方程 Normal Equation方法,引入一个 (n+1)×(n+1)维的方阵L,正则化如下:


  注:当 m < n 时,XTX 不可逆non-invertible。但是当加上 λ⋅L,XTX+ λ⋅L 变为可逆矩阵 invertible。

7.4 正则化的逻辑回归模型 Regularized Logistic Regression

  参考视频: 7 - 4 - Regularized Logistic Regression (9 min).mkv

  逻辑回归的代价函数为:

  加上正则项之后:

  注:这个代价函数看上去同正则化线性回归的式子一样,但是两个 ℎ 不同,所以有很大差别。

θ0不参与任何正则化
  效果(蓝色线是正则化之前,粉色线是正则化之后):

  仍然可以用 fminuc 函数来求解代价函数最小化的参数 ,但我们实现的 costFunction 函数中进行了正则化:

  python代码

 1 import numpy as np
2 def costReg(theta, X, y, learningRate):
3 theta = np.matrix(theta)
4 X = np.matrix(X)
5 y = np.matrix(y)
6 first = np.multiply(-y, np.log(sigmoid(X*theta.T)))
7 second = np.multiply((1 - y), np.log(1 - sigmoid(X*theta.T)))
8 reg = (learningRate / (2 * len(X))* np.sum(np.power(theta[:,1:the
9 ta.shape[1]],2))
10 return np.sum(first - second) / (len(X)) + reg

相关术语

decision boundary 决策边界
loophole 漏洞
nonlinear 非线性
penalize the parameter  惩罚参数
regularization term 正则项
regularization parameter 正则化参数
wiggly/curvy 摆动的 弯曲的
optimization objective 优化目标
lamda  即 λ
shrinking  收缩
magnitude  量级,重要性

【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 7 Regularization 正则化的更多相关文章

  1. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 15—Anomaly Detection异常检测

    Lecture 15 Anomaly Detection 异常检测 15.1 异常检测问题的动机 Problem Motivation 异常检测(Anomaly detection)问题是机器学习算法 ...

  2. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 16—Recommender Systems 推荐系统

    Lecture 16 Recommender Systems 推荐系统 16.1 问题形式化 Problem Formulation 在机器学习领域,对于一些问题存在一些算法, 能试图自动地替你学习到 ...

  3. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 14—Dimensionality Reduction 降维

    Lecture 14 Dimensionality Reduction 降维 14.1 降维的动机一:数据压缩 Data Compression 现在讨论第二种无监督学习问题:降维. 降维的一个作用是 ...

  4. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 13—Clustering 聚类

    Lecture 13 聚类 Clustering 13.1 无监督学习简介  Unsupervised Learning Introduction 现在开始学习第一个无监督学习算法:聚类.我们的数据没 ...

  5. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 12—Support Vector Machines 支持向量机

    Lecture 12 支持向量机 Support Vector Machines 12.1 优化目标 Optimization Objective 支持向量机(Support Vector Machi ...

  6. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 11—Machine Learning System Design 机器学习系统设计

    Lecture 11—Machine Learning System Design 11.1 垃圾邮件分类 本章中用一个实际例子: 垃圾邮件Spam的分类 来描述机器学习系统设计方法.首先来看两封邮件 ...

  7. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 10—Advice for applying machine learning 机器学习应用建议

    Lecture 10—Advice for applying machine learning 10.1 如何调试一个机器学习算法? 有多种方案: 1.获得更多训练数据:2.尝试更少特征:3.尝试更多 ...

  8. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 1_Introduction and Basic Concepts 介绍和基本概念

    目录 1.1 欢迎1.2 机器学习是什么 1.2.1 机器学习定义 1.2.2 机器学习算法 - Supervised learning 监督学习 - Unsupervised learning  无 ...

  9. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 18—Photo OCR 应用实例:图片文字识别

    Lecture 18—Photo OCR 应用实例:图片文字识别 18.1 问题描述和流程图 Problem Description and Pipeline 图像文字识别需要如下步骤: 1.文字侦测 ...

随机推荐

  1. CoreData / MagicalRecord

    CoreData 之前在学习使用SQLite时, 需要编写大量的sql语句,完成数据的增删改查,但对于不熟悉sql语句的开发人员来说,难度较大,调试程序比较困难.由此出现CoreData框架,将sql ...

  2. L125

    The United States Senate (参议院)has taken the first step toward ending President Barack Obama's health ...

  3. lua基础---函数

    Lua的函数功能很强大,保留了C语言的一些基本的特性,但是也有C语言没有的特性,比如,lua可以在一个函数返回多个值,我们来看看下面这个案例: 解释运行: lua test5.lua --定义一个函数 ...

  4. Wordpress在主题或者插件中自定义存储附件的方法

    1.前端使用form表单 //单文件上传,我的业务需求中限制了必须上传图片 <input type="file" name="singlename" ac ...

  5. v-if和v-show的区别

    v-if 是“真实”的条件渲染,因为它会确保条件块(conditional block)在切换的过程中,完整地销毁(destroy)和重新创建(re-create)条件块内的事件监听器和子组件. v- ...

  6. 老师木发的makefile与autotools

    makefile http://scc.qibebt.cas.cn/docs/linux/base/%B8%FA%CE%D2%D2%BB%C6%F0%D0%B4Makefile-%B3%C2%F0%A ...

  7. 关于djangoadmin的一个博客

    http://www.cnblogs.com/linxiyue/category/569717.html

  8. C#合成解析XML与JSON

      http://www.xuanyusong.com/archives/1901  XML与JSON在开发中非常重要, 其实核心就是处理字符串.一个是XML的字符串一个是JSON的字符串,尤其是在处 ...

  9. ecmall允许上传的图片大小

    $uploader->allowed_type($type); $uploader->allowed_size($size);  ecmall上传类型大小是这样定义,你可以去文件中搜索相关 ...

  10. Tomcat设置欢迎页问题

    今天下载了tomat9,配置到eclipse后拉起来,想跑个欢迎页看看是否起好了,随手写了个index.jsp放到项目Struts2的WebContent根目录下,直接打开网页输入http://loc ...