Возможно, простой пример поможет лучше понять его.
# sample array
In [19]: week = np.arange(1, 8)
# find middle 3 days of the week
# to do so, we first find boolean masks by performing
# (week > 2) which performs element-wise comparison, so does (week < 6)
# then we simply do a `logical_and` on these two boolean masks
In [20]: middle = (week > 2) & (week < 6)
In [21]: middle
Out[21]: array([False, False, True, True, True, False, False])
# index into the original array to get the days
In [22]: week[middle]
Out[22]: array([3, 4, 5])
оператор &
эквивалентен numpy.logical_and()
, тогда как >
и <
операторов эквивалентны numpy.greater()
и numpy.less
соответственно.
# create a boolean mask (for days greater than 2)
In [23]: week > 2
Out[23]: array([False, False, True, True, True, True, True])
# create a boolean mask (for days less than 6)
In [24]: week < 6
Out[24]: array([ True, True, True, True, True, False, False])
# perform a `logical_and`; note that this is exactly same as `middle`
In [25]: np.logical_and((week > 2), (week < 6))
Out[25]: array([False, False, True, True, True, False, False])
In [26]: mid = np.logical_and((week > 2), (week < 6))
# sanity check again
In [27]: week[mid]
Out[27]: array([3, 4, 5])