np.nan: How to Use NaN in Numpy Array

What is np.nan, and why is it used?

The np.nan is the IEEE 754 floating-point representation of Not a Number.

The NaN and NAN are aliases of nan.

The nan stands for “not a number“, and its primary constant acts as a placeholder for any missing numerical values in the array.

The nan values are constants defined in numpy: nan, inf.

NaNs can be used as the poor man’s mask, which means you don’t care what the original value was.

While we already covered a couple of different ways to handle NaN values, I would like to go into a little more depth on some of the NaN functions in NumPy.

The majority of the data you will be working with will be given to you.

As we have seen, when we use Pandas to import DataFrame, any missing value is automatically replaced with NaN as a placeholder. But we can also mimic the same behavior directly in NumPy.

Syntax

numpy.nan

Example

# app.py

import numpy as np

myarr = np.array([1, 0, np.nan, 3])

print(myarr)

Output

python3 app.py
[ 1.  0. nan  3.]

The important thing I would like you to take away from this is that all of our integers have been converted to floats, and that’s because NumPy has defined the NaN data type as a float.

Due to implicit upcasting, all our elements have been converted to float data types. 

NaN items also precede every other item when mathematical operations are used.

See the nan value in the console.

# app.py

import numpy as np

print(np.nan)

Output

python3 app.py
nan

Let’s compare two NaN values and see if they are identical.

# app.py

import numpy as np

print(np.nan == np.nan)

Output

python3 app.py
False

np.isnan

The np.isnan() is a numpy library function that tests element-wise for NaN and returns the result as a boolean array.

Numpy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). What this means is that Not a Number is not equivalent to infinity.

# app.py

import numpy as np

print(np.isnan(np.nan))

Output

python3 app.py
True

Conclusion

When you prepare data for Machine learning or AI, it is essential to first check for it, then make sure you have removed or replaced every NaN element during the preprocessing phase.

Missing a single NaN element can cause significant problems in your final result because of its ability to propagate throughout your data.

That’s it for this tutorial.

Related posts

Python isnan()

Python isinf()

Numpy isfinite()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.