在人工智能課程中學習線性回歸一章時,高階線性回歸需要用到PolynomialFeatures方法構造特征。
先看一下官方文檔對於sklearn.preprocessing.PolynomialFeatures方法的解釋:
Generate polynomial and interaction features.
Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree. For example, if an input sample is two dimensional and of the form [a, b], the degree-2 polynomial features are [1, a, b, a^2, ab, b^2].
簡單翻譯一下,意思就是:
生成多項式交互特征。
生成一個新的特征矩陣,包含特定階數及以下的全部多項式組合。例如,樣本特征為二維的,包含[a, b]。其全部二階多項式特征為[1, a, b, a^2, ab, b^2]。
解釋一下,其中包含0階特征[1],一階特征為[a, b],二階特征[a^2, ab, b^2]。也就是說,你只要輸入[a, b],自動生成並返回[1, a, b, a^2, ab, b^2]這樣一個特征矩陣。(偏置值設為默認值include_bias=True)
在用線性模型LinearRegression擬合時,輸入新生成的特征矩陣和標簽值矩陣,便可以擬合訓練為一個相應高階的模型。
下面展示一下PolynomialFeatures的使用:
1、首先創建一個數據集。
將其分為訓練集和驗證集,由於這里用不到所以先不生成測試集了。
import numpy as np from sklearn.model_selection import train_test_split # 生成訓練集與驗證集,數據帶有標准差為0.1的噪聲 n = 100 n_train = int(0.8 * n) n_valid = int(0.2 * n) x = 6 * np.random.rand(n, 1) - 3 y = 1.2 * x - 3.4 * (x ** 2) + 5.6 * (x ** 3) + 5 + 0.1 * np.random.randn(n, 1) x_train_set, x_valid_set, y_train_set, y_valid_set = train_test_split(x, y, test_size=0.2, random_state=5)
2、調用PolynomialFeatures方法生成特征矩陣。
由於我們的特征樣本只有[x],並且設為三階(degree=3),所以生成的特征矩陣(include_bias=True)為[1, x, x^2, x^3]。
可以看到矩陣下標為0的這列全部為‘1’,這就是偏置值的作用。
3、設置偏置值include_bias=False
生成的特征矩陣變為[x, x^2, x^3]
4.1、帶入公式計算參數theta
此時的X_poly是include_bias=True時生成的
4.2、或是使用sklearn.linear_model.LinearRegression擬合模型
此時的X_poly是include_bias=False生成的
5、Pipeline中inlude_bias的設置
根據上面的例子,我們可以看到,使用sklearn的LinearRegression方法進行模型擬合時,輸入的是不含偏置值的特征矩陣,即include_bias=False。
同理,可以理解,在使用sklearn.pipeline.Pipeline是,如果需要生成多項式特征矩陣,LinearRegression方法的偏置值設置也是include_bias=False。
如下圖