【问题标题】:TypeError: ufunc 'isnan' not supported for the input types, Using Imputer for NaN valuesTypeError:输入类型不支持 ufunc 'isnan',将 Imputer 用于 NaN 值
【发布时间】:2020-08-05 21:34:38
【问题描述】:

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

这是数据框

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

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

谢谢

【问题讨论】:

    标签: python nan


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      注意你用过Nan

      的Nan和NaN的区别(注意最后的大写N)
        imputer = Imputer(missing_values='NaN',strategy="mean",axis=0)
      

      将 'Nan' 替换为 'NaN' 不会出现此错误

      【讨论】:

        【解决方案3】:

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

        print(reduced_data.LotFrontage.values.dtype)
        

        Imputer 仅适用于浮点数。

        第一种方法:

        您可以执行以下操作: 1) 将列类型转换为浮点数 2)找出LotFrontage列的平均值 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 的地方填充 Dataframe。

        第二种方法:

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

        【讨论】:

          【解决方案4】:

          在 missing_value 参数中使用 'NaN' 而不是 'Nan' :imputer=Imputer(missing_values='NaN' ,strategy='mean',axis=0)

          【讨论】:

            【解决方案5】:

            这应该可以工作

            imputer = Imputer(missing_values='NaN', strategy='mean', axis=0)
            imputer = imputer.fit(df.iloc[:, 2:3])
            

            【讨论】:

            • 虽然这可能有效,但最好也包含说明。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-09-17
            • 2021-12-01
            • 2018-09-24
            • 2019-03-10
            • 2020-02-18
            • 2020-07-21
            • 2020-09-29
            相关资源
            最近更新 更多