IMO OP tidak benar-benar ingin np.bitwise_and()
(alias &
) tetapi sebenarnya ingin np.logical_and()
karena mereka membandingkan nilai-nilai logis seperti True
dan False
- lihat posting SO ini pada logis vs bitwise untuk melihat perbedaannya.
>>> 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')
Dan cara yang setara untuk melakukan ini adalah dengan np.all()
mengatur axis
argumen dengan tepat.
>>> output = y[np.all([x > 1, x < 5], axis=0)] # desired output is ['o','o','a']
>>> output
array(['o', 'o', 'a'],
dtype='|S1')
dengan angka:
>>> %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
jadi menggunakan np.all()
lebih lambat, tetapi &
dan logical_and
hampir sama.