缺少ndarray .floor()和.ceil()方法?

时间:2018-06-17 19:12:06

标签: python numpy

我正在处理一个numpy.matrix而我错过了向上和向下的功能。 即我能做到:

data = [[1, -20],[-30, 2]]
np.matrix(data).mean(0).round().astype(int).tolist()[0]
Out[58]: [-14, -9]

因此使用.round()。但我无法使用.floor().ceil()

SciPy NumPy 1.14 reference中未提及它们。

为什么缺少这些(非常必要的)功能?

编辑:  我发现你可以np.floor(np.matrix(data).mean(0)).astype(int).tolist()[0]。但为什么差异呢?为什么.round()是一种方法而.floor不是?

1 个答案:

答案 0 :(得分:1)

与大多数这些why问题一样,我们只能从模式中推断出可能的原因,以及对历史的一些了解。

https://docs.scipy.org/doc/numpy/reference/ufuncs.html#floating-functions

floorceil被归类为floating ufuncsrint也是ufunc,其执行方式与round相同。 ufuncs具有标准化界面,包括outwhere等参数。

np.round位于/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.pynumeric是合并为当前numpy的原始软件包之一。它是np.round_的别名,最终调用np.around,也在fromnumeric。请注意,可用参数包括out,还包括decimalsrint中缺少的参数)。它委托.round方法。

拥有一个函数的一个优点是您不必先将列表转换为数组:

In [115]: data = [[1, -20],[-30, 2]]
In [119]: np.mean(data,0)
Out[119]: array([-14.5,  -9. ])
In [120]: np.mean(data,0).round()
Out[120]: array([-14.,  -9.])
In [121]: np.rint(np.mean(data,0))
Out[121]: array([-14.,  -9.])

使用其他参数:

In [138]: np.mean(data,axis=0, keepdims=True,dtype=int)
Out[138]: array([[-14,  -9]])
相关问题