1

I have a 2D array: [[0,0], [0,1], [1,0], [1,1]]

I want to delete the [0,1] element without knowing its position within the array (as the elements may be shuffled).

Result should be: [[0,0], [1,0], [1,1]]

I've tried using numpy.delete but keep getting back a flattened array:

>>> arr = np.array([[0,0], [0,1], [1,0], [1,1]])
>>> arr
array([[0, 0],
       [0, 1],
       [1, 0],
       [1, 1]])
>>> np.delete(arr, [0,1])
array([0, 1, 1, 0, 1, 1])

Specifying the axis removes the 0, 1 elements rather than searching for the element (which makes sense):

>>> np.delete(arr, [0,1], axis=0)
array([[1, 0],
       [1, 1]])

And trying to find the location (as has been suggested) seems equally problematic:

>>> np.where(arr==[0,1])
(array([0, 1, 1, 3]), array([0, 0, 1, 1]))

(Where did that 3 come from?!?)

1
  • You first have to identify the location. Commented Feb 16, 2020 at 7:28

3 Answers 3

2

Here we find all of the rows that match the candidate [0, 1]

>>> (arr == [0, 1]).all(axis=1)
array([False,  True, False, False])

Or alternatively, the rows that do not match the candidate

>>> ~(arr == [0, 1]).all(axis=1)
array([ True, False,  True,  True])

So, to select all those rows that do not match [0, 1]

>>> arr[~(arr == [0, 1]).all(axis=1)]
array([[0, 0],
       [1, 0],
       [1, 1]])

Note that this will create a new array.

Sign up to request clarification or add additional context in comments.

Comments

1
mask = (arr==np.array([0,1])).all(axis=1)
arr1 = arr[~mask,:]

Look at mask.. It should be [False, True,...].

Comments

0

From the documentation:

numpy.delete(arr, obj, axis=None)

axis : int, optional

The axis along which to delete the subarray defined by obj. If axis is None, obj is applied to the flattened array

If you don't specify the axis(i.e. None), it will automatically flatten your array; you just need to specify the axis parameter, in your case np.delete(arr, [0,1],axis=0)

However, just like in the example above, [0,1] is a list of indices; you must provide the indices/location(you can do that with np.where(condition,array) for example)

Here you have a working example:

    my_array = np.array([[0, 1],
                         [1, 0],
                         [1, 1],
                         [0, 0]])

    row_index, = np.where(np.all(my_array == [0, 1], axis=1))
    my_array = np.delete(my_array, row_index,axis=0)
    print(my_array)

    #Output is below

        [[1 0]
         [1 1]
         [0 0]]

4 Comments

That seems to delete the 0 & 1 elements; I end up with array([[1, 0], [1, 1]])
Yes, that is how it works.Unfortunately, you will need to specify the indices. One way is to use np.where to get the indices of your array and to delete only those indices.
So how would you use np.where to find that single element? >>> np.where(arr==[0,1]) returns (array([0, 1, 1, 3]), array([0, 0, 1, 1])) -- not even sure how a 3 got in there. But I've tried multiple permutations of that and get nowhere.
The problem that you are facing is that np.where() on multidimensional arrays returns row_indices and column_indices, not a tuple(x,y) where the condition is satisfied. I updated the example so that you see a working example.

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.