आईएमओ ओपी वास्तव में np.bitwise_and()(उर्फ &) नहीं चाहता है, लेकिन वास्तव में चाहता है np.logical_and()क्योंकि वे तार्किक मूल्यों की तुलना कर रहे हैं जैसे Trueऔर False- इस एसओ पोस्ट को तार्किक बनाम बिटवाइज में अंतर देखने के लिए देखें।
>>> 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')
और ऐसा करने के लिए समतुल्य तरीका उचित रूप np.all()से axisतर्क को स्थापित करने के साथ है ।
>>> output = y[np.all([x > 1, x < 5], axis=0)] # desired output is ['o','o','a']
>>> output
array(['o', 'o', 'a'],
dtype='|S1')
संख्याओं द्वारा:
>>> %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
इसलिए का उपयोग कर np.all()धीमी है, लेकिन &और logical_andएक ही के बारे में कर रहे हैं।