How to Use Numpy Positive in Python?

Numpy Positive

This article would be covering a rather strange function within the numpy library of Python. It is so strange that at prima facie one might even question its very existence.

Enter the numpy.positive( ) function! It returns the element-wise numerical positive for the input array.

Makes sense right? But, here is the catch! It does not convert negative numbers within the input array into positives. Fascinating, ain’t it?  Let us have a closer look at the use case of this function, but before that, there is something that needs to be done. Importing the numpy library. The following code can be used to serve the purpose.

import numpy as np

Thereafter, we shall explore further the positive( ) function through each of the following sections.

  • Syntax of the positive( ) function
  • When to use numpy.positive( )?
  • Using numpy.positive( ) on N-Dimensional Arrays

Syntax of the positive( ) function

Following is the syntax of the positive( ) function which contains both the mandatory and the optional inputs required for its functioning.

numpy.positive(x, out=None, *, where=True, dtype=None)

where,

  • x – Scalar or N-dimensional array for which the numerical positive is to be returned
  • out – an optional construct set to none by default, but could be used to store the results in the desired array which is of the same length as the output
  • * kwargs or keyword argument which is an optional construct used to pass keyword variable length of argument to a function
  • where – an optional construct which is used to calculate the universal function (ufunc) at the given position when set to True (default setting) or not calculate when set to False
  • dtype – an optional construct used to specify the data type which is being used

When to use numpy.positive( )?

Let us construct an array & put it through this function to see what happens.

ar1 = np.array([[-1, 0, -7, 9],
                [3, -4, 23, -13]])
r1 = np.positive (ar1)
print (r1)

Following are the results for the above code.

Numerical Positives For Ar1
Numerical Positives for ar1

It doesn’t even bother to do anything or so you might think! Let’s have a look at the address where the values of the input array & the result are stored.

ad1 = print (id(ar1))
ad2 = print (id(r1))
Comparing Address For Input Result
Comparing Addresses of Input & Result

It is evident that both the input & result are stored at different locations. ‘So what? I can do the same with a copy( ) function?’ one might ask. But does your copy( ) function do this?

r2 = np.positive (ar1, dtype = float)
Converting Data Type Of The Result
Converting Data Type Of The Result

Yep! The numpy.positive( ) function offers a copy of the original input stored at a different location along with the provisions to modify the data type. It works with all data types except for the Boolean which when used shall result in the following.

UFuncNoLoopError: ufunc 'positive' did not contain a loop with signature matching types

One can run the numerical positive of the input array with the modified data type as required, leaving the original untouched. So now you know when to use the positive( ) function.


Using numpy.positive( ) on N-Dimensional Arrays

In this section, we shall demonstrate the usage of positive( ) function to store the result of an N-dimensional array in another designated array of the same size.

ar1 = np.array([[-1, 0, -7, 9],
                [3, -4, 23, -13]])
r2 = np.zeros((2,4))
print(r2)
np.positive (ar1, out=r2, dtype=float)
print(r2)
Results For Ar1
Results for ar1

Conclusion

Now that we have reached the end of this article, hope it has elaborated on how to use the positive( ) function from the numpy library. Here’s another article that details the usage of logaddexp2( ) function from the numpy library in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Audere est faucere!

References