The isfinite() function of NumPy

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:

# 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:

[[3.14285714        inf]

 [       inf        inf]]

[[ True False]

 [False False]]

 


Copyright 2024 © pythontic.com