在 numpy 数组中使用等价运算符是什么意思
What does the equivalence operator mean when used in a numpy array
我正在按照我在 Python 数据科学手册 中找到的示例进行操作,该示例的目的是创建两个数组掩码以最终输出夏季雨天夏季,作者假设夏季从6月21日开始,也就是第172天,3个月后结束。
这里我只对他做summer interval的那段代码感兴趣:
# Construct a mask for all summer days (June 21st is the 172nd day)
summer = (np.arange((365) - 172 < 90 ) & np.arange((365) - 172 > 0)
在本书的另一个版本中,我找到了这段代码,我认为它会导致相同的结果:
# construct a mask of all summer days (June 21st is the 172nd day)
days = np.arange(365)
summer = (days > 172) & (days < 262)
两个例子我都不太清楚,请帮忙。
也许一个简单的例子可以帮助更好地理解它。
# 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()
whereas the >
and <
operators are equivalent to numpy.greater()
and 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])
我正在按照我在 Python 数据科学手册 中找到的示例进行操作,该示例的目的是创建两个数组掩码以最终输出夏季雨天夏季,作者假设夏季从6月21日开始,也就是第172天,3个月后结束。
这里我只对他做summer interval的那段代码感兴趣:
# Construct a mask for all summer days (June 21st is the 172nd day)
summer = (np.arange((365) - 172 < 90 ) & np.arange((365) - 172 > 0)
在本书的另一个版本中,我找到了这段代码,我认为它会导致相同的结果:
# construct a mask of all summer days (June 21st is the 172nd day)
days = np.arange(365)
summer = (days > 172) & (days < 262)
两个例子我都不太清楚,请帮忙。
也许一个简单的例子可以帮助更好地理解它。
# 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()
whereas the >
and <
operators are equivalent to numpy.greater()
and 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])