class sklearn.preprocessing.Imputer(missing_values=’NaN’, strategy=’mean’, axis=0, verbose=0, copy=True)
参数:
- missing_values: integer or “NaN”, optional (default=”NaN”)
-
strategy : string, optional (default=”mean”)
-
The imputation strategy.
- If “mean”, then replace missing values using the mean along the axis. 使用平均值代替
- If “median”, then replace missing values using the median along the axis.使用中值代替
- If “most_frequent”, then replace missing using the most frequent value along the axis.使用众数代替,也就是出现次数最多的数
-
The imputation strategy.
-
axis: 默认为 axis=0
- axis = 0, 按列处理
- aixs =1 , 按行处理
说实话,我还是没太弄明白aixs的具体含义,总感觉在不同的函数中有不同的含义。。还是使用前查找一下官方文档吧,毕竟大多数时候处理的都是2维数组,文档中的参数很容易理解。
注意:
- Imputer 只接受DataFrame类型
- Dataframe 中必须全部为数值属性
所以在处理的时候注意,要进行适当处理
数值属性的列较少,可以将数值属性的列取出来 单独取出来
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
import pandas as pd import numpy as np df = pd.DataFrame([[ "XXL" , 8 , "black" , "class 1" , 22 ], [ "L" , np.nan, "gray" , "class 2" , 20 ], [ "XL" , 10 , "blue" , "class 2" , 19 ], [ "M" , np.nan, "orange" , "class 1" , 17 ], [ "M" , 11 , "green" , "class 3" , np.nan], [ "M" , 7 , "red" , "class 1" , 22 ]]) df.columns = [ "size" , "price" , "color" , "class" , "boh" ] print (df) # out: ''' size price color class boh 0 XXL 8.0 black class 1 22.0 1 L NaN gray class 2 20.0 2 XL 10.0 blue class 2 19.0 3 M NaN orange class 1 17.0 4 M 11.0 green class 3 NaN 5 M 7.0 red class 1 22.0 ''' from sklearn.preprocessing import Imputer # 1. 创建Imputer器 imp = Imputer(missing_values = "NaN" , strategy = "mean" ,axis = 0 ) # 先只将处理price列的数据, 注意使用的是 df[['price']] 这样返回的是一个DataFrame类型的数据!!!! # 2. 使用fit_transform()函数即可完成缺失值填充了 df[ "price" ] = imp.fit_transform(df[[ "price" ]]) df # out: ''' size price color class boh 0 XXL 8.0 black class 1 22.0 1 L 9.0 gray class 2 20.0 2 XL 10.0 blue class 2 19.0 3 M 9.0 orange class 1 17.0 4 M 11.0 green class 3 NaN 5 M 7.0 red class 1 22.0 ''' # 直接处理price和boh两列 df[[ 'price' , 'boh' ]] = imp.fit_transform(df[[ 'price' , 'boh' ]]) df # out: ''' size price color class boh 0 XXL 8.0 black class 1 22.0 1 L 9.0 gray class 2 20.0 2 XL 10.0 blue class 2 19.0 3 M 9.0 orange class 1 17.0 4 M 11.0 green class 3 20.0 5 M 7.0 red class 1 22.0 ''' |
数值属性的列较多,相反文本或分类属性(text and category attribute)较少,可以先删除文本属性,处理完以后再合并
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
from sklearn.preprocessing import Imputer # 1.创建Iimputer imputer = Imputer(strategy = "median" ) # 只有一个文本属性,故先去掉 housing_num = housing.drop( "ocean_proximity" , axis = 1 ) # 2. 使用fit_transform函数 X = imputer.fit_transform(housing_num) # 返回的是一个numpyarray,要转化为DataFrame housing_tr = pd.DataFrame(X, columns = housing_num.columns) # 将文本属性值添加 housing_tr[ 'ocean_proximity' ] = housing[ "ocean_proximity" ] housing_tr[: 2 ] # out: ''' longitude latitude housing_median_age total_rooms total_bedrooms population households median_income 0 -121.89 37.29 38.0 1568.0 351.0 710.0 339.0 2.7042 1 -121.93 37.05 14.0 679.0 108.0 306.0 113.0 6.4214 ''' |
补充:sklearn中的Imputer模块改动
在sklearn的0.22以上版本的sklearn去除了Imputer类,我们可以使用SimpleImputer类代替。或者降级回版本sklearn 0.19
1
2
3
4
5
6
7
8
9
10
|
from sklearn.impute import SimpleImputer #有如下的一些参数 sklearn.impute.SimpleImputer( missing_values = nan, strategy = 'mean' , fill_value = None , verbose = 0 , copy = True , add_indicator = False )[source] |
1
|
imputer = SimpleImputer(missing_values = NA, strategy = "mean" ) |
用上面那个代码就可以实现imputer的功能。其他的参数详解如下,具体的话大家去查阅sklearn库的说明。
-
misssing_values: number,string,np.nan(default) or None
缺失值的占位符,所有出现的占位符都将被计算 -
strategy: string,default=‘mean’ 计算并替换的策略:
"mean,使用该列的平均值替换缺失值。仅用于数值数据; “median”,使用该列的中位数替换缺失值。仅用于数值数据;
“most_frequent”,使用每个列中最常见的值替换缺失值。可用于非数值数据;
“constant”,用fill_value替换缺失值。可用于非数值数据。 -
fill_value: string or numerical value,default=None
当strategy为"constant",使用fil_value替换missing_values。如果是default,使用0替换数值数据,使用"missing_value"替换字符串或对象数据类型 - verbose: integer,default=0
- copy: boolean,default=True
- True: 将创建X的副本;False: 只要有可能,就会原地替换。注意,一下情况即使copy=False,也会创建新的副本:
-
add_indicator: boolean,default=False
True,则MissingIndicator将叠加到输入器转换的输出上。这样即使进行了imputation归算,也同样会让预测估算器描述缺失值。如果某个特征在fit/train时没有缺失值,那么即使在transform/tes时有缺失值,该特征也不会出现在缺失的指示器上。
随着版本的更新,Imputer的输入方式也发生了变化,一开始的输入方式为
1
2
|
from sklearn.preprocessing import Imputer imputer = Imputer(strategy = 'median' ) |
现在需要对上面输入进行更新,输入变为
1
2
|
from sklearn.impute import SimpleImputer imputer = SimpleImputer(strategy = "median" ) |
简单使用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from sklearn.impute import SimpleImputer import numpy as np def im(): """ 缺失值处理 :return: None """ im1 = SimpleImputer(missing_values = np.nan, strategy = 'mean' ) data = im1.fit_transform([[ 1 , 2 ], [np.nan, 3 ], [ 7 , 6 ]]) print (data) return None if __name__ = = "__main__" : im() |
总结
到此这篇关于slearn缺失值处理器之Imputer的文章就介绍到这了,更多相关slearn缺失值处理器Imputer内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/dss_dssssd/article/details/82831240