监督学习 | 非线性回归 之多项式回归原理及Sklearn实现
文章目錄
- 1. 多項(xiàng)式回歸
- 2. Sklearn 實(shí)現(xiàn)
- 參考資料
相關(guān)文章:
機(jī)器學(xué)習(xí) | 目錄
機(jī)器學(xué)習(xí) | 回歸評估指標(biāo)
監(jiān)督學(xué)習(xí) | 線性回歸 之多元線性回歸原理及Sklearn實(shí)現(xiàn)
監(jiān)督學(xué)習(xí) | 線性回歸 之正則線性模型原理及Sklearn實(shí)現(xiàn)
監(jiān)督學(xué)習(xí) | 線性分類 之Logistic回歸原理及Sklearn實(shí)現(xiàn)
1. 多項(xiàng)式回歸
對于非線性數(shù)據(jù),也可以用線性模型來擬合。一個簡單的方法就是將每個特征的冪次方添加為一個新特征,然后在這個拓展多的特征集上訓(xùn)練線性模型。這種方法被稱為多項(xiàng)式回歸。
回歸模型
(1)yi=β0+β1xi+β2xi2+εiy_i=\beta_0+\beta_1x_i+\beta_2x_i^2+\varepsilon_i \tag{1}yi?=β0?+β1?xi?+β2?xi2?+εi?(1)
稱為一元二階(或一元二次)多項(xiàng)式模型,其中,i=1,2,? ,ni=1,2,\cdots,ni=1,2,?,n。
為了反應(yīng)回歸系數(shù)所對應(yīng)的自變量次數(shù),我們通常將多項(xiàng)式回歸模型中的系數(shù)表示稱下面模型中的情形:
(2)yi=β0+β1xi+β11xi2+εiy_i=\beta_0+\beta_1x_i+\beta_{11}x_i^2+\varepsilon_i \tag{2}yi?=β0?+β1?xi?+β11?xi2?+εi?(2)
模型式 (2) 的回歸函數(shù) yi=β0+β1xi+β11xi2y_i=\beta_0+\beta_1x_i+\beta_{11}x_i^2yi?=β0?+β1?xi?+β11?xi2? 是一條拋物線,通常稱稱為二項(xiàng)式回歸函數(shù)。回歸系數(shù) β1\beta_1β1? 稱為線性效應(yīng)系數(shù),β11\beta_{11}β11? 為二次效應(yīng)系數(shù)。
相應(yīng)地,回歸模型
(3)yi=β0+β1xi+β11xi2+β111εiy_i=\beta_0+\beta_1x_i+\beta_{11}x_i^2+\beta_{111}\varepsilon_i \tag{3}yi?=β0?+β1?xi?+β11?xi2?+β111?εi?(3)
稱為一元三次多項(xiàng)式模型。[1]
2. Sklearn 實(shí)現(xiàn)
對于非線性的數(shù)據(jù),我們將利用 sklearn.preprocessing.PolynomialFeatures 將非線性數(shù)據(jù)通過多項(xiàng)式變換為線性數(shù)據(jù),然后就可以重復(fù) 監(jiān)督學(xué)習(xí) | 線性回歸 之多元線性回歸原理及Sklearn實(shí)現(xiàn) 中的方法完成回歸。
PolynomialFeatures(degree=2, interaction_only=False, include_bias=True, order=‘C’)
參數(shù)設(shè)置:
degree: integer
- The degree of the polynomial features. Default = 2.
interaction_only: boolean, default = False
- If true, only interaction features are produced: features that are products of at most degree distinct input features (so not x[1] ** 2, x[0] * x[2] ** 3, etc.).
include_bias: boolean
- If True (default), then include a bias column, the feature in which all polynomial powers are zero (i.e. a column of ones - acts as an intercept term in a linear model).
order: str in {‘C’, ‘F’}, default ‘C’
- Order of output array in the dense case. ‘F’ order is faster to compute, but may slow down subsequent estimators
方法:
powers_: array, shape (n_output_features, n_input_features)
- powers_[i, j] is the exponent of the jth input in the ith output.
n_input_features_: int
- The total number of input features.
n_output_features_: int
- The total number of polynomial output features. The number of output features is computed by iterating over all suitably sized combinations of input features.
首先基于二項(xiàng)式回歸函數(shù)制造一些非線性數(shù)據(jù)(并添加隨機(jī)噪聲)。
import numpy as np import numpy.random as rnd import matplotlib.pyplot as pltnp.random.seed(42)m = 100 X = 6 * np.random.rand(m, 1) - 3 y = 0.5 * X**2 + X + 2 + np.random.randn(m, 1)plt.plot(X, y, "b.") plt.xlabel("$x_1$", fontsize=18) plt.ylabel("$y$", rotation=0, fontsize=18) plt.axis([-3, 3, 0, 10]) plt.show() 圖1 生成的非線性帶噪聲數(shù)據(jù)集顯然,直線永遠(yuǎn)不可能擬合這個數(shù)據(jù)。所以我們使用 PolynomialFeatures 類來對訓(xùn)練數(shù)據(jù)進(jìn)行轉(zhuǎn)換,將每個特征的平方(二次多項(xiàng)式)作為新特征加入訓(xùn)練集(這個例子中只有一個特征):
from sklearn.preprocessing import PolynomialFeatures poly_features = PolynomialFeatures(degree=2, include_bias=False) X_poly = poly_features.fit_transform(X) X[0] array([-0.75275929]) X_poly[0] array([-0.75275929, 0.56664654])X_poly 現(xiàn)在包含原本的特征 X 和該特征的平方。現(xiàn)在對這個拓展后的特征集匹配一個 LinearRegression 模型。
from sklearn.linear_model import LinearRegression lin_reg = LinearRegression() lin_reg.fit(X_poly, y) lin_reg.intercept_, lin_reg.coef_ (array([1.78134581]), array([[0.93366893, 0.56456263]]))還不錯,模型預(yù)估 y^=0.56x12+0.93x11+1.78\hat{y}=0.56x_1^2+0.93x_11+1.78y^?=0.56x12?+0.93x1?1+1.78,而實(shí)際上原來的函數(shù)是 y=0.5x12+1.0x1+2.0+高斯噪聲y=0.5x_1^2+1.0x_1+2.0+高斯噪聲y=0.5x12?+1.0x1?+2.0+高斯噪聲 。
注意,當(dāng)存在多個特征時,多項(xiàng)式回歸能夠發(fā)現(xiàn)特征和特征之間的關(guān)系(純線性回歸模型做不到這一點(diǎn))。這是因?yàn)?PolynomialFeatures 會在給定的多項(xiàng)式階數(shù)下,添加所有特征組合。這是因?yàn)?PolynomialFeatures 會在給定的多項(xiàng)式階數(shù)下,添加所有特征組合(interaction_only = False)。例如,有兩個特征 a 和 b ,階數(shù) degree=3,PolynomialFeatures 不會只添加特征 a2,a3,b2和b3a^2,a^3,b^2和b^3a2,a3,b2和b3,還會添加組合 ab,a2bab,a^2bab,a2b 以及 ab2ab^2ab2。[2]
X_new=np.linspace(-3, 3, 100).reshape(100, 1) X_new_poly = poly_features.transform(X_new) y_new = lin_reg.predict(X_new_poly) plt.plot(X, y, "b.") plt.plot(X_new, y_new, "r-", linewidth=2, label="Predictions") plt.xlabel("$x_1$", fontsize=18) plt.ylabel("$y$", rotation=0, fontsize=18) plt.legend(loc="upper left", fontsize=14) plt.axis([-3, 3, 0, 10]) plt.show()PolynomialFeatures(degree=d) 可以將一個包含 nnn 個特征的數(shù)組為包含 n+dd!n!\frac{n+d}{d!n!}d!n!n+d? 個特征的數(shù)組。
參考資料
[1] 何曉群. 應(yīng)用回歸分析(R語言版)[M]. 北京: 電子工業(yè)出版社, 2018: 203-204.
[2] Aurelien Geron, 王靜源, 賈瑋, 邊蕤, 邱俊濤. 機(jī)器學(xué)習(xí)實(shí)戰(zhàn):基于 Scikit-Learn 和 TensorFlow[M]. 北京: 機(jī)械工業(yè)出版社, 2018: 115-117.
總結(jié)
以上是生活随笔為你收集整理的监督学习 | 非线性回归 之多项式回归原理及Sklearn实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 写python的c扩展简介
- 下一篇: C运行库