Python 按数组数组中的列计算布尔数

Python 按数组数组中的列计算布尔数,python,multidimensional-array,Python,Multidimensional Array,我有一个带布尔值的数组: [[False False True ..., False True False] [False True True ..., True False True] [False False False ..., True True False] ..., [False False False ..., False False False] [ True True True ..., True True True] [ True T

我有一个带布尔值的数组:

[[False False  True ..., False  True False]
 [False  True  True ...,  True False  True]
 [False False False ...,  True  True False]
 ..., 
 [False False False ..., False False False]
 [ True  True  True ...,  True  True  True]
 [ True  True  True ...,  True  True  True]]
<type 'numpy.ndarray'>

如何按列计算布尔数?

假设我有一个numpy数组

a = numpy.ones([3, 4])
>>> a
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])
numpy有一个非常简洁的特性,让您可以在多个维度中指定切片,这样数组[row\u index,col\u index]就有意义了。考虑以下事项:

>>> sum(a[:,0])
3.0

我刚刚添加了列索引为0的所有行值。将该值替换为iterable,您就有了解决方案。

假设我有一个numpy数组

a = numpy.ones([3, 4])
>>> a
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])
numpy有一个非常简洁的特性,让您可以在多个维度中指定切片,这样数组[row\u index,col\u index]就有意义了。考虑以下事项:

>>> sum(a[:,0])
3.0

我刚刚添加了列索引为0的所有行值。用iterable替换该值,您就有了解决方案。

如果您需要一个纯Python解决方案,我会选择它


如果你需要一个纯Python的解决方案,我会选择

支持对多个轴上的数组求和。列使用
0
th轴,行使用
1
st轴

>>> arr = np.ndarray(shape=(3, 4), dtype=bool)
>>> arr
array([[False,  True, False,  True],
       [False, False, False,  True],
       [False, False, False, False]], dtype=bool)
>>> np.sum(arr, axis=0)
array([0, 1, 0, 2])
>>> np.sum(arr, axis=1)
array([2, 1, 0])
支持对多个轴上的数组求和。列使用
0
th轴,行使用
1
st轴

>>> arr = np.ndarray(shape=(3, 4), dtype=bool)
>>> arr
array([[False,  True, False,  True],
       [False, False, False,  True],
       [False, False, False, False]], dtype=bool)
>>> np.sum(arr, axis=0)
array([0, 1, 0, 2])
>>> np.sum(arr, axis=1)
array([2, 1, 0])