gpt4 book ai didi

python - `numpy.positive`的用例

转载 作者:行者123 更新时间:2023-12-03 15:15:04 26 4
gpt4 key购买 nike

有一个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输入的
  • 它仅针对算术类型定义。例如,如果您尝试在 bool 数组上调用它,您将得到
     UFuncTypeError: ufunc 'positive' did not contain a loop with signature matching types dtype('bool') -> dtype('bool')

  • 另一件事是,因为 positiveufunc ,它可以就地工作,使其成为算术类型的有效无操作函数:
    np.positive(x, out=x)

    关于python - `numpy.positive`的用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55419548/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com