There is a positive
function in numpy
(version 1.13+), which seemingly does nothing:
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])
The documentation says: Returned array or scalar: `y = +x`
What are the use cases of this function?
Why use NumPy? NumPy arrays are faster and more compact than Python lists. An array consumes less memory and is convenient to use. NumPy uses much less memory to store data and it provides a mechanism of specifying the data types.
NumPy is very useful for performing mathematical and logical operations on large high dimensional arrays and matrices. It has a plethora of functions that can be used for performing various numerical operations efficiently. A Brief Description of what NumPy offers at various levels.
It is a very diverse library and has a wide range of applications in other sectors. Numpy can be put to use along with Data Science, Data Analysis and Machine Learning. It is also a base for other python libraries. These libraries use the functionalities in NumPy to increase their capabilities.
NumPy uses much less memory to store data The NumPy arrays takes significantly less amount of memory as compared to python lists. It also provides a mechanism of specifying the data types of the contents, which allows further optimisation of the code. If this difference seems intimidating then prepare to have more.
There are likely very few use-cases for this function. It is provided because every python operator is exposed as a ufunc in numpy:
+
: np.positive
-
: np.negative
+
: np.add
-
: np.subtract
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:
It can change the dtype
of the input
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With