🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.isinf()

AI & DATA SCIENCE // np-isinf

np.isinf() tests each element of an array and returns a boolean array indicating which elements are positive or negative infinity.

Syntax

np.isinf(arr)

Deep Dive Course

isinf() returns True specifically for inf and -inf, and False for every finite number and for nan — it doesn't distinguish between positive and negative infinity by default, though np.isposinf() and np.isneginf() exist for that finer distinction if needed. Infinite values typically arise from dividing a nonzero number by zero, or from an operation like np.exp() overflowing for a very large input, so isinf() is a common diagnostic check after computations prone to those specific failure modes.

1Understanding np.isinf()

isinf() returns True specifically for inf and -inf, and False for every finite number and for nan — it doesn't distinguish between positive and negative infinity by default, though np.isposinf() and np.isneginf() exist for that finer distinction if needed. Infinite values typically arise from dividing a nonzero number by zero, or from an operation like np.exp() overflowing for a very large input, so isinf() is a common diagnostic check after computations prone to those specific failure modes.

💡

isinf() treats positive and negative infinity the same by default — use np.isposinf()/np.isneginf() specifically when you need to distinguish which direction the overflow happened in, rather than just detecting that it happened.

editor.html
import numpy as np

arr = np.array([1.0, np.inf, -np.inf, 5.0])
print(np.isinf(arr))
localhost:3000

2Practical Example

Here is a real-world application of np.isinf() showing how it is used in production NumPy code.

editor.html
import numpy as np

result = np.array([1.0, 5.0]) / np.array([2.0, 0.0])
print(result)
print(np.isinf(result))
localhost:3000

3Best Practices

Follow these guidelines when working with np.isinf():

1. Check for inf values specifically with isinf() after divisions or exponentials that could plausibly overflow

2. Use np.isposinf()/np.isneginf() instead of isinf() when the direction, positive vs negative infinity, of the overflow actually matters

3. Combine isinf() with np.where() to replace infinite values with a safe substitute, like the array's maximum finite value

⚠️

Tip: isinf() treats positive and negative infinity the same by default — use np.isposinf()/np.isneginf() specifically when you need to distinguish which direction the overflow happened in, rather than just detecting that it happened.

editor.html
import numpy as np

arr = np.array([1.0, np.inf, -np.inf, 5.0])
print(np.isinf(arr))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([1.0, np.inf, -np.inf, 5.0])
print(np.isinf(arr))
Example 02Advanced Example
import numpy as np

result = np.array([1.0, 5.0]) / np.array([2.0, 0.0])
print(result)
print(np.isinf(result))

Best Practices

  • Check for inf values specifically with isinf() after divisions or exponentials that could plausibly overflow
  • Use np.isposinf()/np.isneginf() instead of isinf() when the direction, positive vs negative infinity, of the overflow actually matters
  • Combine isinf() with np.where() to replace infinite values with a safe substitute, like the array's maximum finite value

Interview Question

Does np.isinf() return True for nan, given that nan also represents an 'invalid' numeric result?

Hint: Think about whether nan and infinity represent the same kind of problem.

No — isinf() returns False for nan. nan and infinity are distinct special floating-point values representing different situations: infinity represents a value that genuinely grew unboundedly large or resulted from dividing by zero, while nan represents a result that's fundamentally undefined or indeterminate, like 0 divided by 0 or the square root of a negative number. isinf() checks specifically for the infinity bit pattern, and nan doesn't match it, which is exactly why NumPy also provides the separate isnan() check, and isfinite() as a combined check covering both cases at once.

Exercises

MediumPractice using np.isinf() in a real scenario.
View Solution
import numpy as np

arr = np.array([1.0, np.inf, -np.inf, 5.0])
print(np.isinf(arr))

Frequently Asked Questions

Does np.isinf() return True for nan, given that nan also represents an 'invalid' numeric result?

No — isinf() returns False for nan. nan and infinity are distinct special floating-point values representing different situations: infinity represents a value that genuinely grew unboundedly large or resulted from dividing by zero, while nan represents a result that's fundamentally undefined or indeterminate, like 0 divided by 0 or the square root of a negative number. isinf() checks specifically for the infinity bit pattern, and nan doesn't match it, which is exactly why NumPy also provides the separate isnan() check, and isfinite() as a combined check covering both cases at once.

Related Functions

np-isfinitenp-isnannp-divide