【问题标题】:Convert nan to Zero when numpy dtype is “object” within custom transformer当 numpy dtype 是自定义转换器中的“对象”时,将 nan 转换为零
【发布时间】:2021-03-27 00:37:40
【问题描述】:

我正在读取一个 csv 文件,该文件包含使用 pandas read_csv 具有空白空间的数字列。 我将读取的数据帧传递到调用自定义转换器的列转换器中。 将数据帧转换为 numpy 数组并传递给上述客户转换器。

在这个转换器中,我试图替换我无法这样做的 nan 值

深入研究,我发现所有这些解决方案到目前为止都没有任何效果

以下是我的自定义转换器

class Xfrmer_replacenum(BaseEstimator, TransformerMixin):
    """
        this transformer does the global repplace within the dataframe
        replace 365243 spcific to this case study with 0
        replace +/-inf , nan with zero
    """
    # constructor
    def __init__(self):
        #we are not going to use this 
        self._features = None
        
    #Return self 
    def fit(self, X,y=None  ):
        return self
    
    def transform(self,X,y=None):     
        print(X)
        print(X.dtype)
        X = X.astype(float)
        #replace high values with zero
        #or col in X.columns:
        #for col in X.columns:
        X[X==365243.0] = 0
        X[X==365243] = 0
        #np.where(X_==X_,X_,0)
        #np.nan_to_num(X[0, :].astype(np.float64))
        #X = np.nan_to_num(X.astype(np.float64))
        #X = X.astype(str).replace('nan', 0).astype(float)
        #np.frompyfunc(lambda x: x.replace(',',''),1,1)(X).astype(float)
        np.array([v.replace(',', '') for v in X], dtype=np.float32)
        print('replaced values')
        #X=X.replace([np.inf,-np.inf],np.nan)
        #X=X.replace(np.nan,0)    
        print('all replace',X.shape)
        print('just before ret',X[X==365243])
        np.savetxt("./output/prvapln_colxfrmr_onlynum.csv", X,fmt='%s',delimiter=",")
        return X

这是我在列转换器中使用自定义转换器的方式




    lst_cols =["ID1","ID2","AMT_CREDIT_SUM","AMT_CREDIT_SUM_DEBT","AMT_CREDIT_SUM_LIMIT","AMT_CREDIT_SUM_OVERDUE"]

        lst_idx =[]

    lst_all_cols = X_train.columns.values.tolist()

        for col in lst_cols:
                
            idx = lst_all_cols.index(col)
            lst_idx.append(idx)

        preprcs_stg1_pipln = ColumnTransformer( transformers = [
                                                    ( 'repl_pipln', Xfrmer_replacenum(),lst_idx)],
                                                    remainder='passthrough')

这些是我尝试在自定义转换器中使用的各种东西:

        np.where(X_==X_,X_,0)
        
        X = np.nan_to_num(X.astype(np.float64))

        X = X.astype(str).replace('nan', 0).astype(float)

        np.frompyfunc(lambda x: x.replace(',',''),1,1)(X).astype(float)

        np.array([v.replace(',', '') for v in X], dtype=np.float32)

我的数据是这样的

[[2163253 154602 4187.34 -1230.0 -1226.0 0.0]
 [1676258 433469 22242.825 -1343.0 -1334.0 1.0]
 [2075578 418383 7656.705 -2341.0 -2332.0 0.0]
 [1548737 391536 21416.85 nan nan nan]
 [2721491 292308 3959.1 -2604.0 -2601.0 1.0]
 [2595549 432416 3951.225 -540.0 -537.0 0.0]]
object

对于那些有替换的我得到这个错误

AttributeError: 'numpy.ndarray' 对象没有属性 'replace'

我的问题

如何替换上述 ndarray of dtype object 中的 nan ?

【问题讨论】:

  • 你能替换 Pandas DataFrame 中的值吗? df['ColumnName'].fillna(value=0) 如果没有,请分享更多代码。
  • 我已经添加了必要的代码。由于数据泄漏问题,我只能使用管道和变压器
  • a[np.isnan(a)] = 1?
  • a[np.isnan(a)] = 1 感谢这工作。你能把这个作为答案然后我会接受它

标签: python pandas numpy pipeline


【解决方案1】:

您正在寻找的是找到所有 np.nan 列,然后使用以下代码将它们设置为跟随

a[np.isnan(a)] = 1 # or whatever value

【讨论】:

    猜你喜欢
    • 2020-01-10
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2014-02-12
    相关资源
    最近更新 更多