逻辑回归(一)

分类问题(Classification)

在分类问题中,我们尝试预测离散值输出,例如:判断一封电子邮件是否为垃圾邮件、判断一次在线交易是否为诈骗和判断肿瘤是否为恶性肿瘤等。

我们先从二元分类问题(即预测结果为0和1)开始讨论。我们将要预测的因变量y的取值分为两类:当y=1时,我们将其标记为正向类(Positive Class);当y=0时,我们将其标记为负向类(Negative Class)。

因此,因变量y可用数学符号记为:y∈{0, 1},其中0表示负向类,1表示正向类。

现在,我们再来回顾一下之前的肿瘤预测这个例子。假设我们有如下数据集,我们根据之前学习的线性回归的知识,可以用一条直线来帮助我们预测肿瘤是否为恶性肿瘤。

根据这条直线,我们可以将分类器的输出阀值设置为0.5,即:

按照上述操作,数据集右侧的数据都为正向类,左侧的数据都为负向类。

这时,我们延长数据集的横轴并添加一个训练数据。

根据之前的判断,数据集中存在某一个值(肿瘤大小)使得假设函数h的值恰好为阀值。当数据集中的值大于这个特定的值时,我们皆可判断这个肿瘤为良性;反之,我们皆可判断这个肿瘤为恶性。

那么我们就可以判断这个新添的训练数据的输出值为1,即为良性肿瘤吗?这当然是不能的,难道我们认为肿瘤越大其越为良性?因此,在这个特定的例子中,我们使用线性回归来进行预测显然是一个非常糟糕的做法。

除此之外,我们在分类问题中使用线性回归还会出现一个有趣的现象。众所周知,在(二元)分类问题中因变量y∈{0, 1}。若我们使用线性回归,则我们的假设函数hθ(x)可能会出现hθ(x)﹥1或者hθ(x) ﹤0的情况;若我们使用将要学习的逻辑回归,则我们的假设函数hθ(x)其取值范围为0 ≤ hθ(x) ≤ 1。

Question:
Which of the following statements is true?
A. If linear regression doesn't work on a classification task as in the previous example shown in the video, applying feature scaling may help.
B. If the training set satisfies 0 ≤ y(i) ≤ 1 for every training example (x(i), y(i)), then linear regression's prediction will also satisfy 0 ≤ hθ(x) ≤ 1 for all values of x.
C. If there is a feature x that perfectly predicts y, i.e. if y = 1 where x ≥ c and y = 0 whenever x ﹤c (for some constant c), then linear regression will obtain zero classification error.
D. None of the above statements are true.

综上所述,我们不难选出D这个正确答案。

补充笔记
Classification

To attempt classification, one method is to use linear regression and map all predictions greater than 0.5 as a 1 and all less than 0.5 as a 0. However, this method doesn't work well because classification is not actually a linear function.

The classification problem is just like the regression problem, except that the values we now want to predict take on only a small number of discrete values. For now, we will focus on the binary classification problem in which y can take on only two values, 0 and 1. (Most of what we say here will also generalize to the multiple-class case.) For instance, if we are trying to build a spam classifier for email, then x(i) may be some features of a piece of email, and y may be 1 if it is a piece of spam mail, and 0 otherwise. Hence, y∈{0,1}. 0 is also called the negative class, and 1 the positive class, and they are sometimes also denoted by the symbols “-” and “+.” Given x(i), the corresponding y(i) is also called the label for the training example.

分类问题建模(Hypothesis Representation)

之前,我们已经介绍一些关于逻辑回归的知识,现在我们开始正式学习逻辑回归。

之前我们介绍过逻辑回归模型其函数值始终在0~1之间,因此我们给出其数学表达式为:hθ(x) = g(z),其中g代表逻辑函数(也称为S形函数),其表达式为:

其函数图为:

该数学表达式中的 z = θTX,其中X为特征向量。因此,我们可将表达式改写成:

关于逻辑函数hθ(x)的函数值我们可以理解为在自变量x和参数θ分别取得某个值的情况下y=0或者y=1的概率,即hθ(x) = P(y=0|x;θ)或者hθ(x) = P(y=1|x;θ)。

由于逻辑回归模型其值非1即0,我们可得如下表达式:
  hθ(x) = P(y=0|x;θ) + hθ(x) = P(y=1|x;θ) = 1

因此,我们可以根据上述表达式在已知y=0(或y=1)的概率下,可求得y=1(或y=0)的概率。

补充笔记
Hypothesis Representation

We could approach the classification problem ignoring the fact that y is discrete-valued, and use our old linear regression algorithm to try to predict y given x. However, it is easy to construct examples where this method performs very poorly. Intuitively, it also doesn’t make sense for hθ(x) to take values larger than 1 or smaller than 0 when we know that y ∈ {0, 1}. To fix this, let’s change the form for our hypotheses hθ(x) to satisfy 0≤hθ(x)≤1. This is accomplished by plugging θTx into the Logistic Function.

Our new form uses the "Sigmoid Function," also called the "Logistic Function":

The following image shows us what the sigmoid function looks like:

The function g(z), shown here, maps any real number to the (0, 1) interval, making it useful for transforming an arbitrary-valued function into a function better suited for classification.

hθ(x) will give us the probability that our output is 1. For example, hθ(x)=0.7 gives us a probability of 70% that our output is 1. Our probability that our prediction is 0 is just the complement of our probability that it is 1 (e.g. if probability that it is 1 is 70%, then the probability that it is 0 is 30%).

边界判定(Decision Boundary)

逻辑函数的函数图像如下图所示:

从函数图中,我们可以得出如下结论:

  • 当z≥0时,即θTX ≥0,则有g(z)≥0.5,我们可以预测y=1
  • 当z≥0时,即θTX <0,则有g(z)<0.5,我们可以预测y=0

注:该结论为假设函数hθ(x)的属性,而非数据集的属性。

现假设hθ(x) = g(θ0 + θ1x1 + θ2x2),且设θ0 = -3,θ1 = 1,θ2 = 1。该假设函数hθ(x)拟合如图所示的数据集,我们通过上述的结论可知当θ0 + θ1x1 + θ2x2 ≥ 0时,即x1 + x2 ≥ 3时,我们可以预测y=1。当x1 + x2 = 3时为一条直线,因此我们可以在图中画出一条直线,这条直线我们称之为边界判断。

直线右侧部分我们可以将其划为y=1部分,左侧部分我们可以将其划为y=0部分。

除此之外,若hθ(x) = g(θ0 + θ1x1 + θ2x2 + θ3x12 + θ4x22),设θ0 = -1,θ1 = 0,θ2 = 0,θ3 = 1,θ4 = 1,我们可以得到一个半径为1圆心在原点的圆,且拟合图中数据集。

我们也称这个圆为边界判定。通过这两个案例,我们可以知道边界判定可以是任何形状。

补充笔记
Decision Boundary

In order to get our discrete 0 or 1 classification, we can translate the output of the hypothesis function as follows:

The way our logistic function g behaves is that when its input is greater than or equal to zero, its output is greater than or equal to 0.5:

Remember.

So if our input to g is θTX, then that means:

From these statements we can now say:

The decision boundary is the line that separates the area where y = 0 and where y = 1. It is created by our hypothesis function.

Example:

In this case, our decision boundary is a straight vertical line placed on the graph where x1=5, and everything to the left of that denotes y = 1, while everything to the right denotes y = 0.

Again, the input to the sigmoid function g(z) (e.g. θTX) doesn't need to be linear, and could be a function that describes a circle (e.g. z=θ01x122x22) or any shape to fit our data.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • 引用类型有哪些?非引用类型有哪些2.如下代码输出什么?为什么 基本类型值(数值、布尔值、null和undefine...
    饥人谷_Young丶K阅读 101评论 0 0
  • 阿呆是个绝对的路痴,再加严重的脸肓症,平日里认错人和路闹的笑话实在太多,所以人送外号“阿呆”。 阿呆人随和,同事们...
    福二姨阅读 1,024评论 25 24
  • 从小就跟着发小儿一家打游戏,插带的那种。他爸玩,他哥玩,他玩,于是带着我玩。每天十二人街霸,忍者龙剑传,不亦乐...
    Men_阅读 159评论 0 0
  • 女,三十多岁,怀孕期。感冒,清鼻涕多,头重,打喷嚏,眼里感觉随时有眼泪,头重还有些疼 ,小便深黄,味道难闻,大便干...
    中医李奇飞阅读 964评论 0 1