The isfinite() function of NumPy

The function numpy.isfinite() checks each element of an array-like for infinity and returns the results in an ndarray of Booleans. If the input is a scalar a Boolean value is returned.

Logical Operations Updated 20 Oct 2024 1 min read

Overview

  • The numpy.isfinite() checks each element of an NumPy array-like whether it is infinite and returns a Boolean ndarray containing results. The definition of infinity here includes positive inifinity, negative inifinity and Not a Number. When the input to the function is a NumPy scalar the result is also a scalar - a Boolean value representing True or False.

Example

python
# Example Python program that checks

            # elements of a NumPy array-like for finite values

            import numpy

            import math

# Create an ndarray

            n1 = numpy.ndarray(shape = (2, 2), dtype = numpy.double)

# Fill the ndarray with finite and infinite values

            n1[0][0] = 22/7

            n1[0][1] = math.inf

            n1[1][0] = math.inf + 1

            n1[1][1] = math.inf * 1

            print(n1)

# Find the infinite numbers

            results= numpy.isfinite(n1)

            print(results)

Output

Output
[[3.14285714        inf]

 [       inf        inf]]

[[ True False]

 [False False]]