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