数据预处理
一、缺失值处理
均值填充、中位数填充、众数填充、常数填充
import pandas as pd
import numpy as np
c = np.array([[1,2,3,4],[4,5,6,np.nan],[5,6,7,8],[9,4,np.nan,8]])
C=pd.DataFrame(c)
C
|
0 |
1 |
2 |
3 |
0 |
1.0 |
2.0 |
3.0 |
4.0 |
1 |
4.0 |
5.0 |
6.0 |
NaN |
2 |
5.0 |
6.0 |
7.0 |
8.0 |
3 |
9.0 |
4.0 |
NaN |
8.0 |
(1)导入数据预处理中填充模块SimpleImputer
from sklearn.impute import SimpleImputer
(2)利用Imputer 创建填充对象imp_mean
imp_mean = SimpleImputer(missing_values=np.nan,strategy='mean',verbose=0)
参数说明:
参数 含义&输入
missing_values 告诉SimpleImputer,数据中的缺失值长什么样,默认空值np.nan
strategy 我们填补缺失值的策略,默认均值。 输入“mean”使用均值填补(仅对数值型特征可用) 输入“median"用中值填补(仅对数值型特征 可用) 输入"most_frequent”用众数填补(对数值型和字符型特征都可用) 输入“constant"表示请参考参数“fill_value"中的值 (对数值型和字符型特征都可用)
fill_value 当参数startegy为”constant"的时候可用,可输入字符串或数字表示要填充的值,常用0
copy 默认为True,将创建特征矩阵的副本,反之则会将缺失值填补到原本的特征矩阵中去。
imp_mean=imp_mean.fit_transform(C) #fit_transform一步完成调取结果
imp_mean
array([[1. , 2. , 3. , 4. ],
[4. , 5. , 6. , 6.66666667],
[5. , 6. , 7. , 8. ],
[9. , 4. , 5.33333333, 8. ]])
pd.DataFrame(imp_mean)
|
0 |
1 |
2 |
3 |
0 |
1.0 |
2.0 |
3.000000 |
4.000000 |
1 |
4.0 |
5.0 |
6.000000 |
6.666667 |
2 |
5.0 |
6.0 |
7.000000 |
8.000000 |
3 |
9.0 |
4.0 |
5.333333 |
8.000000 |
1、均值填充
指定列 处理空值
#指定列处理空值
import pandas as pd
import numpy as np
from sklearn.impute import SimpleImputer
#生成一个有空值的数组
c = np.array([[1,2,3,4],[4,5,6,np.nan],[5,6,7,8],[9,4,np.nan,8]])
C=pd.DataFrame(c)
#取的要处理的列
age = C[2]
#age=C[2].values.reshape(-1,1) #取出缺失值所在列的数值,sklearn当中特征矩阵必须是二维才能传入 使用reshape(-1,1)升维
#升维
age = pd.DataFrame(age)
#利用Imputer 创建填充对象imp_mean
imp_mean = SimpleImputer(missing_values=np.nan,strategy='mean',verbose=0)
#fit_transform一步完成调取结果
imp_mean = imp_mean.fit_transform(age)
#将值赋值给原数组
C[2]=imp_mean
C
|
0 |
1 |
2 |
3 |
0 |
1.0 |
2.0 |
3.000000 |
4.0 |
1 |
4.0 |
5.0 |
6.000000 |
NaN |
2 |
5.0 |
6.0 |
7.000000 |
8.0 |
3 |
9.0 |
4.0 |
5.333333 |
8.0 |
处理整个数组的全部列
#处理整个数组的全部列
import pandas as pd
import numpy as np
from sklearn.impute import SimpleImputer
#生成一个有空值的数组
c = np.array([[1,2,3,4],[4,5,6,np.nan],[5,6,7,8],[9,4,np.nan,8]])
C=pd.DataFrame(c)
#利用Imputer 创建填充对象imp_mean
imp_mean = SimpleImputer(missing_values=np.nan,strategy='mean',verbose=0)
#fit_transform一步完成调取结果
imp_mean = imp_mean.fit_transform(C)
pd.DataFrame(imp_mean)
#检验是否还有空值,为0即说明空值均已被填充
#pd.DataFrame(imp_mean).isnull().sum()
|
0 |
1 |
2 |
3 |
0 |
1.0 |
2.0 |
3.000000 |
4.000000 |
1 |
4.0 |
5.0 |
6.000000 |
6.666667 |
2 |
5.0 |
6.0 |
7.000000 |
8.000000 |
3 |
9.0 |
4.0 |
5.333333 |
8.000000 |
2、中位数填充
#处理整个数组的全部列
import pandas as pd
import numpy as np
from sklearn.impute import SimpleImputer
#生成一个有空值的数组
c = np.array([[1,2,3,4],[4,5,6,np.nan],[5,6,7,8],[9,4,np.nan,8]])
C=pd.DataFrame(c)
#利用Imputer 创建填充对象imp_mean
imp_median = SimpleImputer(missing_values=np.nan,strategy='median',verbose=0)
#fit_transform一步完成调取结果
imp_median = imp_median.fit_transform(C)
pd.DataFrame(imp_median)
#检验是否还有空值,为0即说明空值均已被填充
#pd.DataFrame(imp_median).isnull().sum()
|
0 |
1 |
2 |
3 |
0 |
1.0 |
2.0 |
3.0 |
4.0 |
1 |
4.0 |
5.0 |
6.0 |
8.0 |
2 |
5.0 |
6.0 |
7.0 |
8.0 |
3 |
9.0 |
4.0 |
6.0 |
8.0 |
3、众数填充
#处理整个数组的全部列
import pandas as pd
import numpy as np
from sklearn.impute import SimpleImputer
#生成一个有空值的数组
c = np.array([[1,2,3,4],[4,5,6,np.nan],[5,6,7,8],[9,4,np.nan,8]])
C=pd.DataFrame(c)
#利用Imputer 创建填充对象imp_mean
imp_most_frequent = SimpleImputer(missing_values=np.nan,strategy='most_frequent',verbose=0)
#fit_transform一步完成调取结果
imp_most_frequent = imp_most_frequent.fit_transform(C)
pd.DataFrame(imp_most_frequent)
#检验是否还有空值,为0即说明空值均已被填充
#pd.DataFrame(most_frequent).isnull().sum()
|
0 |
1 |
2 |
3 |
0 |
1.0 |
2.0 |
3.0 |
4.0 |
1 |
4.0 |
5.0 |
6.0 |
8.0 |
2 |
5.0 |
6.0 |
7.0 |
8.0 |
3 |
9.0 |
4.0 |
3.0 |
8.0 |
4、常数填充
#处理整个数组的全部列
import pandas as pd
import numpy as np
from sklearn.impute import SimpleImputer
#生成一个有空值的数组
c = np.array([[1,2,3,4],[4,5,6,np.nan],[5,6,7,8],[9,4,np.nan,8]])
C=pd.DataFrame(c)
#利用Imputer 创建填充对象imp_mean,
#实例化,填充常数0,填充常数需strategy与fill_value一同使用
imp_0 = SimpleImputer(missing_values=np.nan,strategy='constant',fill_value=0)
#fit_transform一步完成调取结果
imp_0 = imp_0.fit_transform(C)
pd.DataFrame(imp_0)
#检验是否还有空值,为0即说明空值均已被填充
#pd.DataFrame(imp_0).isnull().sum()
|
0 |
1 |
2 |
3 |
0 |
1.0 |
2.0 |
3.0 |
4.0 |
1 |
4.0 |
5.0 |
6.0 |
0.0 |
2 |
5.0 |
6.0 |
7.0 |
8.0 |
3 |
9.0 |
4.0 |
0.0 |
8.0 |