機器學習 | 特徵縮放

1. 特徵縮放

特徵縮放(Feature Scaling)

1.1 最大最小值歸一化(min-max normalization)

x=xmin(x)max(x)min(x)(1) x^\prime=\frac{x-min(x)}{max(x)-min(x)} \tag{1}

將數據歸一化到 [0,1][0,1]

sklearn.preprocessing.MinMaxScaler

MinMaxScaler(feature_range=(0, 1), copy=True)

1.2 均值歸一化(mean normalization)

x=xxˉmax(x)min(x)(2) x^\prime=\frac{x-\bar{x}}{max(x)-min(x)} \tag{2}

將數據歸一化到 [1,1][-1,1]

1.3 中心化(mean centering)

x=xxˉ(3) x^{\prime}=x-\bar{x} \tag{3}

將數據縮放到 0 附近。

1.4 標準化 / z值歸一化(standardization / z-score normalization)

x=xxˉσ(4) x^{\prime}=\frac{x-\bar{x}}{\sigma} \tag{4}

將數據標準化爲均值爲 0 ,標準差爲 1。

sklearn.preprocessing.StandardScaler

StandardScaler(copy=True, with_mean=True, with_std=True)

1.5 最大絕對值歸一化(max-abs normalization)

x=xmax(x)(5)x^\prime=\frac{x}{\|max(x)\|} \tag{5}

適用於稀疏數據,將數據縮放到 [1,1][-1,1]

sklearn.preprocessing.Normalizer

Normalizer(norm=‘l2’, copy=True)

norm =‘max’ 時爲最大絕對值歸一化(好像沒有絕對值)。

1.6 穩健歸一化(robust normalization)

先減去中位數,再除以四分位距(interquartile range, IQR)。因爲不涉及極值,因此在數據裏有異常值的情況下表現比較穩健。

x=xmedian(x)IQR(x)(6)x^\prime = \frac{x-median(x)}{IQR(x)} \tag{6}

sklearn.preprocessing.robust_scale

robust_scale(X, axis=0, with_centering=True, with_scaling=True, quantile_range=(25.0, 75.0), copy=True)

2. 需要進行縮放的模型

通過梯度下降法求解的模型需要進行特徵縮放,這包括線性迴歸(Linear Regression)、邏輯迴歸(Logistic Regression)、感知機(Perceptron)、支持向量機(SVM)、神經網絡(Neural Network)等模型。

此外,近鄰法(KNN),K均值聚類(K-Means)等需要根據數據間的距離來劃分數據的算法也需要進行特徵縮放。

主成分分析(PCA),線性判別分析(LDA)等需要計算特徵的方差的算法也會受到特徵縮放的影響。

3. 不需要進行縮放的模型

決策樹(Decision Tree),隨機森林(Random Forest)等基於樹的模型不需要進行特徵縮放,因爲特徵縮放不會改變樣本在特徵上的信息增益。

4. 進行特徵縮放的注意事項

需要先把數據拆分成訓練集與驗證集,在訓練集上計算出需要的數值(如均值和標準值),對訓練集數據做標準化/歸一化處理(不要在整個數據集上做標準化/歸一化處理,因爲這樣會將驗證集的信息帶入到訓練集中,這是一個非常容易犯的錯誤),然後再用之前計算出的數據(如均值和標準值)對驗證集數據做相同的標準化/歸一化處理。

參考資料

[1] HuZihu.[ML] 特徵縮放(Feature Scaling)[EB/OL].https://www.cnblogs.com/HuZihu/p/9761161.html, 2019-08-11.

[2] Sebastian Raschka.[ML] About Feature Scaling and Normalization
– and the effect of standardization for machine learning algorithms[EB/OL].https://sebastianraschka.com/Articles/2014_about_feature_scaling.html, 2014-7-11.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章