gpt4 book ai didi

python - TypeError : ufunc 'isnan' not supported for the input types, 对 NaN 值使用 Imputer

转载 作者:行者123 更新时间:2023-12-01 02:06:27 33 4
gpt4 key购买 nike

我是 python 和 pandas 的新手。我正在尝试预处理一个由数字和分类特征组成的大数据框,并且在某些列中存在 NaN 值。首先,我尝试获取特征矩阵,然后使用 Imputer 放置 Nan 值的平均值或中值。

这是数据框

    MSSubClass MSZoning  LotFrontage  LotArea Street LotShape LandContour  \
0 60 RL 65.0 8450 Pave Reg Lvl
1 20 RL 80.0 9600 Pave Reg Lvl
2 60 RL 68.0 11250 Pave IR1 Lvl
3 70 RL 60.0 9550 Pave IR1 Lvl
4 60 RL 84.0 14260 Pave IR1 Lvl
5 50 RL 85.0 14115 Pave IR1 Lvl
6 20 RL 75.0 10084 Pave Reg Lvl
7 60 RL NaN 10382 Pave IR1 Lvl
8 50 RM 51.0 6120 Pave Reg Lvl
9 190 RL 50.0 7420 Pave Reg Lvl
10 20 RL 70.0 11200 Pave Reg Lvl
11 60 RL 85.0 11924 Pave IR1 Lvl

代码:只是将LotFrontage(索引号= 2)中的Nan值更改为列的平均值

imputer = Imputer(missing_values='Nan',strategy="mean",axis=0)
features = reduced_data.iloc[:,:-1].values
imputer.fit(features[:,2])

当我运行这个时,出现错误:

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''    

首先:我的方法正确吗?第二:出现错误如何处理?

谢谢

最佳答案

试试这个,这是工作代码的示例

from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = np.nan, strategy = 'mean', axis =0)
imputer = imputer.fit(X[:,1:3])
X[:,1:3] = imputer.transform(X[:,1:3])

关于python - TypeError : ufunc 'isnan' not supported for the input types, 对 NaN 值使用 Imputer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49010285/

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