有一个positive
函数在 numpy
(版本 1.13+),它似乎什么都不做:
In [1]: import numpy as np
In [2]: A = np.array([0, 1, -1, 1j, -1j, 1+1j, 1-1j, -1+1j, -1-1j, np.inf, -np.inf])
In [3]: A == np.positive(A)
Out[3]:
array([ True, True, True, True, True, True, True, True, True,
True, True])
文档说:
Returned array or scalar: `y = +x`
这个函数的用例是什么?
最佳答案
此功能的用例可能很少。提供它是因为每个 python 运算符在 numpy 中都作为 ufunc 公开:
+
:np.positive
-
:np.negative
+
:np.add
-
:np.subtract
如documentation状态,并在另一个答案中注明,
np.positive
复制数据,就像 np.copy
可以,但有两个警告:dtype
输入的 UFuncTypeError: ufunc 'positive' did not contain a loop with signature matching types dtype('bool') -> dtype('bool')
另一件事是,因为
positive
是 ufunc
,它可以就地工作,使其成为算术类型的有效无操作函数:np.positive(x, out=x)
关于python - `numpy.positive`的用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55419548/