使用 python3.x 在 numpy 中进行楼层划分的 ufunc 是什么?



在python 2.x中,我可以使用通用函数numpy.divide来进行楼层划分。然而,ufunc dividetrue_divide在python 3.x中都进行了真正的划分。

In [24]: np.divide([1, 2, 3, 4], 2)
Out[24]: array([ 0.5,  1. ,  1.5,  2. ])
In [25]: np.true_divide([1, 2, 3, 4], 2)
Out[25]: array([ 0.5,  1. ,  1.5,  2. ])

那么,在numpy中使用python3进行楼层划分的通用功能是什么呢?

这是NumPy ufunc np.floor_divide:

>>> np.floor_divide([1, 2, 3, 4], 2)
array([0, 1, 1, 2])

或者,您可以使用//运算符:

>>> a = np.array([-2, -1, 0, 1, 2])
>>> a // 2
array([-1, -1,  0,  0,  1])

最新更新