Test element-wise for positive or negative infinity in Numpy


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("
Infinite? ", np.isinf(0))

Checking for float −

print("
Infinite? ", np.isinf(14.)) print("
Infinite? ", np.isinf(3.6))

Checking for NaN −

print("
Infinite? ", np.isinf(np.nan)) print("
Infinite? ", np.isinf(np.NAN))

Checking for infinity −

print("
Infinite? ", np.isinf(np.inf)) print("
Infinite? ", np.isinf(np.NINF))

Checking for log −

print("
Infinite? ", np.isinf(np.log(1))) print("
Infinite? ", 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("
Infinite? ", np.isinf(0)) # Checking for float print("
Infinite? ", np.isinf(14.)) print("
Infinite? ", np.isinf(3.6)) # Checking for NaN print("
Infinite? ", np.isinf(np.nan)) print("
Infinite? ", np.isinf(np.NAN)) # Checking for infinity print("
Infinite? ", np.isinf(np.inf)) print("
Infinite? ", np.isinf(np.NINF)) # Checking for log print("
Infinite? ", np.isinf(np.log(1))) print("
Infinite? ", 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

Updated on: 07-Feb-2022

293 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements