scikit-learn學習之預處理(preprocessing)一


一、標准化,均值去除和按方差比例縮放

  數據集的標准化:當個體特征太過或明顯不遵從高斯正態分布時,標准化表現的效果較差。實際操作中,經常忽略特征數據的分布形狀,移除每個特征均值,划分離散特征的標准差,從而等級化,進而實現數據中心化。

  scale 

1 >>> from sklearn import preprocessing
2 >>> import numpy as np
3 >>> X = np.array([[1., -1., 2.], [2., 0., 0.], [0., 1., -1.]])
4 >>> X_scaled = preprocessing.scale(X)
5 >>> X_scaled
6 array([[ 0.        , -1.22474487,  1.33630621],
7        [ 1.22474487,  0.        , -0.26726124],
8        [-1.22474487,  1.22474487, -1.06904497]])
View Code

  注:scaled data 的均值為0,方差為1。

1 >>> X_scaled.mean(axis=0)  # column mean
2 array([ 0.,  0.,  0.])
3 >>> X_scaled.std(axis=0)
4 array([ 1.,  1.,  1.])
View Code

  StandardScaler

 1 >>> scaler = preprocessing.StandardScaler().fit(X)
 2 >>> scaler
 3 StandardScaler(copy=True, with_mean=True, with_std=True)
 4 >>> scaler.mean_
 5 array([ 1.        ,  0.        ,  0.33333333])
 6 >>> scaler.std_
 7 array([ 0.81649658,  0.81649658,  1.24721913])
 8 >>> scaler.transform(X, 0)
 9 array([[ 0.        , -1.22474487,  1.33630621],
10        [ 1.22474487,  0.        , -0.26726124],
11        [-1.22474487,  1.22474487, -1.06904497]])
12 >>> scaler.transform([[-1., 1., 0.]], 0.)  #scale the new data
13 array([[-2.44948974,  1.22474487, -0.26726124]])
View Code  

  注:scale和StandardScaler可以用於回歸模型中的目標值處理。

1.scaling features to a range

  MinMaxScaler(最小最大值化)

  公式:X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0)) ; X_scaler = X_std / (max - min) + min

  訓練過程(fit_transform())

1 >>> scaler = preprocessing.StandardScaler().fit(X,None)
2 >>> scaler = preprocessing.StandardScaler().fit(X)
3 >>> X_train = np.array([[1., -1., 2.], [2., 0., 0.], [0., 1., -1.]])
4 >>> min_max_scaler = preprocessing.MinMaxScaler()
5 >>> X_train_minmax = min_max_scaler.fit_transform(X_train)
6 >>> X_train_minmax
7 array([[ 0.5       ,  0.        ,  1.        ],
8        [ 1.        ,  0.5       ,  0.33333333],
9        [ 0.        ,  1.        ,  0.        ]])
View Code

  測試過程 (transform())

1 >>> X_test = np.array([[ -3., -1., 4.]])
2 >>> X_test_minmax = min_max_scaler.transform(X_test)
3 >>> X_test_minmax
4 array([[-1.5       ,  0.        ,  1.66666667]])
View Code

2.Nomalization

  向量空間模型(VSM)的基礎,用於文本分類和聚類處理

  normalize

  sklearn.preprocessing.normalize(X, norm='l2', axis=1, copy=Ture)

    l1: 標准化每個非0樣本(row,axis=0) l2:標准化每個非0特征(column,axis=1)

  Normalizer

1 >>> X
2 array([[ 1., -1.,  2.],
3        [ 2.,  0.,  0.],
4        [ 0.,  1., -1.]])
5 >>> normalizer = preprocessing.Normalizer().fit(X)
6 >>> normalizer
7 Normalizer(copy=True, norm='l2')
View Code

  注:normalizer實例可以作為其他數據的轉換器

3.Binarization

  feature binarization是將數值型的特征值轉換為布爾值,可以用於概率估計

1 >>> binarizer = preprocessing.Binarizer().fit(X)
2 >>> binarizer
3 Binarizer(copy=True, threshold=0.0)
4 >>> binarizer.transform(X)
5 array([[ 1.,  0.,  1.],
6        [ 1.,  0.,  0.],
7        [ 0.,  1.,  0.]])
View Code

4.Encoding categorical features

  類別型特征用整數值進行編碼,OneHotEncoder將m種值轉換為m個二元位,其中只有一位是活躍的。

1 >>> enc = preprocessing.OneHotEncoder()
2 >>> enc.fit([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]])
3 OneHotEncoder(categorical_features='all', dtype=<type 'float'>,
4        n_values='auto', sparse=True)
5 >>> enc.transform([[0, 1, 3]]).toarray()
6 array([[ 1.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  1.]])  # from letf to right,the first '1,0' represent 0
View Code

  默認情況下,每個特征有幾種值是由數據集確定的。可以通過n_values參數對其進行顯性指定。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM