欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

slearn缺失值處理器之Imputer詳析

 更新時間:2022年08月16日 09:15:23   作者:墨氳  
這篇文章主要給大家介紹了關(guān)于slearn缺失值處理器之Imputer的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

class sklearn.preprocessing.Imputer(missing_values=’NaN’, strategy=’mean’, axis=0, verbose=0, copy=True)

參數(shù):

  • 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.使用眾數(shù)代替,也就是出現(xiàn)次數(shù)最多的數(shù)
  • axis: 默認為 axis=0
    • axis = 0, 按列處理
    • aixs =1 , 按行處理

說實話,我還是沒太弄明白aixs的具體含義,總感覺在不同的函數(shù)中有不同的含義。。還是使用前查找一下官方文檔吧,畢竟大多數(shù)時候處理的都是2維數(shù)組,文檔中的參數(shù)很容易理解。

注意:

  • Imputer 只接受DataFrame類型
  • Dataframe 中必須全部為數(shù)值屬性

所以在處理的時候注意,要進行適當處理

數(shù)值屬性的列較少,可以將數(shù)值屬性的列取出來 單獨取出來

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. 創(chuàng)建Imputer器
imp =Imputer(missing_values="NaN", strategy="mean",axis=0 )
# 先只將處理price列的數(shù)據(jù), 注意使用的是   df[['price']]   這樣返回的是一個DataFrame類型的數(shù)據(jù)?。。?!
# 2. 使用fit_transform()函數(shù)即可完成缺失值填充了
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
'''

數(shù)值屬性的列較多,相反文本或分類屬性(text and category attribute)較少,可以先刪除文本屬性,處理完以后再合并

from sklearn.preprocessing import Imputer
# 1.創(chuàng)建Iimputer
imputer = Imputer(strategy="median")
# 只有一個文本屬性,故先去掉
housing_num = housing.drop("ocean_proximity", axis=1)
# 2. 使用fit_transform函數(shù)
X = imputer.fit_transform(housing_num)
# 返回的是一個numpyarray,要轉(zhuǎn)化為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類代替?;蛘呓导壔匕姹緎klearn 0.19

from sklearn.impute import SimpleImputer
#有如下的一些參數(shù)
sklearn.impute.SimpleImputer(
		missing_values=nan,
		strategy='mean',
		fill_value=None,
		verbose=0,
		copy=True,
		add_indicator=False
)[source]
imputer = SimpleImputer(missing_values=NA, strategy = "mean")

用上面那個代碼就可以實現(xiàn)imputer的功能。其他的參數(shù)詳解如下,具體的話大家去查閱sklearn庫的說明。

  • misssing_values: number,string,np.nan(default) or None
    缺失值的占位符,所有出現(xiàn)的占位符都將被計算
  • strategy: string,default=‘mean’ 計算并替換的策略:
    "mean,使用該列的平均值替換缺失值。僅用于數(shù)值數(shù)據(jù); “median”,使用該列的中位數(shù)替換缺失值。僅用于數(shù)值數(shù)據(jù);
    “most_frequent”,使用每個列中最常見的值替換缺失值??捎糜诜菙?shù)值數(shù)據(jù);
    “constant”,用fill_value替換缺失值。可用于非數(shù)值數(shù)據(jù)。
  • fill_value: string or numerical value,default=None
    當strategy為"constant",使用fil_value替換missing_values。如果是default,使用0替換數(shù)值數(shù)據(jù),使用"missing_value"替換字符串或?qū)ο髷?shù)據(jù)類型
  • verbose: integer,default=0
  • copy: boolean,default=True
  • True: 將創(chuàng)建X的副本;False: 只要有可能,就會原地替換。注意,一下情況即使copy=False,也會創(chuàng)建新的副本:
  • add_indicator: boolean,default=False
    True,則MissingIndicator將疊加到輸入器轉(zhuǎn)換的輸出上。這樣即使進行了imputation歸算,也同樣會讓預(yù)測估算器描述缺失值。如果某個特征在fit/train時沒有缺失值,那么即使在transform/tes時有缺失值,該特征也不會出現(xiàn)在缺失的指示器上。

隨著版本的更新,Imputer的輸入方式也發(fā)生了變化,一開始的輸入方式為

from sklearn.preprocessing import Imputer
imputer = Imputer(strategy='median')

現(xiàn)在需要對上面輸入進行更新,輸入變?yōu)?/p>

from sklearn.impute import SimpleImputer
imputer = SimpleImputer(strategy="median")

簡單使用:

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()

總結(jié)

到此這篇關(guān)于slearn缺失值處理器之Imputer的文章就介紹到這了,更多相關(guān)slearn缺失值處理器Imputer內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論