Python数组中的零值为nan值

时间:2014-12-30 06:40:44

标签: python arrays nan

我有一个数组,其中包含一些零值,我希望以纳米值转换。当我申请代码时 所有的价值都变成了纳米

myarray
array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       ..., 
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.]])

myarray.shape
(64L, 52L)
myarray.max()
4563.666015625
myarray.min()
0.0

我希望将零值转换为nan。我使用stackoverflow

中的示例
a = np.arange(3.0)
a
array([ 0.,  1.,  2.])
a[a==0] = np.nan
a
array([ nan,   1.,   2.])

当我将示例应用于我的数组时,所有值都变为nan

myarray[myarray == 0.] = nan
myarray
array([[ nan,  nan,  nan, ...,  nan,  nan,  nan],
       [ nan,  nan,  nan, ...,  nan,  nan,  nan],
       [ nan,  nan,  nan, ...,  nan,  nan,  nan],
       ..., 
       [ nan,  nan,  nan, ...,  nan,  nan,  nan],
       [ nan,  nan,  nan, ...,  nan,  nan,  nan],
       [ nan,  nan,  nan, ...,  nan,  nan,  nan]])

myarray.max()
nan
myarray.min()
nan

1 个答案:

答案 0 :(得分:4)

并非所有的价值都变为nan,而且(1)您只查看有关的部分,以及(2){{1} }和min不能与max s合作。

例如,如果我们制作一个类似于你的数组:

nan

它可能看起来就像所有>>> myarray = np.zeros((64, 52)) >>> myarray[3:-3,3:-3] = np.random.uniform(0, 5000, (64-6,52-6)) >>> myarray array([[ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], ..., [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.]]) >>> myarray[myarray==0] = np.nan >>> myarray array([[ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]]) 一样,但它不是:

nan

您还可以使用忽略>>> myarray[2:5, 2:5] array([[ nan, nan, nan], [ nan, 1500.05326562, 4583.70521213], [ nan, 4896.62420284, 892.83210033]]) 的{​​{3}}和nanmin

nan