Replace NaN's in NumPy array with closest non-NaN value

前端 未结 7 1711
你的背包
你的背包 2021-02-05 04:22

I have a NumPy array a like the following:

>>> str(a)
\'[        nan         nan         nan  1.44955726  1.44628034  1.44409573\\n  1.4408         


        
7条回答
  •  悲&欢浪女
    2021-02-05 05:21

    NaNs have the interesting property of comparing different from themselves, thus we can quickly find the indexes of the non-nan elements:

    idx = np.nonzero(a==a)[0]
    

    it's now easy to replace the nans with the desired value:

    for i in range(0, idx[0]):
        a[i]=a[idx[0]]
    for i in range(idx[-1]+1, a.size)
        a[i]=a[idx[-1]]
    

    Finally, we can put this in a function:

    import numpy as np
    
    def FixNaNs(arr):
        if len(arr.shape)>1:
            raise Exception("Only 1D arrays are supported.")
        idxs=np.nonzero(arr==arr)[0]
    
        if len(idxs)==0:
            return None
    
        ret=arr
    
        for i in range(0, idxs[0]):
            ret[i]=ret[idxs[0]]
    
        for i in range(idxs[-1]+1, ret.size):
            ret[i]=ret[idxs[-1]]
    
        return ret
    

    edit

    Ouch, coming from C++ I always forget about list ranges... @aix's solution is way more elegant and efficient than my C++ish loops, use that instead of mine.

提交回复
热议问题