gpt4 book ai didi

python - 当列缺失值时预处理 Sklearn Imputer

转载 作者:行者123 更新时间:2023-12-01 09:20:08 24 4
gpt4 key购买 nike

我正在尝试使用 Imputer 来查找缺失值。我还想跟踪具有所有缺失值的列,但因为否则我不知道其中哪些(列)已被处理:是否还可以返回包含所有缺失值的列?

Impute Notes

When axis=0, columns which only contained missing values at fit arediscarded upon transform. When axis=1, an exception is raised if thereare rows for which it is not possible to fill in the missing values(e.g., because they only contain missing values).

import pandas as pd
import numpy as np
from sklearn.preprocessing import Imputer
data={'b1':[1,2,3,4,5],'b2':[1,2,4,4,0],'b3':[0,0,0,0,0]}
X= pd.DataFrame(data)
Imp = Imputer(missing_values=0)
print (Imp.fit_transform(X))

print(X)
b1 b2 b3
0 1 1 0
1 2 2 0
2 3 4 0
3 4 4 0
4 5 0 0

runfile
[[ 1. 1. ]
[ 2. 2. ]
[ 3. 4. ]
[ 4. 4. ]
[ 5. 2.75]]

最佳答案

Imputer 中的 statistics_ 属性类将返回每列的填充值,包括删除的列。

statistics_ : array of shape (n_features,)
The imputation fill value for each feature if axis == 0.

Imp.statistics_
array([3. , 2.75, nan])

获取包含所有“缺失”值的列的列名称的示例。

nanmask = np.isnan(Imp.statistics_)

nanmask
array([False, False, True])

X.columns[nanmask]
Index([u'b3'], dtype='object')

关于python - 当列缺失值时预处理 Sklearn Imputer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50854626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com