Test element-wise for positive or negative infinity in Numpy

NumpyServer Side ProgrammingProgramming

To test element-wise for positive or negative infinity, use the numpy.isinf() method in Python Numpy. Returns a boolean array of the same shape as x, True where x == +/-inf, otherwise False.

NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). Errors result if the second argument is supplied when the first argument is a scalar, or if the first and second arguments have different shapes.

Steps

At first, import the required library −

import numpy as np

To test element-wise for positive or negative infinity, use the numpy.isinf() method in Python Numpy.

Checking for numbers −

print("Infinite? ", np.isinf(1))
print("\nInfinite? ", np.isinf(0))

Checking for float −

print("\nInfinite? ", np.isinf(14.))
print("\nInfinite? ", np.isinf(3.6))

Checking for NaN −

print("\nInfinite? ", np.isinf(np.nan))
print("\nInfinite? ", np.isinf(np.NAN))

Checking for infinity −

print("\nInfinite? ", np.isinf(np.inf))
print("\nInfinite? ", np.isinf(np.NINF))

Checking for log −

print("\nInfinite? ", np.isinf(np.log(1)))
print("\nInfinite? ", np.isinf(np.log(2)))

Example

import numpy as np

# To test element-wise for positive or negative infinity, use the numpy.isinf() method in Python Numpy

# Checking for numbers
print("Infinite? ", np.isinf(1))
print("\nInfinite? ", np.isinf(0))

# Checking for float
print("\nInfinite? ", np.isinf(14.))
print("\nInfinite? ", np.isinf(3.6))

# Checking for NaN
print("\nInfinite? ", np.isinf(np.nan))
print("\nInfinite? ", np.isinf(np.NAN))

# Checking for infinity
print("\nInfinite? ", np.isinf(np.inf))
print("\nInfinite? ", np.isinf(np.NINF))

# Checking for log
print("\nInfinite? ", np.isinf(np.log(1)))
print("\nInfinite? ", np.isinf(np.log(2)))

Output

Infinite? False

Infinite? False

Infinite? False

Infinite? False

Infinite? False

Infinite? False

Infinite? True

Infinite? True

Infinite? False

Infinite? False
raja
Updated on 07-Feb-2022 11:30:52

Advertisements