The isfinite() function is defined under numpy, imported as import numpy as np. We can create multidimensional arrays and derive other mathematical statistics with the help of numpy. The infinite() function is used to test if an element is finite or not.
np.isfinite
The np.isfinite() function tests element-wise, whether it is finite (not infinity or not, Not a Number), and returns the result as the boolean array. You can pass arrays also to check whether the elements present in the array belong to a finite class. If it is not finite, the method returns False otherwise True. It returns Boolean values.
Syntax
numpy.isfinite(array or the scalar value, out(output array))
Parameters
The isfinite() function takes two parameters, out of which one parameter is optional, and one is required.
The first parameter is the input array or the input we want to check whether it is finite.
The second one is an n-dimensional array, which is optional. It is the output array that is placed with the result.
Return Value
The np.isfinite() function returns a Boolean array, which results if we pass an array and Boolean value True or False if we pass a scalar value.
Example programs on isfinite() method in Python
Write a program to show the isfinite() function’s working in Python.
# app.py import numpy as np # Scalar Values print("Is finite: - : ", np.isfinite(933), "\n") print("Is finite: - : ", np.isfinite(444), "\n") # checking for infinity value print("Is finite: - ", np.isfinite(np.inf), "\n") print("Is finite: - ", np.isfinite(np.NINF), "\n")
Output
python3 app.py Is finite: - : True Is finite: - : True Is finite: - False Is finite: - False
In this example, we’ve seen that bypassing two scalar values in the function isfinite(), we get True as it represents finite values, but when passed infinite values, we’re getting False.
Write a program to use the isfinite() function on math module values.
See the following code.
# app.py import math as m # For finite values print("Is finite:- ", m.isfinite(999)) print("Is finite:- ", m.isfinite(-999)) # For not a number and +ve infinity and -ve infinity values print("Is finite:- ", m.isfinite(float("nan"))) print("Is finite:- ", m.isfinite(float("inf"))) print("Is finite:- ", m.isfinite(float("-inf")))
Output
python3 app.py Is finite:- True Is finite:- True Is finite:- False Is finite:- False Is finite:- False
In this example, we can see that using the math module, and we are getting different sets of True and False values according to the values passed in the function.
Using numpy arange() with numpy isfinite()
Numpy arange() function returns the ndarray object containing evenly spaced values within the given range.
See the following code.
# app.py import numpy as np b = np.arange(10).reshape(2, 5) print("\n", b) print("\nIs Finite : \n", np.isfinite(b)) b = [[1j], [np.inf]] print("\nIs Finite : \n", np.isfinite(b))
Output
python3 app.py [[0 1 2 3 4] [5 6 7 8 9]] Is Finite : [[ True True True True True] [ True True True True True]] Is Finite : [[ True] [False]]
Other examples
# app.py import numpy as np print(np.isfinite(1)) print(np.isfinite(0)) print(np.isfinite(np.nan)) print(np.isfinite(np.inf)) print(np.isfinite(np.NINF))
Output
python3 app.py True True False False False
Not a Number, positive infinity, and negative infinity are considered nonfinite.
Numpy uses an IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity.
Also, positive infinity is not equivalent to negative infinity. But infinity is equal to positive infinity.
Errors result if the second argument is supplied when x is the scalar input or if the first and second arguments have different shapes.
That’s it for np.isfinite() method.