Your expression works if you add parentheses:
>>> y[(1 < x) & (x < 5)]
array(['o', 'o', 'a'],
dtype='|S1')
Answer from jfs on Stack OverflowNumPy
numpy.org › doc › stable › reference › generated › numpy.select.html
numpy.select — NumPy v2.5 Manual
Return an array drawn from elements in choicelist, depending on conditions.
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.
thisPointer
thispointer.com › home › numpy › numpy – select elements by condition
NumPy - Select Elements By Condition - thisPointer
April 29, 2023 - import numpy as np def main(): print('Select elements from Numpy Array based on conditions') #Create an Numpy Array containing elements from 5 to 30 but at equal interval of 2 arr = np.arange(5, 30, 2) print('Contents of the Numpy Array : ' , arr) # Comparision OPerator will be applied to all elements in array boolArr = arr < 10 print('Contents of the Bool Numpy Array : ', boolArr) # Select elements with True at corresponding value in bool array newArr = arr[boolArr] print('Contents of the New Numpy Array : ', newArr) newArr = arr[arr < 10] print('Contents of the New Numpy Array : ', newArr) p
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.0 › reference › generated › numpy.select.html
numpy.select — NumPy v2.0 Manual
Return an array drawn from elements in choicelist, depending on conditions.
NumPy
numpy.org › doc › stable › reference › generated › numpy.where.html
numpy.where — NumPy v2.5 Manual
Return elements chosen from x or y depending on condition.
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.select.html
numpy.select — NumPy v2.3 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.
Codegive
codegive.com › blog › numpy_select_elements_by_condition.php
Numpy select elements by condition
At its heart, numpy select elements by condition refers to the process of extracting or manipulating specific values within a NumPy array based on a logical test. This is primarily achieved through a technique called boolean indexing (also known as boolean masking).
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])
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 › stable › reference › generated › numpy.select.html
numpy.select — NumPy v2.4 Manual
Return an array drawn from elements in choicelist, depending on conditions.
NumPy
numpy.org › doc › 1.21 › reference › generated › numpy.select.html
numpy.select — NumPy v1.21 Manual
numpy.select(condlist, choicelist, default=0)[source]¶ · Return an array drawn from elements in choicelist, depending on conditions.