【问题标题】:How to turn a numpy array mask (boolean) into floats or ints如何将 numpy 数组掩码(布尔值)转换为浮点数或整数
【发布时间】:2014-07-29 07:07:46
【问题描述】:

我正在尝试使用timelatlon 的数组创建一个出现频率图。我应该得到一个二维lat/lon 频率数组。下面的代码概述了我的方法,当我将反转的布尔数组掩码转换为数值时,我在步骤 d 遇到了问题。我不小心找到了一种方法,但我不知道它为什么有效(np.mean)。我不明白为什么np.mean 将布尔值转换为浮点数,但实际上并没有计算沿请求轴的平均值。我不得不再次申请np.mean 以获得所需的结果。我觉得必须有正确的方法将布尔数组转换为浮点数或整数。另外,如果你能想出更好的方法来完成任务,那就开火吧。我的 numpy mojo 很弱,这是我能想到的唯一方法。

import numpy as np

# test 3D array in time, lat, lon; values are percents
# real array is size=(30,721,1440)

a = np.random.random_integers(0,100, size=(3,4,5))
print(a)

# Exclude all data outside the interval 0 - 20 (first quintile)
# Repeat for 21-40, 41-60, 61-80, 81-100

b = np.ma.masked_outside(a, 0, 20)
print "\n\nMasked array:  "
print(b)

# Because mask is false where data within quintile, need to invert

c = [~b.mask] 
print "\n\nInverted mask:  "
print(c)

# Accidental way to turn True/False to 1./0., but that's what I want

d = np.mean(c, axis = 0)  
print "\n\nWhy does this work? How should I be doing it?"
print(d)

# This is the mean I want.  Gives desired end result

e = np.mean(d, axis = 0)
print "\n\nFrequency Map"
print(e)

如何将我的(反转)数组掩码中的布尔值转换为数字(1 和 0)?

【问题讨论】:

    标签: python arrays numpy boolean


    【解决方案1】:

    它“有效”是因为您的 c 不是您认为的那样:

    >>> c
    [array([[[False, False, False, False, False],
            [False, False, False, False,  True],
            [False, False, False, False, False],
            [False, False, False, False, False]],
    
           [[False, False, False, False, False],
            [False, False, False, False,  True],
            [False, False, False,  True, False],
            [False, False, False, False,  True]],
    
           [[False, False, False, False, False],
            [False, False, False, False, False],
            [False,  True, False, False, False],
            [ True, False,  True,  True, False]]], dtype=bool)]
    >>> type(c)
    <type 'list'>
    

    它不是一个数组,它是一个包含数组的列表。所以当你采取

    d = np.mean(c, axis = 0)  
    

    你正在取一个元素列表的平均值,它只是它本身(但转换为浮点数,因为 meanfloat(True) == 1.0 就是这样做的。)

    相反,去掉不需要的括号:

    >>> c = ~b.mask
    >>> output = c.mean(axis=0)
    >>> output
    array([[ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
           [ 0.        ,  0.        ,  0.        ,  0.        ,  0.66666667],
           [ 0.        ,  0.33333333,  0.        ,  0.33333333,  0.        ],
           [ 0.33333333,  0.        ,  0.33333333,  0.33333333,  0.33333333]])
    >>> np.allclose(output, e)
    True
    

    顺便说一句,从 bool 转换为 float 或 int 的规范方法是使用 astype,例如c.astype(float)c.astype(int) 但老实说有时我很懒,只写c + 0.0c + 0。不过,你没有从我这里听到。

    【讨论】: