填补缺失值:sklearn.preprocessing.Imputer(missing_values=’NaN’, strategy=’mean’, axis=0, verbose=0, copy=True) 主要参数说明: missing_values:缺失值,可以为整数或NaN(缺失值numpy.nan用字符串‘NaN’表示),默认为NaN
strategy:替换策略,字符串,默认用均值‘mean’替换
①若为mean时,用特征列的均值替换 ②若为median时,用特征列的中位数替换 ③若为most_frequent时,用特征列的众数替换 axis:指定轴数,默认axis=0代表列,axis=1代表行
copy:设置为True代表不在原数据集上修改,设置为False时,就地修改,存在如下情况时,即使设置为False时,也不会就地修改
①X不是浮点值数组 ②X是稀疏且missing_values=0 ③axis=0且X为CRS矩阵 ④axis=1且X为CSC矩阵 statistics_属性:axis设置为0时,每个特征的填充值数组,axis=1时,报没有该属性错误
以X为数组为例: - In [1]: import numpy as np
...: from sklearn.preprocessing import Imputer
...: train_X = np.array([[1, 2], [np.nan, 3], [7, 6]])
...: imp = Imputer(missing_values=np.nan , strategy='mean', axis=0)
...: imp.fit(train_X)
...:
Out[1]: Imputer(axis=0, copy=True, missing_values=nan, strategy='mean', verbose=
0)
In [2]: imp.statistics_
Out[2]: array([ 4. , 3.66666667])
In [3]: test_X = np.array([[np.nan, 2], [6, np.nan], [7, 6]])
...: imp.transform(test_X)
...:
Out[3]:
array([[ 4. , 2. ],
[ 6. , 3.66666667],
[ 7. , 6. ]])
In [4]: imp.fit_transform(test_X)
Out[4]:
array([[ 6.5, 2. ],
[ 6. , 4. ],
[ 7. , 6. ]])
In [5]: imp.statistics_
Out[5]: array([ 6.5, 4. ])
以X为稀疏矩阵为例:
In [6]: import scipy.sparse as sp
...: from sklearn.preprocessing import Imputer
...: X = sp.csc_matrix([[1, 2], [0, 3], [7, 6]])
...: imp = Imputer(missing_values = 0 , strategy = 'mean',axis=0)
...: imp.fit(X)
...: test_X = sp.csc_matrix([[0, 2], [6, 0], [7, 6]])
...: imp.transform(test_X)
...:
Out[6]:
array([[ 4. , 2. ],
[ 6. , 3.66666667],
[ 7. , 6. ]])
In [7]: imp.statistics_
Out[7]: array([ 4. , 3.66666667])
|