【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 7 Regularization 正则化
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 正则化的更多相关文章
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 15—Anomaly Detection异常检测
Lecture 15 Anomaly Detection 异常检测 15.1 异常检测问题的动机 Problem Motivation 异常检测(Anomaly detection)问题是机器学习算法 ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 16—Recommender Systems 推荐系统
Lecture 16 Recommender Systems 推荐系统 16.1 问题形式化 Problem Formulation 在机器学习领域,对于一些问题存在一些算法, 能试图自动地替你学习到 ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 14—Dimensionality Reduction 降维
Lecture 14 Dimensionality Reduction 降维 14.1 降维的动机一:数据压缩 Data Compression 现在讨论第二种无监督学习问题:降维. 降维的一个作用是 ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 13—Clustering 聚类
Lecture 13 聚类 Clustering 13.1 无监督学习简介 Unsupervised Learning Introduction 现在开始学习第一个无监督学习算法:聚类.我们的数据没 ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 12—Support Vector Machines 支持向量机
Lecture 12 支持向量机 Support Vector Machines 12.1 优化目标 Optimization Objective 支持向量机(Support Vector Machi ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 11—Machine Learning System Design 机器学习系统设计
Lecture 11—Machine Learning System Design 11.1 垃圾邮件分类 本章中用一个实际例子: 垃圾邮件Spam的分类 来描述机器学习系统设计方法.首先来看两封邮件 ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 10—Advice for applying machine learning 机器学习应用建议
Lecture 10—Advice for applying machine learning 10.1 如何调试一个机器学习算法? 有多种方案: 1.获得更多训练数据:2.尝试更少特征:3.尝试更多 ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 1_Introduction and Basic Concepts 介绍和基本概念
目录 1.1 欢迎1.2 机器学习是什么 1.2.1 机器学习定义 1.2.2 机器学习算法 - Supervised learning 监督学习 - Unsupervised learning 无 ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 18—Photo OCR 应用实例:图片文字识别
Lecture 18—Photo OCR 应用实例:图片文字识别 18.1 问题描述和流程图 Problem Description and Pipeline 图像文字识别需要如下步骤: 1.文字侦测 ...
随机推荐
- Spring:org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class
很长时间没有使用Spring,Hibernate,Struts2等一些框架了,现在使用起来还是有点陌生,今天刚弄就在Tomcat在启动的时候是报的这个错误: org.springframework.b ...
- 设置eclipse显示代码错误提示的
http://jingyan.baidu.com/article/f3e34a128d79aff5ea65356c.html
- linux常用开发工具命令行
- Java进阶知识点7:不要只会写synchronized - JDK十大并发编程组件总结
一.背景 提到Java中的并发编程,首先想到的便是使用synchronized代码块,保证代码块在并发环境下有序执行,从而避免冲突.如果涉及多线程间通信,可以再在synchronized代码块中使用w ...
- 监听Documents文件夹内文件发生改变
// 当Documents内文件发生改变时,启动计时器,每秒计算一次大小,当大小不发生改变时说明传输完毕,就开始刷新. @property (nonatomic, strong) NSTimer *t ...
- LeetCode Partition to K Equal Sum Subsets
原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...
- SQL Server: Top 10 Secrets of a SQL Server Expert
转载自:http://technet.microsoft.com/en-us/magazine/gg299551.aspx Many companies have downsized their IT ...
- admins.py总结比较,转
转:http://blog.csdn.net/pipisorry/article/details/46764495
- window下TortoiseGit的安装和使用
一.安装git for windows 首先下载git for windows客户端https://git-for-windows.github.io/安装过程没什么特别的,下载完安装包之后,按照提示 ...
- 致Oracle DBA 的一封信 (网上流传)
1. 数据库的可用度,DBA 说了“不算” --物化视图,加快查询速度 某些时候数据库的可用性,并不由DBA所设定.因为即使DBA对数据库有绝对掌控权,但用户可能从自己的工作和应用角度,与DBA的 ...