【问题标题】:cannot remove NaN from numpy array无法从 numpy 数组中删除 NaN
【发布时间】:2019-12-22 23:47:32
【问题描述】:

我有一个 numpy 数组 d(形状 (2658,12)),在第 6 列中有 77 个 NaN; (d[:,6] != d[:,6]).sum() 给出 77。

我想用特定数字(例如 -1)替换这些 NaN。

所以我做到了:

for i in range(len(d)):
    if d[i,6]!=d[i,6]:
        d[i,6] = -1

之后我还有 56 个 NaN。 (d[:,6] != d[:,6]).sum()now 给出 56。

怎么可能?

如果它很重要: sys.version_info(major=3, minor=6, micro=9, releaselevel='final', serial=0)

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    您可以通过以下方式使用 nan 值获取这些位置的插值:

    import numpy as np
    
    # filling the matrix example
    d = np.random.random([11, 8])
    d[3, 6] = np.nan
    d[5, 6] = np.nan
    
    # take a column
    column = d[:, 6]
    
    # find anchor indexes where values are not nan's for linear interpolation
    xp = np.where(~np.isnan(column))[0]
    
    # take their values according to indexes
    fp = d[:, 6][xp]
    
    # then find indexes where values are nan's
    x = np.where(np.isnan(column))[0]
    
    # copy matrix to compare results
    result = np.copy(d)
    
    # do linear interpolation with numpy for x indexes using xp and fp
    result[(x,), 6] = np.interp(x, xp, fp)
    
    print(d)
    print('---')
    print(result)
    

    【讨论】:

    • 聪明。谢谢。
    【解决方案2】:

    numpy 有一些非常有用的功能。看看这个例子:

    >>> import numpy as np
    >>> d = np.array([[1, 2, 3, None], [None, 3, 4, 5]], dtype=float)
    >>> d
    array([[ 1.,  2.,  3., nan],
           [nan,  3.,  4.,  5.]])
    >>> d[np.where(np.isnan(d))] = -1
    >>> d
    array([[ 1.,  2.,  3., -1.],
           [-1.,  3.,  4.,  5.]])
    

    我认为这是你需要的。

    在您的示例中,它将是:

    d[:,6][np.where(np.isnan(a[:,6]))] = -1
    

    【讨论】:

    • 我的原始数据有 77 个 nan。在评估 d[np.where(np.isnan(d))] = -1 之后,我仍然有 77 个 nan。我也收到错误:输入类型不支持 ufunc 'isnan',并且根据转换规则 ''safe'' 无法安全地将输入强制转换为任何支持的类型。
    • 如果你想处理一个特定的行或列,例如,你可以用切片来做:d[:,6][np.where(np.isnan(a[:,6 ]))] = -1
    • 这行得通吗? d[:,6][np.where(np.isnan(d[:,6].astype(float)))] = -1
    • Uau!有用。但不是 -1 假设我想将相邻行的平均值分配给 d[:,6],例如d[i,6] = (d[i-1,6]+d[i+1,6])/2。我试过for i in range(len(d)): if np.isnan(d[i,6].astype(float)): d[i,6] = (d[i-1,6]+d[i+1,6])/2,但没用。有什么建议吗?
    • 正确的方法是使用numpy.interp。请参阅示例的下一个答案。
    猜你喜欢
    • 2018-12-21
    • 1970-01-01
    • 2015-06-08
    • 2015-05-21
    • 2016-08-15
    • 2020-06-19
    • 2020-04-21
    • 1970-01-01
    • 2020-05-19
    相关资源
    最近更新 更多