slearn缺失值處理器之Imputer詳析_第1頁
slearn缺失值處理器之Imputer詳析_第2頁
slearn缺失值處理器之Imputer詳析_第3頁
slearn缺失值處理器之Imputer詳析_第4頁
slearn缺失值處理器之Imputer詳析_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第slearn缺失值處理器之Imputer詳析目錄參數(shù):注意:所以在處理的時(shí)候注意,要進(jìn)行適當(dāng)處理補(bǔ)充:sklearn中的Imputer模塊改動(dòng)總結(jié)classsklearn.preprocessing.Imputer(missing_values=NaN,strategy=mean,axis=0,verbose=0,copy=True)

參數(shù):

missing_values:integerorNaN,optional(default=NaN)strategy:string,optional(default=mean)Theimputationstrategy.Ifmean,thenreplacemissingvaluesusingthemeanalongtheaxis.使用平均值代替Ifmedian,thenreplacemissingvaluesusingthemedianalongtheaxis.使用中值代替Ifmost_frequent,thenreplacemissingusingthemostfrequentvaluealongtheaxis.使用眾數(shù)代替,也就是出現(xiàn)次數(shù)最多的數(shù)axis:默認(rèn)為axis=0axis=0,按列處理aixs=1,按行處理

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

注意:

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

所以在處理的時(shí)候注意,要進(jìn)行適當(dāng)處理

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

importpandasaspd

importnumpyasnp

df=pd.DataFrame([["XXL",8,"black","class1",22],

["L",np.nan,"gray","class2",20],

["XL",10,"blue","class2",19],

["M",np.nan,"orange","class1",17],

["M",11,"green","class3",np.nan],

["M",7,"red","class1",22]])

df.columns=["size","price","color","class","boh"]

print(df)

#out:

sizepricecolorclassboh

0XXL8.0blackclass122.0

1LNaNgrayclass220.0

2XL10.0blueclass219.0

3MNaNorangeclass117.0

4M11.0greenclass3NaN

5M7.0redclass122.0

fromsklearn.preprocessingimportImputer

#1.創(chuàng)建Imputer器

imp=Imputer(missing_values="NaN",strategy="mean",axis=0)

#先只將處理price列的數(shù)據(jù),注意使用的是df[['price']]這樣返回的是一個(gè)DataFrame類型的數(shù)據(jù)!?。?!

#2.使用fit_transform()函數(shù)即可完成缺失值填充了

df["price"]=imp.fit_transform(df[["price"]])

#out:

sizepricecolorclassboh

0XXL8.0blackclass122.0

1L9.0grayclass220.0

2XL10.0blueclass219.0

3M9.0orangeclass117.0

4M11.0greenclass3NaN

5M7.0redclass122.0

#直接處理price和boh兩列

df[['price','boh']]=imp.fit_transform(df[['price','boh']])

#out:

sizepricecolorclassboh

0XXL8.0blackclass122.0

1L9.0grayclass220.0

2XL10.0blueclass219.0

3M9.0orangeclass117.0

4M11.0greenclass320.0

5M7.0redclass122.0

'''

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

fromsklearn.preprocessingimportImputer

#1.創(chuàng)建Iimputer

imputer=Imputer(strategy="median")

#只有一個(gè)文本屬性,故先去掉

housing_num=housing.drop("ocean_proximity",axis=1)

#2.使用fit_transform函數(shù)

X=imputer.fit_transform(housing_num)

#返回的是一個(gè)numpyarray,要轉(zhuǎn)化為DataFrame

housing_tr=pd.DataFrame(X,columns=housing_num.columns)

#將文本屬性值添加

housing_tr['ocean_proximity']=housing["ocean_proximity"]

housing_tr[:2]

#out:

longitudelatitudehousing_median_agetotal_roomstotal_bedroomspopulationhouseholdsmedian_income

0-121.8937.2938.01568.0351.0710.0339.02.7042

1-121.9337.0514.0679.0108.0306.0113.06.4214

'''

補(bǔ)充:sklearn中的Imputer模塊改動(dòng)

在sklearn的0.22以上版本的sklearn去除了Imputer類,我們可以使用SimpleImputer類代替?;蛘呓导?jí)回版本sklearn0.19

fromsklearn.imputeimportSimpleImputer

#有如下的一些參數(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")

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

misssing_values:number,string,np.nan(default)orNone

缺失值的占位符,所有出現(xiàn)的占位符都將被計(jì)算strategy:string,default=mean計(jì)算并替換的策略:

mean,使用該列的平均值替換缺失值。僅用于數(shù)值數(shù)據(jù);median,使用該列的中位數(shù)替換缺失值。僅用于數(shù)值數(shù)據(jù);

most_frequent,使用每個(gè)列中最常見的值替換缺失值。可用于非數(shù)值數(shù)據(jù);

constant,用fill_value替換缺失值??捎糜诜菙?shù)值數(shù)據(jù)。fill_value:stringornumericalvalue,default=None

當(dāng)strategy為constant,使用fil_value替換missing_values。如果是default,使用0替換數(shù)值數(shù)據(jù),使用missing_value替換字符串或?qū)ο髷?shù)據(jù)類型verbose:integer,default=0copy:boolean,default=TrueTrue:將創(chuàng)建X的副本;False:只要有可能,就會(huì)原地替換。注意,一下情況即使copy=False,也會(huì)創(chuàng)建新的副本:add_indicator:boolean,default=False

True,則MissingIndicator將疊加到輸入器轉(zhuǎn)換的輸出上。這樣即使進(jìn)行了imputation歸算,也同樣會(huì)讓預(yù)測(cè)估算器描述缺失值。如果某個(gè)特征在fit/train時(shí)沒有缺失值,那么即使在transform/tes時(shí)有缺失值,該特征也不會(huì)出現(xiàn)在缺失的指示器上。

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

fromsklearn.preprocessingimportImputer

imputer=Imputer(strategy='median')

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

fromsklearn.imputeimportSimpleImputer

imputer=SimpleImputer(strategy="median")

簡(jiǎn)單使用:

fromsklearn.imputeimportSimpleImputer

importnumpyasnp

defim

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論