scikit-learn

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

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

scale函数提供一种便捷的标准化转换操作,如下:

[python] view plain copy
 
  1. >>> from sklearn import preprocessing #导入数据预处理包  
  2. >>> X=[[1.,-1.,2.],  
  3.        [2.,0.,0.],  
  4.        [0.,1.,-1.]]  
  5. >>> X_scaled = preprocessing.scale(X)  
  6. >>> X_scaled  
  7. array([[ 0.        , -1.22474487,  1.33630621],  
  8.        [ 1.22474487,  0.        , -0.26726124],  
  9.        [-1.22474487,  1.22474487, -1.06904497]])  
[python] view plain copy
 
  1. >>> X_scaled.mean(axis=0)  
  2. array([ 0.,  0.,  0.])  
  3. >>> X_scaled.std(axis=0)  
  4. array([ 1.,  1.,  1.])  
同样我们也可以通过preprocessing模块提供的Scaler(StandardScaler 0.15以后版本)工具类来实现这个功能:
[python] view plain copy
 
  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)  
  9. array([[ 0.        , -1.22474487,  1.33630621],  
  10.        [ 1.22474487,  0.        , -0.26726124],  
  11.        [-1.22474487,  1.22474487, -1.06904497]])  
(2)数据规范化(Normalization)
把数据集中的每个样本所有数值缩放到(-1,1)之间。
[python] view plain copy
 
  1. >>> X = [[ 1., -1., 2.],  
  2.      [ 2., 0., 0.],  
  3.      [ 0., 1., -1.]]  
  4. >>> X_normalized = preprocessing.normalize(X, norm='l2')  
  5. >>> X_normalized  
  6. array([[ 0.40824829, -0.40824829,  0.81649658],  
  7.        [ 1.        ,  0.        ,  0.        ],  
  8.        [ 0.        ,  0.70710678, -0.70710678]])  
  9. >>> normalizer = preprocessing.Normalizer().fit(X) # fit does nothing  
  10. >>> normalizer  
  11. Normalizer(copy=True, norm='l2')  
  12. >>> normalizer.transform(X)  
  13. array([[ 0.40824829, -0.40824829,  0.81649658],  
  14.        [ 1.        ,  0.        ,  0.        ],  
  15.        [ 0.        ,  0.70710678, -0.70710678]])  
  16. >>> normalizer.transform([[-1., 1., 0.]])  
  17. array([[-0.70710678,  0.70710678,  0.        ]])  
(3)二进制化(Binarization)
将数值型数据转化为布尔型的二值数据,可以设置一个阈值(threshold)
[python] view plain copy
 
  1. >>> X = [[ 1., -1., 2.],  
  2.      [ 2., 0., 0.],  
  3.      [ 0., 1., -1.]]  
  4. >>> binarizer = preprocessing.Binarizer().fit(X) # fit does nothing  
  5. >>> binarizer  
  6. Binarizer(copy=True, threshold=0.0) # 默认阈值为0.0  
  7. >>> binarizer.transform(X)  
  8. array([[ 1.,  0.,  1.],  
  9.        [ 1.,  0.,  0.],  
  10.        [ 0.,  1.,  0.]])  
  11. >>> binarizer = preprocessing.Binarizer(threshold=1.1) # 设定阈值为1.1  
  12. >>> binarizer.transform(X)  
  13. array([[ 0.,  0.,  1.],  
  14.        [ 1.,  0.,  0.],  
  15.        [ 0.,  0.,  0.]])  

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

4.1)标签二值化(Label binarization)

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

[python] view plain copy
 
  1. >>> lb = preprocessing.LabelBinarizer()  
  2. >>> lb.fit([1, 2, 6, 4, 2])  
  3. LabelBinarizer(neg_label=0, pos_label=1)  
  4. >>> lb.classes_  
  5. array([1, 2, 4, 6])  
  6. >>> lb.transform([1, 6])  
  7. array([[1, 0, 0, 0],  
  8.        [0, 0, 0, 1]])  

上例中每个实例中只有一个标签(label),LabelBinarizer也支持每个实例数据显示多个标签:

[python] view plain copy
 
  1. >>> lb.fit_transform([(1, 2), (3,)]) #(1,2)实例中就包含两个label  
  2. array([[1, 1, 0],  
  3.        [0, 0, 1]])  
  4. >>> lb.classes_  
  5. array([1, 2, 3])  


 

4.2)标签编码(Label encoding)

[python] view plain copy
 
  1. >>> from sklearn import preprocessing  
  2. >>> le = preprocessing.LabelEncoder()  
  3. >>> le.fit([1, 2, 2, 6])  
  4. LabelEncoder()  
  5. >>> le.classes_  
  6. array([1, 2, 6])  
  7. >>> le.transform([1, 1, 2, 6])  
  8. array([0, 0, 1, 2])  
  9. >>> le.inverse_transform([0, 0, 1, 2])  
  10. array([1, 1, 2, 6])  


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

[python] view plain copy
 
  1. >>> le = preprocessing.LabelEncoder()  
  2. >>> le.fit(["paris", "paris", "tokyo", "amsterdam"])  
  3. LabelEncoder()  
  4. >>> list(le.classes_)  
  5. ['amsterdam', 'paris', 'tokyo']  
  6. >>> le.transform(["tokyo", "tokyo", "paris"])  
  7. array([2, 2, 1])  
  8. >>> list(le.inverse_transform([2, 2, 1]))  
  9. ['tokyo', 'tokyo', 'paris']  
 
 
原文地址:https://www.cnblogs.com/changbosha/p/5797664.html