Linear regression with one variable - Cost function intuition I
摘要: 本文是吴恩达 (Andrew Ng)老师《机器学习》课程,第二章《单变量线性回归》中第8课时《代价函数的直观认识 - 1》的视频原文字幕。为本人在视频学习过程中逐字逐句记录下来以便日后查阅使用。现分享给大家。如有错误,欢迎大家批评指正,在此表示诚挚地感谢!同时希望对大家的学习能有所帮助。
In the previous video (article), we gave the mathematical definition of the cost function. In this video (article), let's look at some examples to get back to intuition about what the cost function is doing, and why we want to use it.

To recap, here's what we had last time. We want to fit a straight line to our data, so we had this formed as a hypothesis with these parameters and
, and with different choices of the parameters, we end up with different straight-line fits. So, the data which are fit like so. And there's a cost function, and that was our optimization objective. For this video (article), in order to better visualize the cost function J, I'm going to work with a simplified hypothesis function, like that shown on the right. So, I'm gonna use my simplified hypothesis which is just
. We can, if you want, think of this as setting the parameter
. So, I have only one parameter
, and my cost function is similar to before except that now
. And I have only one parameter
, and so my optimization objective is to minimize
. In pictures, what this means is that if
that corresponds to choosing only hypothesis functions that pass through the origin, that pass through the point
. Using the simplified definition of hypothesis cost function, let's try to understand the cost function concept better.
It turns out that two key functions we want to understand. The first is the hypothesis function, and the second is the cost function. So, notice that the hypothesis, right, . For a fixed value of
, this is a function of x. So, the hypothesis is a function of what is the size of the house x. In contrast, the cost function J, that's a function of the parameter
which controls the slope of the straight line. Let's plot these functions and try to understand them both better. Let's start with the hypothesis. On the left, let's say here's my training set with three points at
. Let's pick a value
, so when set
=1, and if that's my choice for
, then my hypothesis is going to look like this straight line over here. And I'm gonna point out when I'm plotting my hypothesis function, my X-axis, my horizontal axis is labeled x, is labeled as you know, size of the house over here. Now, of temporary, set
. What I want to do is figure out what is
when
=1. So, let's go ahead and compute what the cost function has for the value one. Well, as usual, my cost function is defined as follows, right? Sum from some of them are my training set of this usual squared error term. And this is therefore equal to
, and if you simplify, this turns out to be
, which is of course, just equal to 0. Now, inside the cost function, it turns out, each of these terms here is equal to 0. Because for the specific training set I have, for my 3 training examples there,
, if
, then
exactly. And so,
, each of these terms is equal to 0, which is why I find that
. Let's plot that. What I'm gonna do on the right is plot my cost function J. And notice, because my cost function is a function of my parameter
, when I plot my cost function, the horizontal axis is now labeled with
. So, I have
, so let's go ahead and plot that. End up with an X over there. Now let's look at some other examples.
can take on a range of different values. Right? So
can take on the negative values, zero and positive values. So, what if
? Let's go ahead and plot that.
I'm now going to set , and in that case, my hypothesis looks like this. As a line with slope equals to 0.5. And, let's compute
. So, that is going to be
of my usual cost function. It turns out that the cost function is going to be the sum of square values of the height of this line, plus the sum of square of the height of that line, plus the sum of square of the height of that line, right? Because just this vertical distance, that's the difference between
and the predicted value
. So, the first example is going to be
. For my second example, I get
, because my hypothesis predicted one, but the actual housing price was two. And finally, plus
. And so that's equal to
. So now we know
is about 0.58. Let's go and plot that. So, we plot that which is maybe about over there. Now, let's do one more. How about if
, what is
equal to?
It turns out if ,
is just equal to 0, you know, this flat line, that just goes horizontally like his. And so, measuring the errors. We have that just
. So, let's go ahead and plot that as well. So, it ends up with a value around 2.3. And of course, we can keep on doing this for other values of
. It turns out that you can have negative for other values of
as well. So if
is negative, then
would be equal to say
, then
, and so that corresponds to a hypothesis with a slope of -0.5. And you can actually keep on computing these errors. This turns out to be, you know, for -0.5, it turns out to have really high error. It works out to be something, like, 5.25 and so on. And for different values of
, you can compute these things. And it turns out that you computed range of values, you get something like that. And by computing the range of values, you can actually slowly create out what this function
looks like. And that's what
is. To recap, for each value of
, right? Each value of
corresponds to a different hypothesis, or to a different straight line fit on the left. And for each value of
, we could then derive a different a different value of
. And for example,
corresponds to this straight line (in cyan) straight through the data. Whereas
, and this point shown in magenta, corresponded to maybe that line (in magenta). And
, which is shown in blue, that corresponds to this horizontal line (in blue). So, for each value of
, we wound up with a different value of
. And then we could use this to trace out this plot on the right. Now you remember the optimization objective for our learning algorithm is we want to choose the value of
, that minimizes
. This (
) was our objective function for the linear regression. Well, looking at this curve, the value that minimizes
is
. And low and behold, that is indeed the best possible straight line fit throughout data, by setting
. And just for this particular training set, we actually end up fitting it perfectly. And that's why minimizing
corresponds to finding a straight line that fits the data well. So, to wrap up, in this video (article), we looked at some plots to understand the cost function. To do so, we simplified the algorithm, so that it only had one parameter
. And we set the parameter
. In the next video (article), we'll go back to the original problem formulation, and look at some visualizations involving both
and
. That is without setting
. And hopefully that will give you an even better sense of what the cost function J is doing in the original linear regression.
Linear regression with one variable - Cost function intuition I的更多相关文章
- Linear regression with one variable - Cost function
摘要: 本文是吴恩达 (Andrew Ng)老师<机器学习>课程,第二章<单变量线性回归>中第7课时<代价函数>的视频原文字幕.为本人在视频学习过程中逐字逐句记录下 ...
- Lecture0 -- Introduction&&Linear Regression with One Variable
Introduction What is machine learning? Tom Mitchell provides a more modern definition: "A compu ...
- Stanford机器学习---第二讲. 多变量线性回归 Linear Regression with multiple variable
原文:http://blog.csdn.net/abcjennifer/article/details/7700772 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...
- Stanford机器学习---第一讲. Linear Regression with one variable
原文:http://blog.csdn.net/abcjennifer/article/details/7691571 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...
- 机器学习笔记1——Linear Regression with One Variable
Linear Regression with One Variable Model Representation Recall that in *regression problems*, we ar ...
- Machine Learning 学习笔记2 - linear regression with one variable(单变量线性回归)
一.Model representation(模型表示) 1.1 训练集 由训练样例(training example)组成的集合就是训练集(training set), 如下图所示, 其中(x,y) ...
- MachineLearning ---- lesson 2 Linear Regression with One Variable
Linear Regression with One Variable model Representation 以上篇博文中的房价预测为例,从图中依次来看,m表示训练集的大小,此处即房价样本数量:x ...
- 机器学习 (一) 单变量线性回归 Linear Regression with One Variable
文章内容均来自斯坦福大学的Andrew Ng教授讲解的Machine Learning课程,本文是针对该课程的个人学习笔记,如有疏漏,请以原课程所讲述内容为准.感谢博主Rachel Zhang的个人笔 ...
- machine learning (2)-linear regression with one variable
machine learning- linear regression with one variable(2) Linear regression with one variable = univa ...
随机推荐
- 2019牛客暑期多校训练营(第七场)D Number——实系数多项式因式分解定理
前置知识 代数基本定理 定理:每个次数 ≥ 1 复系数多项式在复数域中至少有一个跟. 由此推出,n次复系数多项式方程在复数域内有且只有n个根(重根按重数计算).(只要不断把多项式除以(x-xa),即可 ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) A. Forgetting Things
链接: https://codeforces.com/contest/1247/problem/A 题意: Kolya is very absent-minded. Today his math te ...
- JavaScript有趣的知识点
JavaScript中总有一些有趣的小知识,而且又是很容易犯错的.我把我遇到的慢慢罗列一下,方便大家避坑 typeof(null)返回的结果是 object " "变成布尔类型为t ...
- Python3发送webservice请求
Python3使用suds-jurko库来发送webservice接口请求 导入请求webservice接口需要用到的包 pip install suds-jurko 第一步:导入所需要的包 from ...
- JavaMail使用SMTP协议发送电子邮件(详解)
Properties props = new Properties(); props.setProperty("mail.transport.protocol", "sm ...
- 分布式锁的三种实现方式 数据库、redis、zookeeper
版权声明: https://blog.csdn.net/wuzhiwei549/article/details/80692278 一.为什么要使用分布式锁 我们在开发应用的时候,如果需要对某一个共享变 ...
- 使用 ALinq 实现 Linq to MySQL【转】
http://www.cnblogs.com/huangcong/archive/2011/05/24/2055204.html
- 通过无线网络使用ADB ( Connect to android with ADB over TCP )
之前用USB数据线连接电脑和android手机,用adb操控手机. 后来电脑前面板的USB全坏了,键盘.鼠标.USBKkey.打印机都需USB, 少一个USB口,只得对不常用设备进行插拔切换. 于是 ...
- 利用模板和C++11特性实现的智能指针-作用同share_ptr
根据C++11特性实现,基本上实现了同SharePtr同样的功能,有时间继续优化.之前一直以为引用计数是一个静态的int类型,实际上静态值是不可以的.之前项目中总是不太习惯使用智能指针.通过自实现的方 ...
- mkswap/swapon/swapoff/free
free mkswap 创建Linux交换分区 swapon 启用交换分区 swapoff 关闭交换分区 注意: 在创建完交换区之后.是需要激活才能使用的 swapon/swapoff