Linear regression with one variable (單變量線性回歸)

An example of linear regression

For example, from previous study, we can easly darw a conclusion that the model which the table described in the picture is a supervised learning model. And moreover, this is an example of Regression problem (回歸問(wèn)題).

And more formally, in superivised learning, we have a data set, which is called training set (訓(xùn)練集). The algorithm's job is to learn from this data how to predict the "right answer" (such as predict the prices of the houses).

Here is a example of training set

Here are some notations which can help us study:

  • m: represent the number of training examples (the number of rows in the above picture) .
  • x^(i): represent the "input" variable / features in the i-th row.
  • y^(i): represent the "output" variable / "target" variable in the i-th row.

To describle the supervised learning problem more formally, our goal is, given a training set, to learn a function h: x -> y so that h(x) is a "good" predictor for the corresponding value of y. For histirical reasons, this function h is called a hypothesis function(假設(shè)函數(shù)). Seen pictorially, the process is therefore like this:

So h is a function that maps from x (the size of house) to y (the estimated price) in this example, and according to supervised learning we can build a h like this:

This is also called linear regression with one variable (一元線性回歸) or univariate linear regression (單變量線性回歸), which is the basic block of learning other more complicated models.

Cost function (代價(jià)函數(shù))

Here is model with a training set, and we got its hypothesis:

The θi in the hypothesis is the parameters of the model (模型參數(shù)).

And the task of the algorithm is to get these two parameter values (θ0 and θ1), so that the straight line we get out of this corresponds to a straight line that somehow fits the data well.

For example:

What can be seen clearly is that based on different parameter values (θ0 and θ1), we can get different hypothesis.Therefore, in the example of predicting housing prices, we neet to predict the housing prices as correct as possible through choosing appropriate θ0 and θ1.

If we want to choose θ0 and θ1 to minimize the difference between h(x) and y, what we need to do is to minimize the square (平方) of the difference between the output value of the hypothetical function and the real price of the house (to let the cost function be the smallest/使得代價(jià)函數(shù)最小), which can be expressed by mathematical expression (數(shù)學(xué)表達(dá)式) as:

cost function

This is called cost function (代價(jià)函數(shù)) or squared error function (平方誤差函數(shù)), we can measure the accuracy of our hypothesis function by using a cost function.

To understand the cost function intuitively (直觀地) Ⅰ

Firstly, we use a simplified model like this (only one parameter θ1 -- the hypothesis functions that pass through the origin (原點(diǎn))):

So through different θ1 we can get different hypothesis function, and the result of the cost function J is also different:

when θ1 = 1
when θ1 = 0.5

And if we get more result of the function J through different θ1, then we can get a functional image of J like this:

cost function J

Our goal is to minumize the cost function J ( the h(x) line should pass though all the points of our training data set in the ideal situation). In this case, θ1 = 1 is our global minimum which is the minumum value of the cost function.

To understand the cost function intuitively Ⅱ

when it goes to h(x) = θ0 + θ1x, the cost function J has two variables (θ0 and θ1).

which makes the functional image of J a three-dimensional image (三維圖像). And we also can use a contour plots (等高線圖) to show it. A contour plot is a graph that contains many contour lines. A contour line of a two variable function has a constant value at all points of the same line.

a three-dimensional image of J
a contour figures

And through different θ1 and θ0 we can get different hypothesis function, and the result of the cost function J is also different (The contour line position of the results is different):

Our goal is to minumize the cost function J (the h(x) line should pass though all the points of our training data set as far as possible), like this:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末捺萌,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子葫男,更是在濱河造成了極大的恐慌蜈抓,老刑警劉巖飒炎,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件挂签,死亡現(xiàn)場(chǎng)離奇詭異堪侯,居然都是意外死亡舷夺,警方通過(guò)查閱死者的電腦和手機(jī)催蝗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門切威,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人丙号,你說(shuō)我怎么就攤上這事先朦。” “怎么了槽袄?”我有些...
    開封第一講書人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵烙无,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我遍尺,道長(zhǎng)截酷,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任乾戏,我火速辦了婚禮迂苛,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘鼓择。我一直安慰自己三幻,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開白布呐能。 她就那樣靜靜地躺著念搬,像睡著了一般。 火紅的嫁衣襯著肌膚如雪摆出。 梳的紋絲不亂的頭發(fā)上朗徊,一...
    開封第一講書人閱讀 52,156評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音偎漫,去河邊找鬼爷恳。 笑死,一個(gè)胖子當(dāng)著我的面吹牛象踊,可吹牛的內(nèi)容都是我干的温亲。 我是一名探鬼主播棚壁,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼栈虚!你這毒婦竟也來(lái)了袖外?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤魂务,失蹤者是張志新(化名)和其女友劉穎在刺,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體头镊,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年魄幕,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了相艇。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡纯陨,死狀恐怖坛芽,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情翼抠,我是刑警寧澤咙轩,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布,位于F島的核電站阴颖,受9級(jí)特大地震影響活喊,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜量愧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一钾菊、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧偎肃,春花似錦煞烫、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至紊馏,卻和暖如春料饥,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背瘦棋。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工稀火, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人赌朋。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓凰狞,卻偏偏與公主長(zhǎng)得像篇裁,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子赡若,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容