How to create a matrix with only NAN values with numpy ?


To create a matrix with only nan values with numpy a solution is to use numpy.empty((), numpy.nan and fill():

import numpy as np

A = np.empty((5,5))

A.fill(np.nan)

returns

[[nan nan nan nan nan]
 [nan nan nan nan nan]
 [nan nan nan nan nan]
 [nan nan nan nan nan]
 [nan nan nan nan nan]]

Another solution is to do

A[:] = np.nan

Note: to check of an element of the matrix is nan, a solution is to use numpy.isnan

np.isnan(A[2,3])

returns

True

References