w3resource

NumPy: Test element-wise of a given array for finiteness, positive or negative infinity, for NaN, for NaT, for negative infinity, for positive infinity

NumPy Statistics: Exercise-11 with Solution

Write a NumPy program to test element-wise of a given array for finiteness (not infinity or not Not a Number), positive or negative infinity, for NaN, for NaT (not a time), for negative infinity, for positive infinity.

Sample Solution:

Python Code:

# Importing the NumPy library
import numpy as np

# Testing element-wise for finiteness (not infinity or not a Not a Number)
print("\nTest element-wise for finiteness (not infinity or not Not a Number):")
print(np.isfinite(1))      # Check if 1 is finite
print(np.isfinite(0))      # Check if 0 is finite
print(np.isfinite(np.nan)) # Check if NaN is finite

# Testing element-wise for positive or negative infinity
print("\nTest element-wise for positive or negative infinity:")
print(np.isinf(np.inf))    # Check if inf is infinite
print(np.isinf(np.nan))    # Check if NaN is infinite
print(np.isinf(np.NINF))   # Check if negative infinity is infinite

# Testing element-wise for NaN (Not a Number)
print("Test element-wise for NaN:")
print(np.isnan([np.log(-1.), 1., np.log(0)]))  # Check if the elements are NaN

# Testing element-wise for NaT (not a time)
print("Test element-wise for NaT (not a time):")
print(np.isnat(np.array(["NaT", "2016-01-01"], dtype="datetime64[ns]")))  # Check for NaT

# Testing element-wise for negative infinity
print("Test element-wise for negative infinity:")
x = np.array([-np.inf, 0., np.inf])
y = np.array([2, 2, 2])
print(np.isneginf(x, y))  # Check for negative infinity

# Testing element-wise for positive infinity
print("Test element-wise for positive infinity:")
x = np.array([-np.inf, 0., np.inf])
y = np.array([2, 2, 2])
print(np.isposinf(x, y))  # Check for positive infinity 

Sample Output:

Test element-wise for finiteness (not infinity or not Not a Number):
True
True
False

Test element-wise for positive or negative infinity:
True
False
True
Test element-wise for NaN:
[ True False False]
Test element-wise for NaT (not a time):
[ True False]
Test element-wise for negative infinity:
[1 0 0]
Test element-wise for positive infinity:
[0 0 1]

Explanation:

In the above code –

print(np.isfinite(1)): This line checks if a given value is finite, returns True if it is, False otherwise. Here, the code checks if 1 is finite, which is True.

print(np.isfinite(0)): Similarly, this code checks if 0 is finite, which is True.

print(np.isfinite(np.nan)): Here, the code checks if np.nan (not-a-number) is finite, which is False.

print(np.isinf(np.inf)): Checks if a given value is positive or negative infinity. Here, the code checks if np.inf is infinite, which is True. print(np.isinf(np.nan)): Similarly, this code checks if np.nan is infinite, which is False.

print(np.isinf(np.NINF)): Checks if a given value is negative infinity. Here, the code checks if np.NINF is infinite, which is True.

print(np.isnan([np.log(-1.),1.,np.log(0)])): Checks if a given value is NaN (not-a-number). Here, the code checks if np.log(-1.),1.,np.log(0) are NaN, which is True for np.log(-1.) and np.log(0).

print(np.isnat(np.array(["NaT", "2016-01-01"], dtype="datetime64[ns]"))): Checks if a given value is a "not a time" (NaT) value. Here, the code checks if the array [ "NaT", "2016-01-01"] is a NaT value, which is True for the first element.

x = np.array([-np.inf, 0., np.inf]) y = np.array([2, 2, 2]) print(np.isneginf(x, y)): Checks if a given value is negative infinity with support for broadcasting. Here, the code checks if -np.inf is a negative infinity with respect to each element of y, which returns an array [ True, False, False ].

x = np.array([-np.inf, 0., np.inf]) y = np.array([2, 2, 2]): This line defines two arrays x and y for the next line of code.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to compute pearson product-moment correlation coefficients of two given arrays.
Next: Write a Python NumPy program to compute the weighted average along the specified axis of a given flattened array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/numpy/python-numpy-stat-exercise-11.php