NumPy
numpy.org › doc › stable › reference › generated › numpy.select.html
numpy.select — NumPy v2.5 Manual
numpy.select(condlist, choicelist, default=0)[source]# Return an array drawn from elements in choicelist, depending on conditions. Parameters: condlistlist of bool ndarrays · The list of conditions which determine from which array in choicelist the output elements are taken.
numpy.where
An array with elements from x where condition is True, and elements from y elsewhere. ... Try it in your browser! >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])
Array creation routines
Please refer to Record arrays for record arrays.
numpy.nonzero
Returns a tuple of arrays, one for each dimension of a, containing the indices of the non-zero elements in that dimension.
Array manipulation routines
copyto(dst, src[, casting, where]) · Copies values from one array to another, broadcasting as necessary
NumPy
numpy.org › devdocs › reference › generated › numpy.select.html
numpy.select — NumPy v2.6.dev0 Manual
Return an array drawn from elements in choicelist, depending on conditions.
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.select.html
numpy.select — NumPy v2.1 Manual
Return an array drawn from elements in choicelist, depending on conditions.
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.select.html
numpy.select — NumPy v2.0 Manual
numpy.select(condlist, choicelist, default=0)[source]# Return an array drawn from elements in choicelist, depending on conditions. Parameters: condlistlist of bool ndarrays · The list of conditions which determine from which array in choicelist the output elements are taken.
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.select.html
numpy.select — NumPy v2.3 Manual
numpy.select(condlist, choicelist, default=0)[source]# Return an array drawn from elements in choicelist, depending on conditions. Parameters: condlistlist of bool ndarrays · The list of conditions which determine from which array in choicelist the output elements are taken.
NumPy
numpy.org › doc › 1.18 › reference › generated › numpy.select.html
numpy.select — NumPy v1.18 Manual
Return elements from one of two arrays depending on condition. ... >>> x = np.arange(10) >>> condlist = [x<3, x>5] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist) array([ 0, 1, 2, ..., 49, 64, 81])
NumPy
numpy.org › doc › 1.25 › reference › generated › numpy.select.html
numpy.select — NumPy v1.25 Manual
Return elements from one of two arrays depending on condition. ... >>> x = np.arange(6) >>> condlist = [x<3, x>3] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist, 42) array([ 0, 1, 2, 42, 16, 25])
Top answer 1 of 6
274
Your expression works if you add parentheses:
>>> y[(1 < x) & (x < 5)]
array(['o', 'o', 'a'],
dtype='|S1')
2 of 6
45
IMO OP does not actually want np.bitwise_and() (aka &) but actually wants np.logical_and() because they are comparing logical values such as True and False - see this SO post on logical vs. bitwise to see the difference.
>>> x = array([5, 2, 3, 1, 4, 5])
>>> y = array(['f','o','o','b','a','r'])
>>> output = y[np.logical_and(x > 1, x < 5)] # desired output is ['o','o','a']
>>> output
array(['o', 'o', 'a'],
dtype='|S1')
And equivalent way to do this is with np.all() by setting the axis argument appropriately.
>>> output = y[np.all([x > 1, x < 5], axis=0)] # desired output is ['o','o','a']
>>> output
array(['o', 'o', 'a'],
dtype='|S1')
by the numbers:
>>> %timeit (a < b) & (b < c)
The slowest run took 32.97 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 1.15 µs per loop
>>> %timeit np.logical_and(a < b, b < c)
The slowest run took 32.59 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.17 µs per loop
>>> %timeit np.all([a < b, b < c], 0)
The slowest run took 67.47 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 5.06 µs per loop
so using np.all() is slower, but & and logical_and are about the same.
NumPy
numpy.org › doc › 1.16 › reference › generated › numpy.select.html
numpy.select — NumPy v1.16 Manual
February 18, 2020 - Return elements from one of two arrays depending on condition. ... >>> x = np.arange(10) >>> condlist = [x<3, x>5] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist) array([ 0, 1, 2, 0, 0, 0, 36, 49, 64, 81])
Earth Data Science
earthdatascience.org › home
Slice (or Select) Data From Numpy Arrays | Earth Data Science - Earth Lab
September 23, 2019 - Just like for the one-dimensional numpy array, you use the index [1,2] for the second row, third column because Python indexing begins with [0], not with [1] On this page, you will use indexing to select elements within one-dimensional and two-dimensional numpy arrays, a selection process referred to as slicing.
NumPy
numpy.org › doc › stable › reference › generated › numpy.choose.html
numpy.choose — NumPy v2.5 Manual
Given an “index” array (a) of integers and a sequence of n arrays (choices), a and each choice array are first broadcast, as necessary, to arrays of a common shape; calling these Ba and Bchoices[i], i = 0,…,n-1 we have that, necessarily, Ba.shape == Bchoices[i].shape for each i.
SciPy
docs.scipy.org › doc › numpy-1.8.1 › reference › generated › numpy.select.html
numpy.select — NumPy v1.8 Manual
Return elements from one of two arrays depending on condition. ... >>> x = np.arange(10) >>> condlist = [x<3, x>5] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist) array([ 0, 1, 2, 0, 0, 0, 36, 49, 64, 81])
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.choose.html
numpy.choose — NumPy v2.1 Manual
Construct an array from an index array and a list of arrays to choose from. First of all, if confused or uncertain, definitely look at the Examples - in its full generality, this function is less simple than it might seem from the following code description (below ndi = numpy.lib.index_tricks):
NumPy
numpy.org › doc › 1.14 › reference › generated › numpy.select.html
numpy.select — NumPy v1.14 Manual
June 12, 2018 - Return elements from one of two arrays depending on condition. ... >>> x = np.arange(10) >>> condlist = [x<3, x>5] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist) array([ 0, 1, 2, 0, 0, 0, 36, 49, 64, 81])