问题描述
例如,如果我有一个numpy数组:
If I have a numpy array for example :
A = np.array([[3, 2], [2, -1], [2, 3], [5, 6], [7,-1] , [8, 9]])
我想用不为-1的子数组将子数组与不包含子数组的部分分开.请记住,我正在处理非常大的数据集,因此每个操作都可能很长,因此我尝试在内存和CPU时间方面拥有最有效的方式.
I would like to separate the part of the array with the subarrays having -1 from the ones who don't.Keep in mind that I'm working on very big data set, so every operation can be very long so I try to have the most effective way memory and CPU-time wise.
我现在正在做的是:
slicing1 = np.where(A[:, 1] == -1)
with_ones = A[slicing1]
slicing2 = np.setdiff1d(np.arange(A.shape[0]), slicing1, assume_unique=True)
without_ones = A[slicing2]
有没有一种方法可以不创建列表,以减少内存消耗,因为它可能非常大?有没有更好的方法来解决这个问题?
Is there a way to not create the slicing2
list to decrease the memory consumption as it can be very big?Is there a better way to approach the problem?
推荐答案
一种方法是存储所需的逻辑索引,然后在第二种情况下使用其逻辑否定来存储索引:
One way is to store the logical index needed and then in the second case index using its logical negation:
In [46]: indx = A[:, 1] != -1
In [47]: A[indx]
Out[47]:
array([[3, 2],
[2, 3],
[5, 6],
[8, 9]])
In [48]: A[~indx]
Out[48]:
array([[ 2, -1],
[ 7, -1]])
这篇关于numpy数组中的互补切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!