错误类型:无法对输入类型支持ufunc函数“isnan”,使用Imputer处理NaN值

2024-04-28 09:12:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我是学Python和熊猫的新手。我正在尝试预处理一个大数据帧,它由数值和分类特征组成,在一些列中有NaN值。 首先,我尝试得到特征矩阵,然后使用插补器为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中的Nan值(索引号=2)更改为列的平均值

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''    

第一:我的方法正确吗? 第二:如何处理错误?

谢谢


Tags: the数据特征nanregrl平均值features
3条回答

注意Nan和Nan之间的区别您已经使用了Nan

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

将“Nan”替换为“Nan”,则不会出现此错误

我猜由于字符串“Nan”,您的LotFrontage列数据存储为对象数据类型。请使用此类型查找。它很可能会给出object/string。

print(reduced_data.LotFrontage.values.dtype)

输入器只在浮子上工作。

第一次进近:

您可以执行以下操作: 1) 将列类型转换为浮点 2) 柱面平均数 3) 使用pandas dataframe函数fillna在dataframe中填充nan。

reduced_data.LotFrontage = pd.to_numeric(reduced_data.LotFrontage, errors='coerce')
m = reduced_data.LotFrontage.mean(skipna=True)
reduced_data.fillna(m)

以上代码将在数据帧中填充任何存在nan的地方。

第二种方法:

reduced_data.LotFrontage = pd.to_numeric(reduced_data.LotFrontage, errors='coerce')
imputer = Imputer()
features = reduced_data.iloc[:,:-1].values
imputer.fit(features[:,2])

试试这个这是一个工作代码的例子

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])

相关问题 更多 >