# Test finiteness (not infinity and not Not a Number) in Numpy

NumpyServer Side ProgrammingProgramming

#### Python Data Science basics with Numpy, Pandas and Matplotlib

Most Popular

63 Lectures 6 hours

#### Data Analysis using NumPy and Pandas

19 Lectures 8 hours

#### Numpy with Python

Most Popular

12 Lectures 3 hours

To test for finiteness, use the numpy.isfinite() method in Python Numpy. Returns True where x is not positive infinity, negative infinity, or NaN; false otherwise. This is a scalar if x is a scalar.

This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

## Steps

At first, import the required library −

import numpy as np

To test for finiteness, use the numpy.isfinite() method in Python Numpy. Checking for numbers −

print("Finite? ", np.isfinite(1))
print("\nFinite? ", np.isfinite(0))

Checking for float −

print("\nFinite? ", np.isfinite(14.))
print("\nFinite? ", np.isfinite(3.6))

Checking for NaN −

print("\nFinite? ", np.isfinite(np.nan))
print("\nFinite? ", np.isfinite(np.NAN))

Checking for infinity −

print("\nFinite? ", np.isfinite(np.inf))
print("\nFinite? ", np.isfinite(np.NINF))

Checking for log −

print("\nFinite? ", np.isfinite(np.log(1)))
print("\nFinite? ", np.isfinite(np.log(2)))

## Example

import numpy as np

# To test for finiteness, use the numpy.isfinite() method in Python Numpy

# Checking for numbers
print("Finite? ", np.isfinite(1))
print("\nFinite? ", np.isfinite(0))

# Checking for float
print("\nFinite? ", np.isfinite(14.))
print("\nFinite? ", np.isfinite(3.6))

# Checking for NaN
print("\nFinite? ", np.isfinite(np.nan))
print("\nFinite? ", np.isfinite(np.NAN))

# Checking for infinity
print("\nFinite? ", np.isfinite(np.inf))
print("\nFinite? ", np.isfinite(np.NINF))

# Checking for log
print("\nFinite? ", np.isfinite(np.log(1)))
print("\nFinite? ", np.isfinite(np.log(2)))

## Output

Finite? True

Finite? True

Finite? True

Finite? True

Finite? False

Finite? False

Finite? False

Finite? False

Finite? True

Finite? True