简体   繁体   English

numpy:计算元素满足条件的2D数组

[英]numpy: Counting in a 2D array where an element satisfies a condition

I have a numpy array like below. 我有一个如下的numpy数组。 I need a count of rows where the first element is 2. So in the array below, four rows start with 2 - the answer would be 4. How is this best accomplished in numpy? 我需要计算第一个元素为2的行数。因此,在下面的数组中,四行以2开头-答案是4。如何在numpy中做到最好? (I cannot use pandas, but can use scipy). (我不能使用熊猫,但可以使用scipy)。

array([[1, 4, 5],
       [1, 4, 5],
       [2, 4, 5],
       [2, 4, 5],
       [2, 4, 5],
       [2, 4, 5],
       [3, 4, 5],
       [3, 4, 5],
       [3, 4, 5],
       [3, 4, 5],
       [3, 4, 5],
       [3, 4, 5]])

First, take the first column, all rows: 首先,获取第一列,所有行:

a[:,0]

Then, find the 2 s: 然后,找到2 s:

a[:,0] == 2

That gives you a boolean array. 那给你一个布尔数组。 Which you can then sum: 然后可以总结一下:

(a[:,0] == 2).sum()

There is np.count_nonzero which in a common idiom is applied to logical arrays generated by evaluating a condition 有一个np.count_nonzero ,它在一个常见用法中适用于通过评估条件生成的逻辑数组

np.count_nonzero(data[:, 0] == 2)

Btw. 顺便说一句。 it's probably just for the sake of example, but if your array is sorted like yours you can also use np.searchsorted 这可能只是出于示例的目的,但是如果您对数组进行排序的方式也可以使用np.searchsorted

np.diff(np.searchsorted(data[:, 0], (2, 3)))[0]

One more approach in addition to above approaches 除上述方法外,还有另一种方法

>>> x[:,0]==2
array([False, False,  True,  True,  True,  True, False, False, False,
       False, False, False], dtype=bool)

will give you TRUE for the rows which have first column as 2. 对于第一列为2的行,将为TRUE。

>>> x[x[:,0]==2]
array([[2, 4, 5],
       [2, 4, 5],
       [2, 4, 5],
       [2, 4, 5]])

gives you corresponding rows and which satisfy the required condition. 给您相应的行并满足所需条件。 Now, you can use shape function to get length. 现在,您可以使用形状函数来获取长度。

x[x[:,0]==2].shape[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM