How to delete an object from a numpy array without knowing the index

Question:

Is it possible to delete an object from a numpy array without knowing the index of the object but instead knowing the object itself?

I have seen that it is possible using the index of the object using the np.delete function, but I’m looking for a way to do it having the object but not its index.

Example:

[a,b,c,d,e,f]

x = e

I would like to delete x.

Asked By: MhmdMnsr

||

Answers:

You can find the index/indices of the object using np.argwhere, and then delete the object(s) using np.delete.

Example:

x = np.array([1,2,3,4,5])
index = np.argwhere(x==3)
y = np.delete(x, index)
print(x, y)
Answered By: jojonas

Boolean indexing or masking is a good basic way of selecting, or removing specific elements of an array

You talk about removing a specific ‘object’. Let’s take that literally and define an array of dtype object:

In [2]: x=np.array(['a','b','c','d','e'],dtype=object)
In [3]: x
Out[3]: array(['a', 'b', 'c', 'd', 'e'], dtype=object)
In [4]: x=='d'   # elements that equal 'd'
Out[4]: array([False, False, False,  True, False], dtype=bool)
In [5]: x!='d'    # elements that don't
Out[5]: array([ True,  True,  True, False,  True], dtype=bool)
In [6]: x[x!='d']   # select a subset
Out[6]: array(['a', 'b', 'c', 'e'], dtype=object)

Behind the scenes argwhere and delete use this. Note that argwhere uses the x==d boolean array, converting it to array indices. And constructing mask like this is one way that delete operates.

There are some important limits:

  • that equality (or not equality) test has to work for your values. It might not if the elements are floats.

  • deleting from a 1d array is easier than from a 2d (or larger) one. With 2d you have to decide whether to delete a row, a column, or an element (and in the process flattening the array).

  • deleting only one element of that matches is a bit trickier.

For some cases it might be better to .tolist() the array and use a list method.

In [32]: xl=x.tolist()
In [33]: xl.remove('d')
In [34]: np.array(xl,dtype=object)
Out[34]: array(['a', 'b', 'c', 'e'], dtype=object)

There’s no exact equivalent to list.remove for arrays.

Answered By: hpaulj

Cast it as a numpy array, and mask it out:

x = np.array(list("abcdef"))

x = x[x!='e']  # <-- THIS IS THE METHOD

print x
# array(['a', 'b', 'c', 'd', 'f'])

Doesn’t have to be more complicated than this.

Answered By: Ulf Aslak

arr = np.array(['a','b','c','d','e','f'])

Then

arr = [x for x in arr if arr != 'e']

Answered By: Shoresh

You could use np.setdiff1d(a,b), it returns all unique elements from a that are not in b.

>>> arr = np.array(['a', 'a', 'b', 'c', 'd', 'e', 'f'])
>>> to_remove = ['b', 'c']
>>> np.setdiff1d(arr, to_remove)
array(['a', 'd', 'e', 'f'], dtype='<U1')
Answered By: Philipp

If you have a multi-dimension np array:

arr= np.array([1,2,3,], [1,2,3,4],['a','b'])

You should do this way to remove specific values:

Create a boolean mask of values to be deleted

mask = arr == 3

Delete elements from the array using the boolean mask

new_arr = np.delete(arr, np.where(mask))

Print the new array

print(new_arr)

But in this way shape is changing. for example, if I have (12,3) ndarray it will be converted to (36,). To keep it fixed like (12,3):

Create a boolean mask of values to be deleted

mask = arr == 3

Flatten the boolean mask

flat_mask = np.ravel(mask)

Delete elements from the array using the flattened boolean mask

 new_arr = np.delete(arr, np.where(flat_mask))

Reshape the new array to the original shape

new_arr = new_arr.reshape(arr.shape[0], arr.shape[1])

Print the new array

print(new_arr)

I hope it will be useful for you.

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.