您的位置:首页 > 其它

scikit-learn数据预处理

2017-07-01 11:52 429 查看

Python下的机器学习工具scikit-learn(-数据预处理)

参考自http://lib.csdn.net/article/machinelearning/1119http://scikit-learn.org/stable/modules/preprocessing.html

(1)数据标准化(Standardization or Mean Removal and Variance Scaling)

进行标准化缩放的数据均值为0,具有单位方差。

#scale函数提供一种便捷的标准化转换操作,如下:
from sklearn import preprocessing #导入数据预处理包
X=[[1.,-1.,2.],
[2.,0.,0.],
[0.,1.,-1.]]
X_scaled=preprocessing.scale(X)
X_scaled


array([[ 0.        , -1.22474487,  1.33630621],
[ 1.22474487,  0.        , -0.26726124],
[-1.22474487,  1.22474487, -1.06904497]])


X_scaled.mean(axis=0)#preprocessing.scale()方法默认是按0轴(x坐标)缩放的


array([ 0.,  0.,  0.])


X_scaled.std(axis=0)


array([ 1.,  1.,  1.])


同样我们也可以通过preprocessing模块提供的Scaler(StandardScaler 0.15以后版本)工具类来实现这个功能:

scaler=preprocessing.StandardScaler().fit(X)
scaler


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


scaler.mean_


array([ 1.        ,  0.        ,  0.33333333])


scaler.scale_#scaler.std_ will be removed in 0.19. Use ``scale_`` instead


array([ 0.81649658,  0.81649658,  1.24721913])


scaler.transform(X)


array([[ 0.        , -1.22474487,  1.33630621],
[ 1.22474487,  0.        , -0.26726124],
[-1.22474487,  1.22474487, -1.06904497]])


(2)数据规范化(Normalization)

将矩阵缩放至[0,1]区间

import numpy as np
X_train = np.array([[ 1., -1.,  2.],
[ 2.,  0.,  0.],
[ 0.,  1., -1.]])
min_max_scaler=preprocessing.MinMaxScaler()
X_train_minmax=min_max_scaler.fit_transform(X_train)#每个元素减去行最小值然后除以每行最大值与最小值的差
X_train_minmax


array([[ 0.5       ,  0.        ,  1.        ],
[ 1.        ,  0.5       ,  0.33333333],
[ 0.        ,  1.        ,  0.        ]])


#上面的min_max_scaler可以用来适配新的数据
X_test=np.array([[-3.,-1.,4.]])
X_test_minmax=min_max_scaler.transform(X_test)
X_test_minmax


array([[-1.5       ,  0.        ,  1.66666667]])


min_max_scaler.scale_


array([ 0.5       ,  0.5       ,  0.33333333])


min_max_scaler.min_


array([ 0.        ,  0.5       ,  0.33333333])


如果MinMaxScaler被赋予了一个显式的feature_range =(min,max),则完整的公式是:

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

X_scaled = X_std / (max - min) + min

MaxAbsScaler以非常相似的方式工作,但通过划分每个特征中最大的最大值,训练数据位于[-1,1]范围内。它是针对已经以零或稀疏数据为中心的数据。

X_train = np.array([[ 1., -1.,  2.],
[ 2.,  0.,  0.],
[ 0.,  1., -1.]])
max_abs_scaler=preprocessing.MaxAbsScaler()#是除以最大值的绝对值
X_train_maxabs=max_abs_scaler.fit_transform(X_train)
X_train_maxabs


array([[ 0.5, -1. ,  1. ],
[ 1. ,  0. ,  0. ],
[ 0. ,  1. , -0.5]])


X_test=np.array([[-3.,-1.,4.]])
X_test_maxabs=max_abs_scaler.transform(X_test)
X_test_maxabs


array([[-1.5, -1. ,  2. ]])


max_abs_scaler.scale_


array([ 2.,  1.,  2.])


如果数据包含许多异常值,则使用数据的均值和方差进行缩放可能无法正常工作。在这些情况下,可以使用robust_scale和RobustScaler。他们对数据的中心和范围使用更强大的估计

把数据集中的每个样本所有数值缩放到(-1,1)之间。

X=[[1.,-1.,2.],
[2.,0.,0.],
[0.,1.,-1.]]
X_normalized=preprocessing.normalize(X,norm='l2')#norm l1 l2 默认axis=1,按行
#norm=l2 相当于每个元素除以根号下整行元素的平方和
#norm=l1  X np.abs(X).sum(axis=1)每个元素除以整行元素绝对值的和
#norm=max 每个元素除以整行元素的最大值
X_normalized


array([[ 0.40824829, -0.40824829,  0.81649658],
[ 1.        ,  0.        ,  0.        ],
[ 0.        ,  0.70710678, -0.70710678]])


normalizer=preprocessing.Normalizer().fit(X)#fit does nothing
normalizer


Normalizer(copy=True, norm='l2')


normalizer.transform(X)


array([[ 0.40824829, -0.40824829,  0.81649658],
[ 1.        ,  0.        ,  0.        ],
[ 0.        ,  0.70710678, -0.70710678]])


normalizer.transform([[-1.,1.,0.]])


array([[-0.70710678,  0.70710678,  0.        ]])


(3)二进制化(Binarization)

将数值型数据转化为布尔型的二值数据,可以设置一个阈值(threshold)

X=[[1.,-1.,2.],
[2.,0.,0.],
[0.,1.,-1.]]
binarizer=preprocessing.Binarizer().fit(X)#fit does nothing
binarizer #默认阈值为0.0


Binarizer(copy=True, threshold=0.0)


binarizer.transform(X)


array([[ 1.,  0.,  1.],
[ 1.,  0.,  0.],
[ 0.,  1.,  0.]])


binarizer=preprocessing.Binarizer(threshold=1.1)#设置阈值为1.1
binarizer.transform(X)


array([[ 0.,  0.,  1.],
[ 1.,  0.,  0.],
[ 0.,  0.,  0.]])


(4)标签预处理(Label preprocessing)

4.1)标签二值化(Label binarization)

LabelBinarizer通常用于通过一个多类标签(label)列表,创建一个label指示器矩阵

lb=preprocessing.LabelBinarizer()
lb.fit([1,2,6,4,2])


LabelBinarizer(neg_label=0, pos_label=1, sparse_output=False)


lb.classes_


array([1, 2, 4, 6])


lb.transform([1,6])


array([[1, 0, 0, 0],
[0, 0, 0, 1]])


4.2)标签编码(Label encoding)

le=preprocessing.LabelEncoder()
le.fit([1,2,2,6])


LabelEncoder()


le.classes_


array([1, 2, 6])


le.transform([1,1,2,6])#编码


array([0, 0, 1, 2], dtype=int64)


le.inverse_transform([0,0,1,2])#解码


array([1, 1, 2, 6])


也可以用于非数值类型的标签到数值类型标签的转化:

le=preprocessing.LabelEncoder()le.fit(["paris","paris","tokyo","amsterdam"])


LabelEncoder()


list(le.classes_)


['amsterdam', 'paris', 'tokyo']


le.transform(["tokyo","tokyo","paris"])


array([2, 2, 1], dtype=int64)


le.inverse_transform([0,2,1])


array(['amsterdam', 'tokyo', 'paris'],
dtype='<U9')


(5)编码分类特征 OneHotEncoder

通常特征不是连续值,而是类似于性别:[“male”, “female”], 地区:[“from Europe”, “from US”, “from Asia”], 浏览器:[“uses Firefox”, “uses Chrome”, “uses Safari”, “uses Internet Explorer”]的类别或者字段值。这样的特征可以被有效地编码为整数。

[“male”, “from US”, “uses Internet Explorer”] 被编码为[0, 1, 3]

[“female”, “from Asia”, “uses Chrome”]则为[1, 2, 1]

但是这样的特征不能直接放入机器学习算法中

对于上述的问题,性别的属性是二维的,同理,地区是三维的,浏览器则是思维的,这样,我们可以采用One-Hot编码的方式对上述的样本“[“male”,”US”,”Internet Explorer”]”编码,“male”则对应着[1,0],同理“US”对应着[0,1,0],“Internet Explorer”对应着[0,0,0,1]。则完整的特征数字化的结果为:[1,0,0,1,0,0,0,0,1]。这样导致的一个结果就是数据会变得非常的稀疏。

enc=preprocessing.OneHotEncoder()
enc.fit([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]])


OneHotEncoder(categorical_features='all', dtype=<class 'numpy.float64'>,
handle_unknown='error', n_values='auto', sparse=True)


上面的代码我这样理解的,将矩阵的每一列视为一个特征进行编码,比如第一列是[0 1 0 1],其中有两个不同的元素0和1,所以需要两个维度进行编码,分别编码为[0 1]和[1 0].

类似,第二列元素是[0 1 2 0],共有3个不同的元素,分别是0 1 2,所以需要三个维度进行编码,依次编码为[1 0 0][0 1 0][0 0 1]

第三列元素是[3 0 1 2],共有四个不同的元素,分别是 0 1 2 3,所以需要四个维度进行编码,依次编码为[1 0 0 0][0 1 0 0][0 0 1 0][0 0 0 1]

enc.transform([[1,2,0]]).toarray()


array([[ 0.,  1.,  0.,  0.,  1.,  1.,  0.,  0.,  0.]])


enc = preprocessing.OneHotEncoder(n_values=[2, 3, 4])
# Note that there are missing categorical values for the 2nd and 3rd
# features
enc.fit([[1, 2, 3], [0, 2, 0]])


array([[ 0.,  1.,  1.,  0.,  0.,  1.,  0.,  0.,  0.]])


enc.transform([[1, 0, 0]]).toarray()


array([[ 0.,  1.,  1.,  0.,  0.,  1.,  0.,  0.,  0.]])


(6)估算缺失值

以下代码段演示了如何使用包含缺失值的列(轴0)的平均值来替换编码为np.nan的缺失值:

imp=preprocessing.Imputer(missing_values='NaN',strategy='mean',axis=0)
imp.fit([[1,2],[np.nan,3],[7,6]])


Imputer(axis=0, copy=True, missing_values='NaN', strategy='mean', verbose=0)


X=[[np.nan,2],[6,np.nan],[7,6]]
imp.transform(X)


array([[ 4.        ,  2.        ],
[ 6.        ,  3.66666667],
[ 7.        ,  6.        ]])


Imputer类也支持稀疏矩阵

import scipy.sparse as sp
X=sp.csc_matrix([[1,2],[0,3],[7,6]])
imp=preprocessing.Imputer(missing_values=0,strategy='mean',axis=0)
imp.fit(X)


Imputer(axis=0, copy=True, missing_values=0, strategy='mean', verbose=0)


X_test=sp.csc_matrix([[0,2],[6,0],[7,6]])
imp.transform(X_test)


array([[ 4.        ,  2.        ],
[ 6.        ,  3.66666667],
[ 7.        ,  6.        ]])


(7)生成多项式特征

X=np.arange(6).reshape(3,2)
X


array([[0, 1],
[2, 3],
[4, 5]])


poly=preprocessing.PolynomialFeatures(degree=2)
poly.fit_transform(X)
#X的特征从 (X_1, X_2) 转换为了(1, X_1, X_2, X_1^2, X_1X_2, X_2^2).


array([[  1.,   0.,   1.,   0.,   0.,   1.],
[  1.,   2.,   3.,   4.,   6.,   9.],
[  1.,   4.,   5.,  16.,  20.,  25.]])


使用interaction_only=True,会保留元素之间的交叉项,去掉平方立方项

X = np.arange(9).reshape(3, 3)
X


array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])


poly=preprocessing.PolynomialFeatures(degree=3,interaction_only=True)
poly.fit_transform(X)
#(X_1, X_2, X_3) to (1, X_1, X_2, X_3, X_1X_2, X_1X_3, X_2X_3, X_1X_2X_3).


array([[   1.,    0.,    1.,    2.,    0.,    0.,    2.,    0.],
[   1.,    3.,    4.,    5.,   12.,   15.,   20.,   60.],
[   1.,    6.,    7.,    8.,   42.,   48.,   56.,  336.]])


(8)自定义转换

import numpy as np
from sklearn.preprocessing import FunctionTransformer
transformer = FunctionTransformer(np.log1p)
X = np.array([[0, 1], [2, 3]])
transformer.transform(X)


array([[ 0.        ,  0.69314718],
[ 1.09861229,  1.38629436]])



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐