Python “Numpy天花板和地板”;“出去”;论点

Python “Numpy天花板和地板”;“出去”;论点,python,numpy,Python,Numpy,从NumPy中,NumPy.ceil函数接受两个参数,第二个参数是out。文档没有说明此out参数的作用,但我假设您可以设置此函数返回的输出类型,但我无法使其工作: In [107]: np.ceil(5.5, 'int') --------------------------------------------------------------------------- TypeError Traceback (most rec

从NumPy中,
NumPy.ceil
函数接受两个参数,第二个参数是
out
。文档没有说明此
out
参数的作用,但我假设您可以设置此函数返回的输出类型,但我无法使其工作:

In [107]: np.ceil(5.5, 'int')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-107-c05bcf9f1522> in <module>()
----> 1 np.ceil(5.5, 'int')

TypeError: return arrays must be of ArrayType

In [108]: np.ceil(5.5, 'int64')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-108-0937d09b0433> in <module>()
----> 1 np.ceil(5.5, 'int64')

TypeError: return arrays must be of ArrayType
[107]中的
:np.ceil(5.5,'int')
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 np.ceil(5.5,'int')
TypeError:返回数组必须为ArrayType
In[108]:np.ceil(5.5,'int64')
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 np.ceil(5.5,'int64')
TypeError:返回数组必须为ArrayType
是否可以使用此参数使
np.ceil
返回一个整数


谢谢。

out
是输出数组(必须与输入具有相同的形状)

如果您将其构造为所需的
dtype
,则将得到以下
dtype

>>> arr = np.array([5.5, -7.2])
>>> out = np.empty_like(arr, dtype=np.int64)
>>> np.ceil(arr, out)
array([ 6, -7], dtype=int64)
>>> out
array([ 6, -7], dtype=int64)

您没有指定返回类型。 试试这个


np.ceil
ufuncs
之一。该类别的一般文件如下:

op(X, out=None)
Apply op to X elementwise

Parameters
----------
X : array_like
    Input array.
out : array_like
    An array to store the output. Must be the same shape as `X`.

Returns
-------
r : array_like
    `r` will have the same shape as `X`; if out is provided, `r`
    will be equal to out.
out
r
是获取函数输出的不同方式。最简单的方法是让函数返回值。但有时您可能需要将它将填充的数组
out
。控制
dtype
是使用
out
的一个原因。另一种方法是通过“重用”已经存在的数组来节省内存


np.ceil
返回的数组也可以强制转换为您所需的类型,例如
np.ceil(x).astype('int')

这将导致
5.0
,而OP的ceil将返回
6.0
这将分别导致
6
6
-7
op(X, out=None)
Apply op to X elementwise

Parameters
----------
X : array_like
    Input array.
out : array_like
    An array to store the output. Must be the same shape as `X`.

Returns
-------
r : array_like
    `r` will have the same shape as `X`; if out is provided, `r`
    will be equal to out.