3

The title is pretty self-explanatory: I have an numpy array like (let's say ints) [ 1 2 10 2 12 2 ] and I would like to remove all occurrences of 2, so that the resulting array is [ 1 10 12 ]. Preferably I would like to do this as fastest as possible, because I am using relatively large arrays.

NumPy has a function called numpy.delete() but it takes the indexes as an argument, which I do not have.

Edit: The question is indeed different from Deleting certain elements from numpy array using conditional checks, which is I guess a more "general" case. However, the idea of removing occurrences from an array is fundamental enough to merit its own explicit question, so I am keeping the question.

mlg556
  • 419
  • 3
  • 13

3 Answers3

7

You can use indexing:

arr = np.array([1, 2, 10, 2, 12, 2])
print(arr[arr != 2])
# [ 1 10 12]

Timing is pretty good:

from timeit import Timer

arr = np.array(range(5000))
print(min(Timer(lambda: arr[arr != 4999]).repeat(500, 500)))
# 0.004942436999999522
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
3

you can use another numpy function.It is numpy.setdiff1d(ar1, ar2, assume_unique=False). This function Finds the set difference of two arrays.

import numpy as np
a = np.array([1, 2, 10, 2,12, 2])
b = np.array([2])
c = np.setdiff1d(a,b,True)
print(c)
1

There are several ways to do this. I suggest you use a mask:

import numpy as np
a = np.array([ 1, 2 ,10, 2, 12, 2 ])
a[~np.isin(a, 2)]
>> array([ 1, 10, 12])

np.isin is convenient because you can apply the filter to multiple elements at once if you need to:

a[~np.isin(a, (1,2))]
>>  array([ 10, 12])

Also note that a[mask] is a slice of the original array. This is memory efficient; but if you need to create a new array with your filtered values and leave the original ones untouched, use .copy, e.g.:

b = a[~np.isin(a, (1,2))].copy()
Tarifazo
  • 4,118
  • 1
  • 9
  • 22