Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use cases of `numpy.positive`

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?

like image 413
Yury Kirienko Avatar asked Mar 29 '19 14:03

Yury Kirienko


People also ask

What is the advantage of using NumPy?

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.

What is NumPy used for in real life?

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.

Where is NumPy used in real world?

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.

What are the advantages of using NumPy for data analysis?

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.


1 Answers

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)
like image 90
Mad Physicist Avatar answered Nov 06 '22 03:11

Mad Physicist