網(wǎng)站首頁 編程語言 正文
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: 默認為 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,要轉化為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"替換字符串或對象數(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將疊加到輸入器轉換的輸出上。這樣即使進行了imputation歸算,也同樣會讓預測估算器描述缺失值。如果某個特征在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()
總結
原文鏈接:https://blog.csdn.net/dss_dssssd/article/details/82831240
相關推薦
- 2022-06-08 Spring Cloud Openfeign分析
- 2022-03-11 C++實操之內(nèi)聯(lián)成員函數(shù)介紹_C 語言
- 2024-03-24 go 連接redis集群
- 2021-12-05 Linux系統(tǒng)運行級別詳細介紹_Linux
- 2022-08-18 golang?select?機制和超時問題_Golang
- 2023-01-17 如何使用python中的networkx來生成一個圖_python
- 2022-11-07 SwiftUI?引導頁界面實現(xiàn)示例_Swift
- 2022-05-01 python中NumPy的安裝與基本操作_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支