Use cases of `numpy.positive` Use cases of `numpy.positive` numpy numpy

Use cases of `numpy.positive`


There are likely very few use-cases for this function. It is provided because every python operator is exposed as a ufunc in numpy:

  • Unary +: np.positive
  • Unary -: np.negative
  • Binary +: np.add
  • Binary -: np.subtract
  • etc ...

As the documentation states, and noted in the other answer, np.positive makes a copy of the data, just as np.copy does, but with two caveats:

  1. It can change the dtype of the input

  2. It is only defined for arithmetic types. If you attempt to call it on a boolean array, for example, you will get

     UFuncTypeError: ufunc 'positive' did not contain a loop with signature matching types dtype('bool') -> dtype('bool')

One other thing, is that since positive is a ufunc, it can work in-place, making it an effective no-op function for arithmetic types:

np.positive(x, out=x)


if you have a vector x, then np.positive(x) gives you, +1*(x) and np.negative(x) gives you -1*(x).

np.positive([-1,0.7])output: array([-1. ,  0.7])np.negative([-1.5,0.7])output:array([ 1.5, -0.7])np.positive(np.array([0, 1, -1, 1j, -1j, 1+1j, 1-1j, -1+1j, -1-1j, np.inf, -np.inf]))output: array([  0.+0.j,   1.+0.j,  -1.+0.j,   0.+1.j,  -0.-1.j,   1.+1.j,         1.-1.j,  -1.+1.j,  -1.-1.j,  inf+0.j, -inf+0.j])np.negative(np.array([0, 1, -1, 1j, -1j, 1+1j, 1-1j, -1+1j, -1-1j, np.inf, -np.inf]))output: array([ -0.-0.j,  -1.-0.j,   1.-0.j,  -0.-1.j,   0.+1.j,  -1.-1.j,        -1.+1.j,   1.-1.j,   1.+1.j, -inf-0.j,  inf-0.j])

Use Case depends though. Once use case its an alternative of x1 = copy(x). Its creates an duplicate array for your use.