亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

如何在python中實(shí)現(xiàn)線性回歸

 更新時(shí)間:2020年08月10日 14:37:42   作者:劉早起  
這篇文章主要介紹了如何在python中實(shí)現(xiàn)線性回歸,幫助大家更好的理解和學(xué)習(xí)Python,感興趣的朋友可以了解下

線性回歸是基本的統(tǒng)計(jì)和機(jī)器學(xué)習(xí)技術(shù)之一。經(jīng)濟(jì),計(jì)算機(jī)科學(xué),社會(huì)科學(xué)等等學(xué)科中,無(wú)論是統(tǒng)計(jì)分析,或者是機(jī)器學(xué)習(xí),還是科學(xué)計(jì)算,都有很大的機(jī)會(huì)需要用到線性模型。建議先學(xué)習(xí)它,然后再嘗試更復(fù)雜的方法。

本文主要介紹如何逐步在Python中實(shí)現(xiàn)線性回歸。而至于線性回歸的數(shù)學(xué)推導(dǎo)、線性回歸具體怎樣工作,參數(shù)選擇如何改進(jìn)回歸模型將在以后說(shuō)明。

回歸

回歸分析是統(tǒng)計(jì)和機(jī)器學(xué)習(xí)中最重要的領(lǐng)域之一。有許多可用的回歸方法。線性回歸就是其中之一。而線性回歸可能是最重要且使用最廣泛的回歸技術(shù)之一。這是最簡(jiǎn)單的回歸方法之一。它的主要優(yōu)點(diǎn)之一是線性回歸得到的結(jié)果十分容易解釋。那么回歸主要有:

  • 簡(jiǎn)單線性回歸
  • 多元線性回歸
  • 多項(xiàng)式回歸

如何在python中實(shí)現(xiàn)線性回歸

用到的packages

  • NumPy

NumPy是Python的基礎(chǔ)科學(xué)軟件包,它允許在單維和多維數(shù)組上執(zhí)行許多高性能操作。

  • scikit-learn

scikit-learn是在NumPy和其他一些軟件包的基礎(chǔ)上廣泛使用的Python機(jī)器學(xué)習(xí)庫(kù)。它提供了預(yù)處理數(shù)據(jù),減少維數(shù),實(shí)現(xiàn)回歸,分類,聚類等的方法。

  • statsmodels

如果要實(shí)現(xiàn)線性回歸并且需要功能超出scikit-learn的范圍,則應(yīng)考慮使用statsmodels可以用于估算統(tǒng)計(jì)模型,執(zhí)行測(cè)試等。

scikit-learn的簡(jiǎn)單線性回歸

1.導(dǎo)入用到的packages和類

import numpy as np
from sklearn.linear_model import LinearRegression

2.創(chuàng)建數(shù)據(jù)

x = np.array([5, 15, 25, 35, 45, 55]).reshape((-1, 1))
y = np.array([5, 20, 14, 32, 22, 38])

現(xiàn)在就生成了兩個(gè)數(shù)組:輸入x(回歸變量)和輸出y(預(yù)測(cè)變量),來(lái)看看

>>> print(x)
[[ 5]
 [15]
 [25]
 [35]
 [45]
 [55]]
>>> print(y)
[ 5 20 14 32 22 38]

可以看到x是二維的而y是一維的,因?yàn)樵趶?fù)雜一點(diǎn)的模型中,系數(shù)不只一個(gè)。這里就用到了.reshape()來(lái)進(jìn)行轉(zhuǎn)換。

3.建立模型

創(chuàng)建一個(gè)類的實(shí)例LinearRegression,它將代表回歸模型:

model = LinearRegression()

現(xiàn)在開始擬合模型,首先可以調(diào)用.fit()函數(shù)來(lái)得到優(yōu)的?₀和?₁,具體有下面兩種等價(jià)方法

model.fit(x, y)
model = LinearRegression().fit(x, y)

4.查看結(jié)果

擬合模型之后就是查看與模型相關(guān)的各項(xiàng)參數(shù)

>>> r_sq = model.score(x, y)
>>> print('coefficient of determination:', r_sq)
coefficient of determination: 0.715875613747954

.score()函數(shù)可以獲得模型的?²,再看看系數(shù)

>>> print('intercept:', model.intercept_)
intercept: 5.633333333333329
>>> print('slope:', model.coef_)
slope: [0.54]

可以看到系數(shù)和截距分別為[0.54]和5.6333,注意系數(shù)是一個(gè)二維數(shù)組哦。

5.預(yù)測(cè)效果

一般而言,線性模型最后就是用來(lái)預(yù)測(cè),我們來(lái)看下預(yù)測(cè)效果

>>> y_pred = model.predict(x)
>>> print('predicted response:', y_pred, sep='\n')
predicted response:
[ 8.33333333 13.73333333 19.13333333 24.53333333 29.93333333 35.33333333]

當(dāng)然也可以使用下面的方法

>>> y_pred = model.intercept_ + model.coef_ * x
>>> print('predicted response:', y_pred, sep='\n')
predicted response:
[[ 8.33333333]
 [13.73333333]
 [19.13333333]
 [24.53333333]
 [29.93333333]
 [35.33333333]]

除了可以利用樣本內(nèi)的數(shù)據(jù)進(jìn)行預(yù)測(cè),也可以用樣本外的數(shù)據(jù)進(jìn)行預(yù)測(cè)。

>>> x_new = np.arange(5).reshape((-1, 1))
>>> print(x_new)
[[0]
 [1]
 [2]
 [3]
 [4]]
>>> y_new = model.predict(x_new)
>>> print(y_new)
[5.63333333 6.17333333 6.71333333 7.25333333 7.79333333]

至此,一個(gè)簡(jiǎn)單的線性回歸模型就建立起來(lái)了。

scikit-learn的多元線性回歸

直接開始吧

1.導(dǎo)入包和類,并創(chuàng)建數(shù)據(jù)

import numpy as np
from sklearn.linear_model import LinearRegression

x = [[0, 1], [5, 1], [15, 2], [25, 5], [35, 11], [45, 15], [55, 34], [60, 35]]
y = [4, 5, 20, 14, 32, 22, 38, 43]
x, y = np.array(x), np.array(y)

看看數(shù)據(jù)

>>> print(x)
[[ 0 1]
 [ 5 1]
 [15 2]
 [25 5]
 [35 11]
 [45 15]
 [55 34]
 [60 35]]
>>> print(y)
[ 4 5 20 14 32 22 38 43]

2.建立多元回歸模型

model = LinearRegression().fit(x, y)

3.查看結(jié)果

>>> r_sq = model.score(x, y)
>>> print('coefficient of determination:', r_sq)
coefficient of determination: 0.8615939258756776
>>> print('intercept:', model.intercept_)
intercept: 5.52257927519819
>>> print('slope:', model.coef_)
slope: [0.44706965 0.25502548]

4.預(yù)測(cè)

#樣本內(nèi)
>>> y_pred = model.predict(x)
>>> print('predicted response:', y_pred, sep='\n')
predicted response:
[ 5.77760476 8.012953  12.73867497 17.9744479 23.97529728 29.4660957
 38.78227633 41.27265006]
#樣本外
>>> x_new = np.arange(10).reshape((-1, 2))
>>> print(x_new)
[[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]
>>> y_new = model.predict(x_new)
>>> print(y_new)
[ 5.77760476 7.18179502 8.58598528 9.99017554 11.3943658 ]

所有的結(jié)果都在結(jié)果里,就不再過多解釋。再看看多項(xiàng)式回歸如何實(shí)現(xiàn)。

多項(xiàng)式回歸

導(dǎo)入包和創(chuàng)建數(shù)據(jù)

import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
x = np.array([5, 15, 25, 35, 45, 55]).reshape((-1, 1))
y = np.array([15, 11, 2, 8, 25, 32])

多項(xiàng)式回歸和之前不一樣的是需要對(duì)數(shù)據(jù)轉(zhuǎn)換,因?yàn)槟P屠锇?#63;²等變量,所以在創(chuàng)建數(shù)據(jù)之后要將x轉(zhuǎn)換為?²。

transformer = PolynomialFeatures(degree=2, include_bias=False)

再看看數(shù)據(jù)

>>> print(x_)
[[  5.  25.]
 [ 15. 225.]
 [ 25. 625.]
 [ 35. 1225.]
 [ 45. 2025.]
 [ 55. 3025.]]

建模

接下來(lái)的步驟就和之前的類似了。其實(shí)多項(xiàng)式回歸只是多了個(gè)數(shù)據(jù)轉(zhuǎn)換的步驟,因此從某種意義上,多項(xiàng)式回歸也算是線性回歸。

model = LinearRegression().fit(x_, y)

查看結(jié)果

>>> r_sq = model.score(x_, y)
>>> print('coefficient of determination:', r_sq)
coefficient of determination: 0.8908516262498564
>>> print('intercept:', model.intercept_)
intercept: 21.372321428571425
>>> print('coefficients:', model.coef_)
coefficients: [-1.32357143 0.02839286]

預(yù)測(cè)

>>> y_pred = model.predict(x_)
>>> print('predicted response:', y_pred, sep='\n')
predicted response:
[15.46428571 7.90714286 6.02857143 9.82857143 19.30714286 34.46428571]

那么本次多項(xiàng)式回歸的所有結(jié)果都在上面了,一目了然。

以上就是如何在python中實(shí)現(xiàn)線性回歸的詳細(xì)內(nèi)容,更多關(guān)于Python實(shí)現(xiàn)線性回歸的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論