Can someone provide me with a better (simpler, more readable, more Pythonic, more efficient, etc.) way to remove multiple values from an array than what follows:
import numpy as np
# The array.
x = np.linspace(0, 360, 37)
# The values to be removed.
a = 0
b = 180
c = 360
new_array = np.delete(x, np.where(np.logical_or(np.logical_or(x == a,
x == b),
x == c)))
A good answer to this question would produce the same result as the above code (i.e., new_array), but might do a better job dealing with equality between floats than the above code does.
BONUS
Can someone explain to me why this produces the wrong result?
In [5]: np.delete(x, x == a)
/usr/lib/python2.7/dist-packages/numpy/lib/function_base.py:3254: FutureWarning: in the future insert will treat boolean arrays and array-likes as boolean index instead of casting it to integer
"of casting it to integer", FutureWarning)
Out[5]:
array([ 20., 30., 40., 50., 60., 70., 80., 90., 100.,
110., 120., 130., 140., 150., 160., 170., 180., 190.,
200., 210., 220., 230., 240., 250., 260., 270., 280.,
290., 300., 310., 320., 330., 340., 350., 360.])
The values 0 and 10 have both been removed, rather than just 0 (a).
Note, x == a is as expected (so the problem is inside np.delete):
In [6]: x == a
Out[6]:
array([ True, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False, False], dtype=bool)
Note as well that np.delete(x, np.where(x == a)) produces the correct result. Thus, it appears to me that np.delete cannot handle Boolean indices.
delete. It specifies thatobj : slice, int or array of ints(no boolean).