简体   繁体   English

在numpy数组中互补切片

[英]complementary slicing in a numpy array

If I have a numpy array for example : 如果我有一个numpy数组,例如:

A = np.array([[3, 2], [2, -1], [2, 3], [5, 6], [7,-1] , [8, 9]])

I would like to separate the part of the array with the subarrays having -1 from the ones who don't. 我想将数组的部分与具有-1的子数组分开。 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. 请记住,我正在处理非常大的数据集,所以每个操作都可能很长,所以我尝试以最有效的方式记忆内存和CPU时间。

What I am doing for the moment is : 我现在正在做的是:

 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? 有没有办法不创建slicing2列表来减少内存消耗,因为它可能非常大? 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]])

我设法创建了without_one:

filter(lambda x: x[1] != -1,A)

Or you could use a generator function: 或者您可以使用生成器功能:

A = np.array([[3, 2], [2, -1], [2, 3], [5, 6], [7,-1] , [8, 9]])

def filt(arr):
    for item in arr:
        if item[1]!=-1:
            yield item

new_len = 0
for item in A:
    if item[1] != -1:
        new_len += 1

without_ones = np.empty([new_len, 2], dtype=int)
for i, item in enumerate(filt(A)): 
    without_ones[i] = item

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

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