Overview:
- The numpy.isinf() function checks each element of a NumPy array-like for infinity and returns the results as an ndarray containing Boolean values. When the input is a scalar it returns a single Boolean value.
- The numpy.isinf() function returns True for both positive infinity and negative infinity. For "Not a number(nan)" the function isinf() will return False.
Example:
# Example Python program that checks the array_nd = numpy.ndarray(shape = (2, 2)) array_nd[0][0] = numpy.nan print("Contents of the ndarray:") print("Is an element infinity:") |
Output:
Contents of the ndarray: [[ nan -inf] [ inf 3.14285714]] Is an element infinity: [[False True] [ True False]] |
Example 2:
# Example Python program that checks the # Check a complex number for infiniti # Check a float for infiniti # Check googol for infiniti |
Output:
False <class 'float'> False Googol: 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 Traceback (most recent call last): File "/Users/vinodh/PythonProgs/np_isinf_ex1.py", line 20, in <module> infResult = numpy.isinf(googol) ^^^^^^^^^^^^^^^^^^^ TypeError: ufunc 'isinf' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' |