3

If I have a numpy array for example :

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. 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.

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? Is there a better way to approach the problem?

3 Answers 3

6

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]])
Sign up to request clarification or add additional context in comments.

1 Comment

This is certainly better than using setdiff1d for many reasons. And since boolean arrays only use a byte per item, even two copies of a boolean index array will be smaller than an integer index array and its complement. To save even a bit more memory, I believe this won't make a copy: numpy.logical_not(ix, out=ix).
1

I managed to create without_ones with:

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.