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

## How to test for infinity in a Numpy array?

You can use the `numpy.isinf()`

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

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

It returns a boolean value (`True`

if the value is positive or negative infinity otherwise `False`

) if you pass a scaler value and a 1-D boolean array if you pass an array.

## Examples

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

### Example 1 – Check if a number is an infinity or not using `numpy.isinf()`

First, let’s pass scaler values to the `numpy.isinf()`

function.

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.isinf()`

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 infinity print(np.isinf(a)) print(np.isinf(b)) print(np.isinf(c))

Output:

False True True

We get `False`

as the output for the scaler value `21`

and `True`

as the output for both the positive and the negative infinity values.

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 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.

### Example 2 – Element-wise check for infinity in a Numpy array using `numpy.isinf()`

If you apply the `numpy.isinf()`

function on an array, it will return a boolean array containing `True`

for values that are infinity and `False`

for finite values.

Let’s create a 1-D array and apply the `numpy.isinf()`

function to it.

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

Output:

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

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 infinity in the original array.

## Summary

In this tutorial, we looked at how we can use the `numpy.isinf()`

function to check for infinity (both positive and negative infinities) 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 –

- Numpy – Select Random Elements From Array
- Check If Two Numpy Arrays are Equal
- Numpy – Count Values Between a Given Range

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