Sklearn 數據預處理與特徵工程 preprocessing&impute

  • 數據預處理:目的是爲了提高數據質量,使數據挖掘的過程更加有效,更加容易,同時也提高挖掘結果的質量。數據預處理的對象主要是清理其中的噪聲數據、空缺數據和不一致數據。
  • 特徵工程:降低計算成本、提升模型上限

目錄:

1、無量綱化

  •   線性:中心化處理、縮放處理
    • 中心化處理:中心化的本質是讓所有記錄減去一個固定值,讓所有的數據平移到某個位置。
    • 縮放處理:縮放的本質是通過處以一個固定值,將數據固定在某個範圍之內,取對數也算是一種縮放處理。
  •   非線性

2、缺失值
3、處理連續特徵
4、生成多項式特徵
5、自定義轉換器
6、編碼分類特徵

一、無量綱化

1、線性變換

sklearn.preprocessing.StandardScaler(copy=True, with_mean=True, with_std=True)

通過減掉均值並將數據縮放到單位方差來標準化特徵,標準化完後的特徵符合標準正態分佈,即方差爲1, 均值爲0.

sklearn.preprocessing.MinMaxScaler(feature_range=(0, 1), copy=True)

通過最大值最小值將每個特徵縮放到給定範圍,默認爲[0,1]。

X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
X_scaled = X_std * (max - min) + min

sklearn.preprocessing.MaxAbsScaler(copy=True)

通過讓每一個特徵裏的數據,除以該特徵中絕對值最大的數值,將數據縮放到[-1,1]。這種做法並沒有中心化數據,因此不會破壞數據的稀疏性。

sklearn.preprocessing.RobustScaler(with_centering=True, with_scaling=True, quantile_range=(25.0, 75.0), copy=True)

數據集的標準化是通過去除均值,縮放到單位方差來完成, 但是異常值通常會對樣本的均值和方差造成負面影響,當異常值很多噪聲很大時,用中位數和四分位範圍通常會產生更好的效果。
這個縮放器刪除中位數,並根據百分數範圍縮放數據。

sklearn.preprocessing.Normalizer(norm='l2', copy=True)[source]

norm = ‘l1’, ‘l2’, or ‘max’ (‘l2’ by default)
if “l1”: vector x / sum(abs(x));
if “l2”: vextor x / sum(x^2);

2、非線性變換

sklearn.preprocessing.PowerTransformer(method='yeo-johnson', standardize=True, copy=True)[source]

Power Transformer are applied to make data more Gaussian-like.
Currently, PowerTransformer supports the Box-Cox transform and the Yeo-Johnson transform.
Box-Cox requires input data to be strictly positive, while Yeo-Johnson supports both positive or negative data.

sklearn.preprocessing.QuantileTransformer(n_quantiles=1000, output_distribution='uniform', ignore_implicit_zeros=False, subsample=100000, random_state=None, copy=True)

通過縮小邊緣異常值和非異常值之間的距離來提供特徵的非線性變換。通過執行一個秩轉換能夠使異常的分佈平滑化,並且能夠比縮放更少地受到離羣值的影響。但是它的確使特徵間及特徵內的關聯和距離失真了。
該方法將特徵變換爲均勻分佈或正態分佈(通過設置output_distribution=‘normal’)。因此,對於給定的特性,這種轉換傾向於分散最頻繁的值,還減少了(邊緣)異常值的影響。

二、缺失值

sklearn.impute.SimpleImputer(missing_values=nan, strategy='mean', fill_value=None, verbose=0, copy=True, add_indicator=False)
參數 含義&輸入
missing_values 告訴SimpleImputer,數據中的缺失值長什麼樣,默認空值np.nan
strategy “mean”:使用均值填補;“media”:中值填補;“most_frequent”:衆數填補;“constan”:參考fill_value中的數據填補
fill_value 當參數“strategy”爲“constant”時可用,可輸入字符串或數字表示要填充的值
copy 默認爲True,將創建特徵矩陣的副本,反之則會將缺失值填補到原本的特徵矩陣中去

三、處理連續特徵

sklearn.preprocessing.Binarizer(threshold=0.0, copy=True)

根據閾值將數據二值化(將特徵值設爲0或1),大於閾值的值映射爲1,小於或等於的值映射爲0。

四、生成多項式特徵

sklearn.preprocessing.PolynomialFeatures(degree=2, interaction_only=False, include_bias=True, order='C')

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].

五、自定義轉換器

class sklearn.preprocessing.FunctionTransformer(func=None, inverse_func=None, validate=False, accept_sparse=False, check_inverse=True, kw_args=None, inv_kw_args=None)

在這裏插入圖片描述

六、編碼分類特徵

1、數據特徵

sklearn.preprocessing.OrdinalEncoder(categories='auto', dtype=<class 'numpy.float64'>)

將分類特徵轉換爲分類數值。
在這裏插入圖片描述

sklearn.preprocessing.OneHotEncoder(categories='auto', drop=None, sparse=True, dtype=<class 'numpy.float64'>, handle_unknown='error')[source]

在這裏插入圖片描述
2、標籤

sklearn.preprocessing.LabelBinarizer(neg_label=0, pos_label=1, sparse_output=False)

Binarize labels in a one-vs-all fashion
dim(result) = (number of data) * (number of targets)
在這裏插入圖片描述

sklearn.preprocessing.LabelEncoder

Encode target labels with value between 0 and n_classes-1.
在這裏插入圖片描述

sklearn.preprocessing.MultiLabelBinarizer(classes=None, sparse_output=False)

在這裏插入圖片描述

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