简体   繁体   English

如何使用 numpy 方法根据另一个 np 数组的条件对一个 np 数组的某些行执行操作?

[英]How to perform operations on certain rows of one np array based on conditions of another np array using numpy methods?

For example, I have one np array A = [[30, 60, 50...], [ 15, 20, 18...], [21, 81, 50...]...] of size (N, 10) .例如,我有一个 np 数组A = [[30, 60, 50...], [ 15, 20, 18...], [21, 81, 50...]...]大小(N, 10)

And I have another np array B = [1, 1, 0...] of size (N, ) .我还有另一个大小为(N, )的 np 数组B = [1, 1, 0...]

I want to do operations Eg I want all the sums of each column in A but only for rows where B==1.我想做操作 例如,我想要 A 中每一列的所有总和,但只适用于 B==1 的行。 How would I do that without using any loops and just numpy methods?如果不使用任何循环而仅使用 numpy 方法,我将如何做到这一点?

So if I want sum of columns in A for indices where B == 1:因此,如果我想要 A 中的列总和用于 B == 1 的索引:

result = 30 + 15 because the first two indices in B are 1 but the third index is 0 so I wouldn't include it in the sum.结果 = 30 + 15 因为 B 中的前两个索引是 1 但第三个索引是 0 所以我不会将它包含在总和中。

Use np.compress and sum along axis=0使用np.compress和沿axis = 0 sum

>>> A = [[30, 60, 50], [ 15, 20, 18], [21, 81, 50]]
>>> B = [1, 1, 0]
>>> np.compress(B, A, axis=0).sum(0)
array([45, 80, 68])

If array, use np.nonzero on B :如果是数组,请在B上使用np.nonzero

>>> A = np.array([[30, 60, 50], [ 15, 20, 18], [21, 81, 50]])
>>> A[np.nonzero(B)].sum(0)
array([45, 80, 68])

Another way:另一种方式:

>>> A[B.astype(bool)].sum(0)
array([45, 80, 68])

If you want 0 s:如果你想要0秒:

>>> np.compress(B==0, A, axis=0).sum(0)
# Or,
>>> A[np.nonzero(B==0)].sum(0)
# Or,
>>> A[~B.astype(bool)].sum(0)

If you want both 1 s and 0 s, obviously:如果你想要1 s 和0 s,显然:

>>> A.sum(0)

You can convert B to bool type and mask A .您可以将B转换为bool类型并掩码A Then you can get the sum along columns.然后你可以得到沿列的sum

A = np.array([[30, 60, 50], [ 15, 20, 18], [21, 81, 50]])
B = np.array([1, 1, 0])

A[B.astype(np.bool)].sum(axis=0)
array([45, 80, 68])

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

相关问题 根据布尔条件将np数组转换为锯齿np数组 - Turning an np array into a jagged np array based on boolean conditions 如何根据另一个数组的行分配np数组的行? - How to assign rows of np array according to rows of another array? NumPy:按np.array过滤行 - NumPy: filter rows by np.array 如何使用 np.where 在二维数组上使用 numpy 进行过滤 - How to filter with numpy on 2D array using np.where 如何根据其他两个 numpy arrays 仅使用 Z2EA9510C37F7F821ECBFZF24 操作的条件获得新的 numpy 阵列? - How to get a new numpy array based on conditions of two other numpy arrays using only numpy operations? 如何使用 np.array() 和 np.arange(3) 的组合手动创建一个 3×3×3 NumPy 数组? - How to manually create a three-by-three-by-three NumPy array using a combination of np.array() and np.arange(3)? Numpy:用另一个数组组合数组列表(np.choose alternative) - Numpy: Combine list of arrays by another array (np.choose alternative) 避免在np.array操作中使用for循环。 蟒蛇 - Avoid using for loop in np.array operations. Python 根据另一个维度使用np.where设置一个numpy切片 - Set a numpy slice using np.where based on another dimension 我正在尝试使用for循环和np.concatenate将索引为0的所有行从一个数组转换为另一个数组 - I'm trying to get all rows with an Index 0 from one array into another one with for loop and np.concatenate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM