Numpy isposinf: How to Use np isposinf() Method
The isposinf() function is defined under numpy, which can be imported as import numpy as np, and we can create multidimensional arrays.
Numpy isposinf()
The isposinf() function in Numpy tests if an element is a positive infinity or not. We have already written an article about whether an element is negative infinity or not using the Numpy isneginf() function.
The numpy isposinf() function returns the result in Boolean values for scalar values and Boolean array for boolean array inputs.
Syntax
numpy.isposinf(array or the scalar value, out(output array))
Parameters
The isposinf() function takes two parameters out of which 1 parameter is optional.
The first parameter is an input array or the input for which we want to check whether it is +ve infinity.
The second one is the n-dimensional array, which is optional. It is an output array that is placed with the result.
Return Value
The function returns the Boolean array, which has the result if we pass the array and Boolean value True or False if we pass a scalar value according to the value passed.
Example programs on isposinf() method in Python
Write a program to show the working of isposinf() function in Python.
# app.py import numpy as np # Scalar Values print("Positive Infinity - : ", np.isposinf(933), "\n") print("Positive Infinity - : ", np.isposinf(444), "\n") # checking for infinity value print("Positive Infinity - : ", np.isposinf(np.inf), "\n") print("Positive Infinity - : ", np.isposinf(np.NINF), "\n")
Output
python3 app.py Positive Infinity -: False Positive Infinity -: False Positive Infinity -: True Positive Infinity -: False
In this example, we have seen that bypassing two scalar values in the function isposinf() we get False as it doesn’t represent any positive infinity. Still, using +ve infinite values, we’re getting True.
Write a program to use arange() function and create an array then check for each value of the elements if it’s positive infinity or not.
See the following code.
# app.py import numpy as np a = np.arange(24).reshape(6, 4) print("List: ") print(a) print("\n") print("Positive Infinity: ") print(np.isposinf(a))
Output
python3 app.py List: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15] [16 17 18 19] [20 21 22 23]] Positive Infinity: [[False False False False] [False False False False] [False False False False] [False False False False] [False False False False] [False False False False]]
In the above code example, we can see that after creating an array using the np.arange() function, we have checked for each element if it’s positive infinity or not.
That’s it for this tutorial.