w3resource
NumPy Exercises

NumPy: Test element-wise for NaN of a given array

NumPy: Basic Exercise-7 with Solution

Write a NumPy program to test element-wise for NaN of a given array.

Sample Solution :

Python Code :

import numpy as np
a = np.array([1, 0, np.nan, np.inf])
print("Original array")
print(a)
print("Test element-wise for NaN:")
print(np.isnan(a))

Sample Output:

Original array
[  1.   0.  nan  inf]
Test element-wise for NaN:
[False False  True False]                         

Pictorial Presentation:

NumPy: Test element-wise for NaN of a given array.

Python Code Editor:


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

Previous: Write a NumPy program to test element-wise for positive or negative infinity.
Next: Write a NumPy program to test element-wise for complex number, real number of a given array. Also test if a given number is a scalar type or not.

What is the difficulty level of this exercise?