numpy.isinf()

Python’s numpy.isinf() is used to test if element is a positive or negative infinity or not. It returns a Boolean array with same shape as the input array, where each element is ‘True’ if the corresponding element of the input array is either positive or negative infinity, and ‘False’ otherwise.

Syntax: numpy.isinf(array [,out])

Parameters:

array : Input array or object whose elements are need to test weather it is a infinity or not.

Out : (ndarray, optional) It represents the location into which the output of the method is stored.

import numpy as np
arr = np.array([0, 1, -1, np.inf, -np.inf, np.nan, 5, -np.inf, np.inf])
result = np.isinf(arr)
print("Array:",arr)
print("Is Infinity:", result)
Array: [0. 1. -1. inf -inf nan 5. -inf inf]
Is Infinity:[False False False True True False False True True]

The output shows True for the infinite values (np.inf, -np.inf) and False for all other values.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top