slearn缺失值處理器之Imputer詳析
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ù)
- The imputation strategy.
- axis: 默認(rèn)為 axis=0
- axis = 0, 按列處理
- aixs =1 , 按行處理
說實(shí)話,我還是沒太弄明白aixs的具體含義,總感覺在不同的函數(shù)中有不同的含義。。還是使用前查找一下官方文檔吧,畢竟大多數(shù)時(shí)候處理的都是2維數(shù)組,文檔中的參數(shù)很容易理解。
注意:
- Imputer 只接受DataFrame類型
- Dataframe 中必須全部為數(shù)值屬性
所以在處理的時(shí)候注意,要進(jìn)行適當(dāng)處理
數(shù)值屬性的列較少,可以將數(shù)值屬性的列取出來 單獨(dú)取出來
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']] 這樣返回的是一個(gè)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") # 只有一個(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: ''' 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 '''
補(bǔ)充: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")
用上面那個(gè)代碼就可以實(shí)現(xiàn)imputer的功能。其他的參數(shù)詳解如下,具體的話大家去查閱sklearn庫的說明。
- misssing_values: number,string,np.nan(default) or None
缺失值的占位符,所有出現(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: string or numerical value,default=None
當(dāng)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)換的輸出上。這樣即使進(jìn)行了imputation歸算,也同樣會讓預(yù)測估算器描述缺失值。如果某個(gè)特征在fit/train時(shí)沒有缺失值,那么即使在transform/tes時(shí)有缺失值,該特征也不會出現(xiàn)在缺失的指示器上。
隨著版本的更新,Imputer的輸入方式也發(fā)生了變化,一開始的輸入方式為
from sklearn.preprocessing import Imputer imputer = Imputer(strategy='median')
現(xiàn)在需要對上面輸入進(jì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)文章
python socket多線程實(shí)現(xiàn)客戶端與服務(wù)器連接
這篇文章主要為大家詳細(xì)介紹了python socket多線程實(shí)現(xiàn)客戶端與服務(wù)器連接,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Python 列表(List)的底層實(shí)現(xiàn)原理分析
這篇文章主要介紹了Python 列表(List)的底層實(shí)現(xiàn)原理分析,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03python 獲取utc時(shí)間轉(zhuǎn)化為本地時(shí)間的方法
今天小編就為大家分享一篇python 獲取utc時(shí)間轉(zhuǎn)化為本地時(shí)間的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12python3調(diào)用百度翻譯API實(shí)現(xiàn)實(shí)時(shí)翻譯
這篇文章主要為大家詳細(xì)介紹了python3調(diào)用百度翻譯API,實(shí)現(xiàn)實(shí)時(shí)翻譯,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08使用python 計(jì)算百分位數(shù)實(shí)現(xiàn)數(shù)據(jù)分箱代碼
這篇文章主要介紹了使用python 計(jì)算百分位數(shù)實(shí)現(xiàn)數(shù)據(jù)分箱代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03OpenCV MediaPipe實(shí)現(xiàn)顏值打分功能
這篇文章主要介紹了通過OpenCV MediaPipe實(shí)現(xiàn)攝像頭實(shí)時(shí)檢測顏值打分功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Python有一定的幫助,感興趣的可以了解一下2021-12-12基于django和dropzone.js實(shí)現(xiàn)上傳文件
這篇文章主要介紹了基于django和dropzone.js實(shí)現(xiàn)上傳文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11python獲得文件創(chuàng)建時(shí)間和修改時(shí)間的方法
這篇文章主要介紹了python獲得文件創(chuàng)建時(shí)間和修改時(shí)間的方法,涉及Python針對文件屬性的相關(guān)操作技巧,需要的朋友可以參考下2015-06-06教你用一行Python代碼實(shí)現(xiàn)GUI圖形界面
這篇文章主要介紹了教你用一行Python代碼實(shí)現(xiàn)GUI圖形界面,通過使用PySimpleGUI的popup_get_folder()方法,一行代碼就能實(shí)現(xiàn)選擇文件夾的操作,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01