Andrew Ng机器学习笔记

Week_3 -- -Logistic Regression

This week, we’ll be covering logistic regression. Logistic regression is a method for classifying data into discrete outcomes. In this module, we introduce the notion of classification, the cost function for logistic regression, and the application of logistic regression to multi-class classification.

We’ll introduce regularization, which helps prevent models from over-fitting the training data.


1.Classification

例子:肿瘤与否,垃圾邮件与否

一般可以通过Threshold,阈值,来辨别

例如:0和1的话,可以选择0.5作为阈值

和回归问题差不多,不过这个数据是离散数据

For Now,we will focus on the Binary Classification Problem

in which you can take on only two values, 0 and 1. (Most of what we say here will also generalize to the multiple-class case.)

例如:识别垃圾邮件的时候,我们用\(x^{(i)}\) 来表示垃圾邮件的特征量feature, Y则只有2个值,1和0,表示是垃圾邮件和非垃圾邮件。

2.Hypothesis Representation假设函

讨论逻辑函数的假设函数----逻辑回归

首先我们需要的假设函数应该预测值在0到1之间

模仿线性回归的形式: 

\[h_\theta(x) = g(\theta^T X)
\]

其中定义g(Z) = \(\frac1{1+e^{-z}}\)

即是:\(h_\theta(x) = \frac1{1+e^{\theta^T x}}\)

如下图:

这可以理解为一个概率函数

可以写为   \(h_\theta(x) = {(y = 1 | x,\theta)}\)

概率参数为\(\theta\)和x,y只有1或者0 。值就是有肿瘤的概率(1)

3. Decision Boundary 决策界限

如上图所示:0.5即为决策界限

那么如何算出 决策界限呢?

上面是线性的,那么,非线性拟合呢?

那就要通过增加参数\theta

5. 优化目标,代价函数cost function

目标:如何拟合\(\theta\)??

和线性回归的代价函数相似:

定义:\(J(\theta) = \frac1m\sum_{i=1}^mCost(h_\theta(x),y)\)

其中Cost 函数可以等于 \(\frac12(h_\theta(x) - y)^2\)

但是,在classification 中,h(x)是非线性的,所以,h(x)图像可能为:

所以,根据h(x)的定义

可以定义cost函数

它的图像为:

J(\theta)将会是一个凸函数且没有局部最优

6.更简单的方式找到代价函数.用梯度下降来拟合逻辑回归的参数\(\theta\).Simplified Cost Function and Gradient Descent

代价函数可以不用分段函数:还可以用

\(Cost(h_\theta(x),y) = -yln(1 - h_\theta(x)) - (1-y)ln(1-h_\theta(x))\)

来表示,这样就不用分段了

所以,代价函数就是:

或者,用向量来表示:


类似,我们要找到J($\theta $)的最小值

当然,使用Gradient Descent梯度下降

如图操作,注意此时h函数不是和线性回归的是一样的

接下来,如何监测梯度下降?

当然,也可以用向量方法来实现:?????(如何推导??)

看这里更清楚:(截图功能真的是太赞了!)

7.高级优化Advanced Optimization

算法优化,Optimization algorithms

  • Gradient descent

  • Conjugate gradient

  • BFGS

  • L-BFGS 共轭梯度算法

后三种算法的优点:

  • No need to manually pick \(\alpha\)
  • Often faster than gradient descent

缺点:

  • More complex

代码细节不用知道!

可以以后学习tensorflow 来实现

8. Multiclass classification

例如,把邮件贴上不同的标签

那么,如何找到多元分类的决策界限?

例如,3种的话,通过两两分类

一对多方法。分为3个二元问题

上图拟合出3个分类器

\(h_\theta^{(i)}(x) = P(y = i| x,\theta), (i = 1,2,3)\)

9. 过度拟合的问题over-fitting

变量太多,无法通过更多的数据来进行约束

以至于无法泛化到新数据中

线性:

第一个是under fitting ,欠拟合,有 high bias

第二个just right

第三个则是over fitting

逻辑回归:

如何解决?

  • 减少变量数目 reduce number of features
  • Regularization正则化

10. Regularization and its Cost function 正则化及其代价函数

像上节课一样,当过度拟合的时候,我们可以让其他的参数的影响尽可能的小

penalize惩罚\(\theta_3和\theta_4\)这两个参数。使他们尽可能为0

如何操作?

在最初的cost function中添加 正则化项

\(\lambda\) 叫做正则化参数

$\lambda $太大的话,会under fitting

所以应该选择合适的正则化参数

一张图来解释:

11. 正则线性回归Regularized Linear Regression

算法:

  1. Gradient descent

可以等价的写为:

\(\theta_j := \theta_j(1- \alpha\frac1m) - \alpha\frac1m\sum_{i=1}^m(h_\theta(x^{(i)}_j)-y^{(i)}) x_j^{(i)}\)

其中\(1-\alpha\frac1m\)这一项,如果学习率小,例子数量大的化,一般是比1小于一点点的值

而后面这一大坨,则和以前一模一样!!!

只不过前面这一项把theta压缩了!

  1. Normal equation

    使用了正则化,如何得到矩阵式子?

    数学推导略!

或者写成:

12. Regularized Logistic Regression 逻辑回归的正则化

和线性回归差不多,要添加正则项

算法类似,都要将0单独写出

下面来说明如何在更高级的算法中,应用正则化:

(学完octave后,应该就能看懂)

综上所述:




题目摘录:

第 3 题

Which of the following statements about regularization are true? Check all that apply.

Using too large a value of λ can cause your hypothesis to overfit the data; this can be avoided by reducing λ.

Using a very large value of λ cannot hurt the performance of your hypothesis; the only reason we do not set λ to be too large is to avoid numerical problems.

Consider a classification problem. Adding regularization may cause your classifier to incorrectly classify some training examples (which it had correctly classified when not using regularization, i.e. when λ=0).

Because logistic regression outputs values 0≤hθ(x)≤1, its range of output values can only be “shrunk” slightly by regularization anyway, so regularization is generally not helpful for it.

  • 答案: 3 *

正则化方法的公式: J(θ)=12m[∑i=1m(hθ(x(i))−y(i))2+λ∑i=1nθ2j]J(θ)=12m[∑i=1m(hθ(x(i))−y(i))2+λ∑i=1nθj2]

  • 选项1: λλ太大导致overfit不对,是underfit,当λλ太大时θ1θ2...θn≈0θ1θ2...θn≈0.只有θ0θ0起作用,拟合出来是一条直线. λλ太小才会导致overfit. 不正确 **
  • 选项2: 同1. 不正确 **
  • 选项3: 当λλ没有选择好时,可能会导致训练效果还不如不加的λλ好. 正确 **
  • 选项4: “shrunk” slightly的是θθ, regularization是想要解决overfit. 不正确 !

第 1 题

You are training a classification model with logistic

regression. Which of the following statements are true? Check

all that apply.

Introducing regularization to the model always results in equal or better performance on the training set.

Adding many new features to the model helps prevent overfitting ont the training set.

Introducing regularization to the model always results in equal or better performance on examples not in the training set.

Adding a new feature to the model always results in equal or better performance on the training set.

  • 答案: 4 *

正则化方法的公式: J(θ)=12m[∑i=1m(hθ(x(i))−y(i))2+λ∑i=1nθ2j]J(θ)=12m[∑i=1m(hθ(x(i))−y(i))2+λ∑i=1nθj2]

  • 选项1: 将正则化方法加入模型并不是每次都能取得好的效果,如果λλ取得太大的化就会导致欠拟合. 这样不论对traing set 还是 examples都不好. 不正确 **
  • 选项2: more features能够更好的fit 训练集,同时也容易导致overfit,是more likely而不是prevent. 不正确 **
  • 选项3: 同1,将正则化方法加入模型并不是每次都能取得好的效果,如果λλ取得太大的化就会导致欠拟合. 这样不论对traing set 还是 examples都不好. 不正确 **
  • 选项4: 新加的feature会提高train set的拟合度,而不是example拟合度. 正确 *![]

week_3的更多相关文章

  1. 【Duke-Image】Week_3 Spatial processing

    Chapter_3 Intensity Transsformations and Spatial Filtering 灰度变换与空间滤波 Intensity transformation functi ...

  2. 使用cJSON库解析JSON

    cJSON库的下载 cJSON是一个基于C的JSON解析库,这个库非常简单,只有cJSON.c和cJSON.h两个文件,支持JSON的解析和封装,需要调用时,只需要#include "cJS ...

  3. Qt平台下使用QJson解析和构建JSON字符串

    前言 上一篇介绍了C语言写的JSON解析库cJSON的使用:使用cJSON库解析和构建JSON字符串 本篇文章介绍,Qt开发环境下QJson库的使用示例,JSON解析配合API接口,就可以实现一些有趣 ...

  4. 使用cJSON库解析和构建JSON字符串

    使用cJSON库解析和构建JSON字符串 前言 其实之前的两篇博文已经介绍了json格式和如何使用cJSON库来解析JSON: 使用cJSON库解析JSON JSON简介 当时在MCU平台上使用时,会 ...

  5. HttpClient设置忽略SSL,实现HTTPS访问, 解决Certificates does not conform to algorithm constraints

    话不多说,直接上代码. 测试API:   https://api.k780.com/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f7 ...

随机推荐

  1. vue3 vite2 封装 SVG 图标组件 - 基于 vite 创建 vue3 全家桶项目续篇

    在<基于 vite 创建 vue3 全家桶>一文整合了 Element Plus,并将 Element Plus 中提供的图标进行全局注册,这样可以很方便的延续 Element UI 的风 ...

  2. TF-GNN踩坑记录(三)

    引言 Batch size问题 在Tensorflow-GNN中使用batch size除了需要注意上面的链接问题之外,最近我在调试的发现,使用了merge_batch_to_components() ...

  3. 成功解决IDEA中建立新项目Maven会默认选择配置(图解详细说明)

    文章目录 1.File-->Other Settings --> Settings for New Projects 2.选择自己的maven配置 3.效果展示(我这里省略建立新项目的过程 ...

  4. vue中将验证表单输入框的方法写在一个js文件中(表达式验证邮箱、身份证、号码、两次输入的密码是否一致)

    文章目录 1.实现的效果 2.编写的js文件(这里写在了api文件下) 3.在vue页面中引入(script) 4.页面代码 1.实现的效果 20220606_154646 2.编写的js文件(这里写 ...

  5. [Oracle]复习笔记-SQL部分内容

    Oracle笔记--SQL部分 整体框架 语句的执行顺序:from →where →group by→having→select→order by select * from * where * gr ...

  6. golang中的一些实用功能

    0.1.索引 https://waterflow.link/articles/1663921524839 通过使用一些通用代码来节省时间,而无需单独实现它们.以下是一些开发中经常会用到的函数实现的列表 ...

  7. 第三方库openPyxl读取excel文件

    import openpyxl from openpyxl.worksheet.worksheet import Worksheet def openpyxl_read(): #1.打开文件 work ...

  8. 8.uvloop

    uvloop是asyncio的事件循环的替代方案,性能高于默认asyncio的事件循环的效率,相当于提升两倍,效率可以比肩Go pip3 install uvloop   import asyncio ...

  9. Gitea 1.18 功能前瞻(其三):增强文本预览效果、继续扩展软件包注册中心、增强工单实用功能、完善了用户邀请机制和SEO

    今天是 10 月 26 日星期三,Gitea 周期性地发布了 1.18 的第一个 RC0 版本,在此阶段会收集一些功能和使用上的问题,随后还会发布 RC1,新功能的完整性和健壮性会逐步趋近正式版. 继 ...

  10. 三、redis环境安装

    三.redis环境安装 3.1.下载和安装 下载地址:https://github.com/tporadowski/redis/releases   使用以下命令启动redis服务端 redis-se ...