Skip to Content

Numpy – Check For Negative Infinity

In this tutorial, we will look at how to check if a value in a Numpy array is negative infinity or not with the help of some examples.

How to test for negative infinity in a Numpy array?

check for negative infinity in numpy array

You can use the numpy.isneginf() function to check (element-wise) if values in a Numpy array are negative infinity or not. The following is the syntax –

📚 Discover Online Data Science Courses & Programs (Enroll for Free)

Introductory ⭐

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# test for negative infinity - pass scaler value or numpy array
np.isneginf(a)

It returns a boolean value (True if the value is negative infinity otherwise False) if you pass a scaler value and a boolean array if you pass an array.

Examples

Let’s now look at some examples of using the above function to test for -ve infinity.

Example 1 – Check if a number is a negative infinity or not using numpy.isneginf()

First, let’s pass scaler values to the numpy.isneginf() function.


Upskill your career right now →

Let’s create three variables – one containing a finite value and the others set to positive and negative infinity respectively and then apply the numpy.isneginf() function on each of these values.

import numpy as np

# create three variables with scaler values
a = 21
b = np.inf
c = -np.inf

# check for negative infinity
print(np.isneginf(a))
print(np.isneginf(b))
print(np.isneginf(c))

Output:

False
False
True

We get True as the output for the negative infinity and False as the output for the finite number and the positive infinity.

Example 2 – Element-wise check for negative infinity in a Numpy array using numpy.isneginf()

If you apply the numpy.isneginf() function on an array, it will return a boolean array containing True for values that are negative infinity and False for other values.

Let’s create a 1-D array and apply the numpy.isneginf() function to it.

# create a numpy array
ar = np.array([1, 2, np.inf, 4, 5, -np.inf, np.inf])
# check for negative infinity in ar
np.isneginf(ar)

Output:

array([False, False, False, False, False,  True, False])

We get a boolean array as an output. You can see that in the boolean array we get True for only the values that test as negative infinity in the original array.


Upskill your career right now →

Summary

In this tutorial, we looked at how we can use the numpy.isneginf() function to check for negative infinity in a Numpy array. Keep in mind that if you pass a scaler value, it returns a boolean value and if you pass a 1-D array it returns a boolean array.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.