Determine whether a NumPy array contains positive infinity, or negative infinity individually

By IncludeHelp Last updated : November 25, 2023

Problem Statement

Given a NumPy array, write a Python program to Determine whether a NumPy array contains positive infinity, or negative infinity individually.

Determining positive infinity in an array

Use the numpy.isposinf() method along with the .any() method. It will return True if an array contains any positive infinity; False, otherwise.

Syntax

np.isposinf(arr).any()

Example

Python program to determine positive infinity in an array

# Importing numpy library
import numpy as np

# Create two arrays
arr1 = np.array([1, 2, 3, np.inf, 5])
arr2 = np.array([1, 2, 3, -np.inf, 5])

# Checking positive infinity in arr1
if np.isposinf(arr1).any():
    print("arr1 contains positive infinity")
else:
    print("arr1 does not contain positive infinity")

# Checking positive infinity in arr2
if np.isposinf(arr2).any():
    print("arr2 contains positive infinity")
else:
    print("arr2 does not contain positive infinity")

Output:

arr1 contains positive infinity
arr2 does not contain positive infinity

Determining negative infinity in an array

Use the numpy.isneginf() method along with the .any() method. It will return True if an array contains any negative infinity; False, otherwise.

Syntax

np.isneginf(arr).any()

Example

Python program to determine negative infinity in an array

# Importing numpy library
import numpy as np

# Create two arrays
arr1 = np.array([1, 2, 3, np.inf, 5])
arr2 = np.array([1, 2, 3, -np.inf, 5])

# Checking negative infinity in arr1
if np.isneginf(arr1).any():
    print("arr1 contains negative infinity")
else:
    print("arr1 does not contain negative infinity")

# Checking negative infinity in arr2
if np.isneginf(arr2).any():
    print("arr2 contains negative infinity")
else:
    print("arr2 does not contain negative infinity")

Output:

arr1 does not contain negative infinity
arr2 contains negative infinity

Determining negative or negative infinity, individually in an array

Use two separate conditional statements for determining both types of infinity individually and use numpy.isposinf() and numpy.isneginf() respectively along with the .any() method.

Syntax

if np.isposinf(arr).any():
 # message 
else:
 # message

if np.isneginf(arr).any():
 # message 
else:
 # message

Example

Python program to determine positive or negative infinity individually in an array

# Importing numpy library
import numpy as np

# Create two arrays
arr1 = np.array([1, 2, 3, np.inf, 5])
arr2 = np.array([1, 2, 3, -np.inf, 5])

# Checking positive infinity in arr1
if np.isposinf(arr1).any():
    print("arr1 contains positive infinity")
else:
    print("arr1 does not contain positive infinity")

# Checking positive infinity in arr2
if np.isposinf(arr2).any():
    print("arr2 contains positive infinity")
else:
    print("arr2 does not contain positive infinity")

# Checking negative infinity in arr1
if np.isneginf(arr1).any():
    print("arr1 contains negative infinity")
else:
    print("arr1 does not contain negative infinity")

# Checking negative infinity in arr2
if np.isneginf(arr2).any():
    print("arr2 contains negative infinity")
else:
    print("arr2 does not contain negative infinity")

Output:

arr1 contains positive infinity
arr2 does not contain positive infinity
arr1 does not contain negative infinity
arr2 contains negative infinity

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.